Home » » BASIC

BASIC

Written By Unknown on Tuesday 7 May 2013 | 20:53

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++;
}
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

  1. clear all
  2. clc
  3. arduino=serial('COM4','BaudRate',9600);
  4. fopen(arduino);
  5. x=linspace(1,100);
  6. for i=1:length(x)
  7. y(i)=fscanf(arduino,'%d');
  8. end
  9. fclose(arduino);
  10. disp('making plot..')
  11. 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:

1 comments:

HOST PARTNER