6 December 2015

Sending Data From Arduino To Matlab Via USB Without Additional Tool

This brief tutorial discussed the integration of Matlab 2010a and Arduino Uno development board. Analog signals coming from the sensor (in this case used a light sensor) is processed by the arduino into a digital signal. The signal then transferred by the Arduino via the USB port to the PC. Matlab software is set up to read the data transferred via USB and visualize the results using the "plot" command. This tutorial uses only the Arduino Uno and Matlab without additional software, making it very easy to implement.

Code for Arduino
Open your Arduino IDE, use the code below

#include <SoftwareSerial.h>
float sensorPin = A0; // I use potensiometer as input and set it on analog pin

float sensorValue = 0; //declare SensorValue as float with initial value zero

void setup() {

Serial.begin(115200); //activate serial port wit 115200 baudrate, change the baudrate as you like,
it will effect the speed of visualization in Matlab
}

void loop() {

sensorValue = analogRead(sensorPin); // reading analog value from sensorPin, in this case A0


Serial.println(sensorValue); // sending the value to serial port (USB)

delay(200);// give some delay, it also effect the speed of visualization in Matlab

}


After you done with the code, compile and just upload it to Arduino

Code for Matlab 
While for the Matlab you can use this code, click file-new-script(m-file), and paste it

s = serial('COM20'); %make sure you put the same port with your Arduino device

set(s, 'InputBufferSize', 256);

set(s, 'BaudRate', 115200);
%make sure you also put the same baudrate as your Arduino device
set(s, 'DataBits', 8);
set(s, 'Parity', 'none');
set(s, 'StopBit', 1);
set(s, 'FlowControl', 'hardware'); %do not use 'none', it will slowdown the process
set(s, 'Timeout', 10);

disp(get(s, 'Name')) %just to visualize the port configuration 
prop(1)=(get(s, 'BaudRate'));
prop(2)=(get(s, 'DataBits'));
prop(3)=(get(s, 'StopBit'));
prop(4)=(get(s, 'InputBufferSize'));
disp(['Port Setup Done ', num2str(prop)])

fopen(s); % opening the port
t=1;
disp('start')
 

while(t>0)
data=fscanf(s, '%f'); %storing the received data to variable “data”

plot(t,data,'*') %visualize it in graph
axis ([t-20 t 0 1100]) %configuring X and Y axis boundaries

hold on;
t=t+1;
drawnow;
end

fclose(s); %closing the port
clear s; %clear the variable s memory, just for refreshment of your PC



After you done with the Matlab Code, Run it. Matlab will visualize your port setting as shown in figure 1.

 
Figure 1. Baudrate and serial port information (source: link)

 
Figure 2. Real time Plot (source: link)

While plot windows will appears after as shown in figure 2. Just try to change the analog input in your Arduino, the plot will also change. Keep in mind that the plot will not appear if the port is in use by another application, including serial monitor on Arduino IDE, so you have to close it first and reset the arduino board. Piece a cake huh??

Reference:
[1]http://www.mathworks.com/matlabcentral/answers/111231-real-time-data-acquisition-from-arduino-using-simulink
[2] http://billwaa.wordpress.com/2013/07/10/matlab-real-time-serial-data-logger/
[3] http://playground.arduino.cc/Interfacing/Matlab
[4] sabakukyu.wordpress.com/2013/07/23/arduino-matlab-serial-plot/‎



No comments:

Post a Comment