How To Send Data From The Arduino To MATLAB
Guys in this tutorial you will come to know the very basic of arduino and matlab data transfer and I hope you will enjoy the same.
So let's go go step-wise
1. Upload your arduino with the code
code:
int i=0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println(i);
i++;
}
int i=0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println(i);
i++;
}
EXPLANATIONS
int i=0; // this will initialize a variable with the first value =0(zero)
void setup()
{
} // the loop will execute once and what-ever the codes within the parenthesis will be evaluted once the loop will execute only at the time when the power is on or when the reset is pressed.
Serial.begin(9600); // this will open serial port and set the data rate to 9600;
void loop()
{
} // this is loop similar to while(1) and it executes for infinite no. of time and the code within the parenthesis will b evaluated each time the loop executes.
Serial.println(i); // this will print data to serial port
i++; // it's a normal incremental to just to print different values each time and this will decide the graph or the nature of curve.
HERE we have used [i++] i.e it will follow 0, 1 2 3 4 5 6 7 8 and so on so the graph will be a linear one.
NOTE: You can try using different maths operation instead of [i++] for example [i*i]
THE MATLAB CODE TO BE EXECUTED
- clear all
- clc
- arduino=serial('COM4','BaudRate',9600);
- fopen(arduino);
- x=linspace(1,100);
- for i=1:length(x)
- y(i)=fscanf(arduino,'%d');
- end
- fclose(arduino);
- disp('making plot..')
- plot(x,y);
NOW YOU CAN VERIFY YOUR RESULTS ON THE GRAPH . OBTAINED AT YOUR MATLAB OUTPUT
NOW YOU ARE DONE ENJOY AND COMMENT.
THANKS:
NOW YOU ARE DONE ENJOY AND COMMENT.
THANKS:
thanks bro i was searching for it
ReplyDelete