Introduction: Arduino and Matlab: Let Them Talk Using Serial Communication!

Picture of Arduino and Matlab: Let Them Talk Using Serial Communication!

Hi Everybody!

since when I've started playing with Arduino, I have always been wandering how I could make it talk and interact with the Matlab environment in an easy and effective way. I bumped into many Instructables and tutorials dealing with Arduino libraries and Matlab script files to get them talking one to each other. What I've never been able to find, though, was a straightforward tutorial that could teach me how to understand the core of the serial communication with an external hardware.

Here's an Instructable that will guide you throughout the essential steps you will be in need of, if you want to acquire data from the external world using the Arduino interface without giving up using the powerful data visualization and elaboration engine given by Matlab! It is extremely easy to understand it! I know the struggle one can go through facing for the first time this kind of things!

I'll be go through few steps in which I'll be illustrating the basis of a serial communication established between Arduino (our external physical interface) and our PC (the data collector and elaborator). Everything will be referred to a very simple case study of a temperature/humidity sensor (DHT_22) wired up to Arduino, and that will act as the data source. The fun thing is that this Instructable applies quite unchanged to almost all tipe of sensors you want to get and visualize data from! So temperature, humidity as well as light, proximity sensors or 3-axis gyroscopes....The core is really the same!


Hope it will be helpful to all of you interested!!

Step 1: Getting the Concept.

Picture of Getting the Concept.

The general idea one should keep in mind when it comes to wire up Arduino to Matlab is that they shall communicate using the same language and protocol. That is the serial communication via USB port. Both In Arduino and Matlab coding environment we have specific commands that enables the serial communication, but the core behind its operation is built on the same few principles:

  • Arduino and Matlab should both know the rate (BAUD RATE) at which they share infos and the physical serial port they are talking through, otherwise they are not going to understand each other;
  • They both have commands to send (WRITE) data to the other part through the serial port;
  • ...and of course commands to receive and READ data from the serial buffer, which is a temporary storage space in which written data are kept waiting to be picked up and removed (READ).

What we are going to do, after wiring up the temperature sensor as illustrated in the Fritzing scheme, is writing the code that will run on Arduino, and a Matlab script with a bunch of functions, that will allow us to query Arduino for the temperature readings.

Before illustrating the codes in detail, let's fix the concept by schematizing the whole thing:
on the Arduino board we will upload a program that, when asked by a serial command (serial means that passes through the serial port!), will make the board retrieve the temperature reading from the sensor, and subsequently copy it in the serial buffer, ready to be picked up by Matlab!
Matlab, on the other side, will do the rest of the job: a script will be run, and at a fixed frequency, will send the Temperature reading request string to Arduino, and will read back the temperature value sent to the serial buffer plotting it in a real time graph Temperature vs Time.

That's it! Now let's start moving a little bit further....

Step 2: The Arduino Code!

Attached here, you will find the .ino file with the script you will have to upload on your board, but since I said that this want to be a tutorial whose main aim is to lead you directly to the core of the versatile serial communication between Arduino and Matlab, let me comment it! (you may want to open the .ino file in the Arduino IDE and follow the following comments in parallel, trust me it will help you). I really don't like to fill the script itself with comments, since it result in a messy look!
Skip the following lines if you are using a different sensor, or your purpose for using the serial communication with Matlab is different! The script start with the inclusion of the DHT library you will find attached as well (copy the .cpp and .h files in a folder called DHTlib inside the libraries folder related to Arduino in your PC, you aren't going to need anything else) and defining a couple of useful variables: temp, to store the temperature value, and val, assigned to the characters sent by Matlab.
In the setup area we have to include the real player of the game: serial communication initializer. more info here>> Arduino Serial. So we write Serial.begin(9600), where 9600 is the baud rate, namely the velocity at which we would like the two guys to talk to each other (we will set the same rate when we will take care of the Matlab serial object). The few code lines that follow are just a simple handshake to know if the serial communication has started flawlessly. Actually you can exclude it! Just not to leave anything unclear, with the handshake, Arduino send the character 'a' to the serial buffer, and when Matlab will read it visualizing an "ok message" we will know that they are talking to each other properly.
Now let's come to the main loop. Arduino always has to keep an eye wide open on what it's happening in the serial port! So with the line

if (Serial.available() > 0)

we are asking him to check as fast as he can, if something has been put in the famous serial buffer by Matlab, and if it is the case, to store the value in the variable val!
With the further if loop:

if (val == 'R')

we check if the command sent through the serial port, coincide with the one we assigned to the sensor reading. If even this condition is fulfilled, the reading function getTemperature() is launched, and the correlated temperature value is sent back to the serial buffer with the Serial.println(val) function.
We are done here with Arduino. You can upload the sketch, and leave the Arduino connected through the serial port (USB) to the computer. One last important thing: make sure you know the name of the port Arduino is connected to. You can check it by going to "instrument" in the Arduino IDE, and checking which port is ticked in the top down menu named "port". Write down the name, we will use it later! If you are a Windows user, it will be something like "COM1" or "COM2" ecc., if you are using MAC, it will be something like "/dev/tty.KeySerial1".

Step 3: Matlab: Let's Tame the Beast!

Here I assume that you are familiar with the Matlab environment, at least with the basis. If my assumption is not correct, don't panic, and have a look here: Matlab for beginners!
I'll tackle the Matlab side in the following way: I will create a single script .m file that will include all the functions we are going to use. In this way, once you'll get the grasp on each section of the script (MainScript), all you have to do is launch it by writing its name in the workspace, and suddenly you will have Arduino and your PC exchanging data nicely and easily! Make sure you save all the three documents I've attached here inside the same folder that has to be selected as the current folder in the "Current Folder" window in the Matlab main view! As a general rule you must remember that Matlab has to access easily the function you are citing inside the main script, otherwise you are getting an error signal of a function not declared!
We will start the script with saving the serial port name in the variable comPort. As I told you before, make sure you know the exact name of the port Arduino is connected to. With the new section, we take care of the serial communication initialization. We are going to write a function file named setupSerial, which accept as input the serial port header, and gives back the serial object arduino and the "serialFlag" value that tells us that the serial object has been created.
useful tip: if you need some help with the syntax function or with any other Matlab command, don't hesitate to ask Matlab! You have just to type in in the Matab command window, "help" followed by the topic you want to know more about. If the topic exists, Matlab will give you a short answer directly in the command widow, otherwise you can navigate the "help browser" by typing "doc" in the workspace and click enter.
Let's move on! I'll will end analyzing the MainScript before taking in consideration the other functions.
It comes the section devoted to the graphic visualization of the data: a figure h is created, and a system of an x and y axis and a line, that will help us visualizing the data we are getting from the sensor., are created as well Keep in mind that every time you create an object, whatever its type might be (serial objects, a figure, a GUI object...ecc) you create also a series of properties associated to it. You can use those properties to call functions or to create condition for loops (while, for, if...ecc). Does the "if(~exist('h','var'))..." command make more sense now?? NO? Listen: we want to check if the property var of the object h is equal to zero! if it is the case we have to create the figure object, since a 0 in the var property, means that the figure does not exist yet!!

Now let's come to the last section of the script. Here you have few lines that act as the core of the whole program: with the "while" loop we are going to send to Arduino the "R" command, that stands for "Hey you, read the Temperature for me, and send me back the result of your reading!". You will notice that we have a new function inside the while loop that is readTemp, briefly, it will take care of the bureaucracy that lies behind the serial communication from the Matlab point of view. I will come back to it when I'll analyze the two functions of the script. One piece of information is missing: the sampling rate, at which we would like to get temperature readings. It is expressed by the "pause" command at the end of the while loop! If you want to pick a temperature value every hour, you have to write pause(3600) (pause command accept the parameter in seconds).
Ok, now let's analyze the two functions:
setupSerial: this creates the serial object Arduino connected to the serial port comPort. The command to create the serial object is simply name-of-the-object = serial(name-of-the-port). As simple as that! Note that, as one of the property of the serial object, we are setting (using the set command, followed by the property whose value we want to set) the baudrate of 9600 equal to the one set on the Arduino.

readTemp: Few more things you need to now: remember the commands we used on Arduino to WRITE into and READ from the serial buffer, Serial.println and Serial.read? Here are replaced by fprintf and fscanf respectively. That's it. The syntax is easy and you can check it in Matlab help browser. fprintf requires, as the first entry, the serial object we want to write to, and as the second piece of information the character we would like to send (in our case "R" which means for us "Read"). fscanf wants the serial object as well, and the format we want the data to be visualized, in our case a floating point number indicated with the simbol "%f" (DHT 22 temperature values are resolved down to one tenth of celsius degree).

Note that, the amount of readings that are going to be saved will be equal to the value of the variable "buf_len" (the default value in the script is 30, change it at your own whim!). After the 30th reading, the new one will be overwritten on the first reading stored in the "tcdata" array, which will contain all the temperature values in a chronological order.

Step 4: Summing Up...

Picture of Summing Up...

An here you real time Temperature reading!

To conlude: remember that you should be careful when you are using the serial communication. You may not want to create conflicts by sending commands through the serial port, while it is in unse by some other serial object! For this reason make sure that, after you are satisfied with your measurement, you close the serial object by typing in the Matlab workspace fclose(arduino). You have to close the figure h as well! (you can save it wherever you want though) using the command close(h). If you need the read data, keep in mind that they are all stored in the "tcdata" variable, so nothing has gone lost!

I personally believe that this set up is one of the most effective to enable the communication between the two part. It doesn't clog you PC with useless libraries and leaves you free to intervene in slender scripts personalizing them as much as you want, keeping constant a bunch of core commands. The possibilities at this point are endless!

Hope it has bee somewhat helpful to you, and please, if you have some comments or wise advices, don't hesitate to contact me, with a message down below! I'm a beginner too!!

Thanks, and enjoy this powerful tool!

Comments

rouis.jihene.20 (author)2017-06-02

I tried the code but i don't get the right value why ? any help please

TomasS47 (author)2017-02-22

Hi, i was searching, but iam still not sure what this line in Matlab code is for.

set(h,'UserData',1);

Could you explain?

Thanks !

laredj179 (author)2017-02-21

I have no folder that called DHTlib

Aralas (author)2016-08-10

Thank u very much let me try !

rohanpaleja27 (author)2016-08-02

Hi,

is there any way to make this plot in real time? This code takes all the data after its done and then plots. I would like plotting done as the sonar measures.

SitiZ2 (author)2016-04-05

Thank you for the tutorial. It is really helping me.

but when I run the coding there are error like the following:

Error using serial/fopen (line 72)
Open failed: Port: COM4 is not available. No ports are available.
Use INSTRFIND to determine if other instrument objects are connected to the requested device.

For your info ArduinoIDE detect my arduino but when come to Matlab this error is occured.

What I need to do?. Do I need to install support package for Arduino?

Rafael AccácioN (author)SitiZ22016-04-08

try to change the com4 port to another one, com3 for example, or use another usb port from your pc. Use instrfind and see if matlab recongizes your arduino

AdrianR54 (author)2016-03-23

Thank you very much for the tutorial, extremely helpful. However, in my application i would need to read the information provided by Arduino (Due) at certain frequency (higher than 100 Hz). Following your instructions, and removing pause() from matlab script, sample acquisition seems to be asynchronous (using tic toc in Matlab), that is, time between samples are not the same. Would you have any hint why that happen/ maybe something regarding serial communication protocols?

rtkilian (author)2016-02-19

Thank you gianluca88!

You mentioned in another comment you can have multiple inputs and plot them. I am creating a setup where I need to read force data from a load cell and positional data from an incremental encoder - then plot them on MATLAB. Do you know if I can read the data from the load cell and encoder synchronously?

HanaJin (author)2015-09-30

Nice tutorial. I wonder if you have heard of the MATLAB support package for Arduino (which comes with base MATLAB ) here,

http://www.mathworks.com/hardware-support/arduino-...

It takes care of the serial communication between MATLAB and Arduino for you and maps most of the Arduino commands into MATLAB functions. All you have to do is to write MATLAB scripts to read data from your sensors and plot it.

You may find it interesting or useful for your applications :)

gianluca88 (author)HanaJin2016-02-02

Dear Hana,

Thanks for the hint!
I know the package Matlab is providing, and I think that it's great! I'll surely implement it in my next instructables!

HaoxueB (author)2016-01-19

Thank you gianluca88! you saved my day! it was a really helpful tutorial.

and based on your comments, it is easy to modify too!!!

you are the angel!~!~hahah

gianluca88 (author)HaoxueB2016-01-20

Hi HaoxueB!

You are very welcome! Happy to have been helpful!!

Bye

HaoxueB (author)gianluca882016-01-27

Hi, gianluca88. May i ask what if i have multiple inputs? is it possible to plot all these into one graph? for example, a graph with many line

gianluca88 (author)HaoxueB2016-01-29

Hi HaoxueB!
sure thing! You can plot multiple signals on the same graph!
Make sure you modify the reading function, asking Arduino for the reading for another sensor or for the humidity value, for example, if you are working with DHT-type sensors.
I will try to clean a little bit the plotting section of the Matlab script (MainScript), in order to it to be clearer, but the simplest command to plot two lines on the same graph (having to different scaling) is:

plot(x,temp,x,humidity);

where temp and humidity are the variables in which readings from DHT sensor are stored! X, being in this case the time, is equal for both y datasets!

Gianluca

speedyhak (author)2016-01-25

that was helpful .. thanks a lot! ^_^
i have tried with DHT11 applying the same scripts but with a little bit changing in arduino source code at :
" DHT.read11 " instead of " DHT.read22 " ,
also changing analog pin no. to ...
" DHT22_PIN A1 " instead of " DHT22_PIN 2 "
and that's it. .. it works perfectly .. thanks again :))

gianluca88 (author)speedyhak2016-01-25

That's it! great job!
Using this approach, you can visualize any output from any sensor: Arduino will take care of the sensor query (using the proper protocol, I2C or SPI...), while the data can be easily visualized using the call function from Matlab. Data are then available and securely stored in a Matlab array for a future manipulation!

Thanks for the message!

Gianluca

Soon more Instructubles on Arduino-Matlab world! Stay tuned!

tomatoskins (author)2015-06-24

Great info! I haven't done anything with Matlab in a really long time. Thanks for sharing and welcome to the community!

gianluca88 (author)tomatoskins2015-06-24

Thanks man!

About This Instructable

71,534views

54favorites

License:

More by gianluca88:Arduino and Matlab: let them talk using serial communication!
Add instructable to: