Thursday 10 July 2014

MATLAB & ARDUINO serial communication

I have being asked by many people about interfacing Arduino UNO with MATLAB without using any supporting inbuilt functions. So , for answering all those queries I am posting this article by explaining MATLAB & Arduino UNO serial communication .
This post will cover the topic of controlling arduino pin13 LED by send serial command from MATLAB to arduino UNO.

The Arduino code required for establishing the serial communication and reading serial data is.

const int ledpin=13;
int recValue;

void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
}

void loop()
{
  if(Serial.available()>0)
  {
    recValue=Serial.read();

    if  (recValue == 100)            // If use will send value 100 from MATLAB                                           then LED will turn ON
     { 
       digitalWrite(ledpin, HIGH);
       }
        if(recValue == 101)          // If use will send value 101 from MATLAB                                            then LED will turn OFF
         { 
         digitalWrite(ledpin, LOW);
         }
      }
    }



Upload the above program on Arduino UNO and then open MATLAB environment for establishing serial communication with UNO.
The MATLAB code required for establishing communivcation and sending required command for toggling LED is:

s=serial('COM','BAUD', 9600); % Make sure the baud rate and COM port is 
                              % same as in Arduino IDE
fopen(s);

for m=1:10                    % The program will execute 10 times , 
                              %you can change the vaalue of m to increase or decrease
                              %the number of executions

servalue= input('Enter the value 100 to turn ON LED & 101 to turn OFF LED :');

fprintf(s,servalue);          %This command will send entered value to Arduino 
end


After opening the MATLAB execute this program, initially a LED on PIN13 on Arduino UNO will blink that denoted handshaking or serial communication establishment between UNO and MATLAB . Then MATLAB will ask user to enter desired value, make sure to enter 100 to turn ON and 101 to turn OFF LED.

Note: Make sure the baud rate and COM port entered in MATLAB code is similar to the one in Arduino IDE

For any further query commen on this post or post your question on

Google+ account name MATuino R
https://plus.google.com/u/0/106198205604943884729/posts

Facebook community page arduinomatlabrobotics
https://www.facebook.com/arduinomatlabrobotics?ref=hl

1 comment:

  1. Thanks

    So doe that mean I can control, say, more actuators from MATLAB that are connected to the Arduino board. Do you have any case illustrating that with, say, a servo motor?

    ReplyDelete

Browse and display image in MATLAB

First of all, excuse us for not posting any tutorial and videos on our you tube channel for such a long time. Recently one of our subscrib...