Serial Communication between MATLAB and Arduino

Interfacing MATLAB and Arduino

  Serial Communication between MATLAB and Arduino

components Required:

Arduino board
MATLAB software
A-B connector (USB cable suitable for arduino BOARD)

Serial communication

First open your MATLAB software and write a command in your command window.
We need to create a serial port object. Serial port object is just a name given to that serial port so that we can use it in later commands to communicate with Arduino.

>> s = serial ('COM#'); 

%%in place of # write the proper COM number.


Serial Port Object : Serial-COM1Communication SettingsPort: COM1BaudRate: 9600Terminator: 'LF'Communication StateStatus: closedRecordStatus: o_Read/Write StateTransferStatus: idleBytesAvailable: 0ValuesReceived: 0ValuesSent: 0

This line of command only constructs the object. It does not check/setup/initialize the communication. This command will still work even if the serial port is not connected to any device. Many objects can be created for a serial port but only one can be connected at a time. This shows all the property of the constructed serial port object.
In MATLAB, s is a structure which has all the above properties. We can access/modify them using dot(.) operator. Note that the Status is closed. It implies that the serial port is not connected.
Baud Rate is the rate of transfer of the data in bits/sec.
We can change the BaudRate using the set method as follows-


>> set(s, 'BaudRate', 9600);  %% or using other command>> s.BaudRate = 9600;

You can also setup different BaudRate while making the serial port object as follows-

>> s = serial('COM1','BaudRate', 9600);

Setup the Connection


Before you actually write the data to the serial port, you must connect Arduino to computer. This is like a JAVA lock. Only one entity can acquire the lock at a time. Use fopen to acquire the lock and setup the connection.

>> fopen(s)

Notice the status property of the serial port object-

>> s.Statusans = open

If the lock is already acquired, fopen will give an error. To avoid error, first check the Status property of the serial port object. If it is closed then try to setup the connection.

Writing to Serial Port in MATLAB

MATLAB can write any kind of data to the serial port binary, string, int, oat etc. with specified precision. We use fwrite orfprintf to write data.

Transfer an int/float array-


>> fwrite(s, vector array, 'precision');

The precision specifies the data type of the vector array. It can be 'int8', 'int16', 'oat32', 'oat64', 'uint8', 'char' etc.
String-

>> fwrite(s, 'string');

You can specify the format in fprintf for a string.

Reading from Serial port on an Arduino


You can follow these steps to read data in Arduino

%%%%%%%%Program of Arduino%%%%%%%%%%%%%%%%%%%%%

void setup()  

   {

    Serial.begin(9600);  

    }     

void loop()

     {

if(Serial.available()>0)

    byte b = Serial.read();        //Process the Data        

} }

You can choose the kind of data you are expecting, otherwise byte data type can be used.


Example: To switch on the Led in Arduino through MATLAB



**************************MATLAB CODE *********************

s=serial('COM1','BAUDRATE',9600);   %%%to create the serial port in MATLAB
fopen(s);                            %%%open the serial port
fwrite(s,1);                          %%%send 1 through Serial port to Arduino
fwrite(s,2);                          %%%send 1 through Serial port to Arduino
fclose(s);                              %%after completing your work close the port

***********************ARDUINO CODE*********************** 

void setup()

{

Serial.begin(9600);

pinMode(13,OUTPUT);      

}

void loop()

{

if(Serial.available()>0)

{

        int b = Serial.read();   

 if(b==1)

{

//switch on the led

digitalWrite(13,1);

}

if(b==2)

{

//switch off the led

digitalWrite(13,0);

}} }

**********************end******************

NOTE:
  • Make sure you can not open the serial monitor of Arduino while doing interfacing with MATLAB
  • First you have to write a code in Arduino and then write the code in MATLAB
  • If you will get a error in MATLAB then try to restart the MATLAB. 

click here to know in details.


If you found this tutorial helpful, feel free to Share it. It really means a lot to me! and do comment if i can help you in any way.

No comments:

Post a Comment