Sunday, November 2, 2014

Interfacing Arduino Uno with MATLAB

Method-1:
Step-1: Download the ArduinoIO MATLAB support file from here 
Step-2: Download the Source file from here 


Step-3: Add the downloaded ArduinoIO folder to your MATLAB bin path 
in my case it is "C:\Program Files\MATLAB\R2012a\bin"

Step-4: Connect your Arduino board to your PC/Laptop

Step-5: Open Arduino IDE Tool and copy the downloaded source file on to the new sketch file. Compile it and upload it on to the Arduino Uno board.

Step-6: Now Open your MATLAB and type "arduino" on command prompt. You will see some scrolling message. Then type "a=arduino('COMx'), x is your COM port number which is assigned to your board. 

Step-7: If you want to read analog port data then type "b=a.analogRead(0)" this will read analog port A0 data. If you want read/write any digital pin use these instructions
 a.pinMode(9,'input'); 



 a.pinMode(13,'output');

 a.digitalwrite(13,0) ;





arduino;
a=arduino('COM3'); // In my case it is connected to COM3 port
t=1:1:100;
b=a.analogRead(0);
plot(b);
a.pinMode(8,'input');
a.digitalRead(8);
a.pinMode(13,'output');
a.digitalWrite(13,1);
pause(1)
a.digitalWrite(13,0);

Method-2:

Step-1: Write this code in Arduino IDE tool and upload this code on to your board. 
void setup()
{
   Serial.begin(9600);
  while (!Serial) {
      ; // wait for serial port to connect. Needed for Leonardo only
    }
}

void loop()
{
  Serial.print("Hello World!");
 // Write your code here
}

Step-2: Open MATLAB and type this code in matlab file and run the code

s = serial('COM3'); // In my case it is connected to COM3 port
fopen(s);
fprintf(s, 'Your serial data goes here');
out = fscanf(s);
% Data Analysis 
fclose(s);
delete(s);
clear s;