viernes, 22 de febrero de 2013


Usar el Arduino UNO conectado al USB como tarjeta de adquisición de datos midiendo la señal del sensor GP2D12 conectado a la entrada analógica en A0.

Se requiere que en el ARduino se haya previamente cargado el servidor del puerto serie. Ver enlace siguiente.

http://youtu.be/DKA5k-G_ESw



function GP2D12

%% crea un objeto arduino y conecta con la tarjeta
if exist('a','var') && isa(a,'arduino') && isvalid(a),
    % nada   
else
    a=arduino('COM7');
end

%% inicializar pins
temp=0.001;% en segundos
t=0;
disp('Inicializando Pins ...');
a.pinMode(9, 'OUTPUT'); % pwm available here
a.pinMode(13,'OUTPUT'); 
 while(1) 

%Intermitencia en pin 13     
a.digitalWrite(13,1);
pause(temp);
a.digitalWrite(13,0);
pause(temp);

%Leer entrada analógica del pin # 0
ain=a.analogRead(0); 

%Graficar
t=t+temp;
voltios=ain*5/1024;
  %Linealizar
   ainlin=((((inv(voltios+4))*10)-1.5))*5
line(t,ainlin);
Title('Voltios Entrada Analógica 0 en PIN A0');
%Fin graficar
 end

end


Arduino UNO working standalone with the model compiled and simulated from Simulink.
One interested task if you have experience working with Simulink, and you want to run your Simulink models in the Arduino board, is not a big deal when your read this memory.

Your requirements are:

Software:  
1.      Matlab with Simulink R2011a Version 7.12.0.635 or newer.
2.      The IDE Arduino software to program the Arduino UNO board .  “ arduino-0023.zip “                     
3.      Simulink package for the Arduino.  “ Simulink_support_package_arduino.zip ”

Hardware:
  1. Arduino™ UNO rev3  aprox. 22€
  2. USB cable aprox. 4€
  3. Some LEDs to check your first programs with some 220 Ohms resistors.
Process:
  1. Install the driver for Windows of the Arduino UNO
  2. Unzip “ arduino-0023.zip “   -> For instance at: 'C:\arduino-0023’
    1. It will create the folder 'C:\arduino-0023\arduino-0023’
    2. In this folder you will find the IDE file, the arduino.exe (This is the file that Simulink will call to compile the target file to send from Simulink environment to the Arduino Board via the USB port.
  3. You should have already the Matlab + Simulink in your PC.
  4. Unzip the file ‘ Simulink_support_package_arduino.zip ‘
    1. It will create 3 folders:
                                                              i.      \arduino  It has inside the folder +arduino and inside it a file Prefs.m
                                                            ii.      \blocks
                                                          iii.      \demos   It contains simulink mdl files like the example ‘demo_arduino_blink.mdl’   This make flashing one led in the pin 13
  1.  Open Matlab and type:

    •       cd C:\simulink_support_package_arduino
    •       addpath(fullfile(pwd,'arduino'),fullfile(pwd,'blocks'),fullfile(pwd,'demos'))
    •       savepath

    • arduino.Prefs.setArduinoPath('C:\arduino-0023\arduino-0023')
    • comPorts=arduino.Prefs.searchForComPort
    • arduino.Prefs.setComPort('com3')
    • arduino.Prefs.setBoard('uno')
    •  demo_arduino_blink

This will select the board as Arduino uno and the com port will be shown.
Note that you need to have the Windows driver installed to recognize the Arduino UNO board as hardware.




  1. Once the model demo_arduino_blink.mdl is loaded in Simulink, if you have the board connected, you only need to do this to download the model on the board, to execute it stand alone. After this the board will not need to be connected to the PC.

  1. Build and deploy the executable by pressing Ctrl+B, or choosing
Tools > Code Generation > Build Model.

 If the deployment is
successful, you should see the LED connected to PIN 13 blinking
once per second.


If you did like this project you can donate some cents of Bitcoins SV in the following address.
1sAZQMy5Ci1G88CmbucFJDsF7TxeXAcko
or bitcoin cash in the address:
qzmjpeqrlgd3flltpavm9t2xh0nz8y97mggajvvqnv
thanks😊