٢٠١٨/١/٦ How To Send Data From The Arduino To MATLAB Communication
Home
Suggest a Tutorial
Recommended Books
Microcontrollers
Subscribe
How To Send Data From The Arduino To
MATLAB
by Miguel on July 4, 2011
in Arduino
I’ve been learning MATLAB lately and playing with the Arduino more and more, then I started to wonder
how to make the Arduino communicate with this program, it turns out it’s not hard at all. See the code below.
What The Program Will Do
It will send the value of a variable from the Arduino to MATLAB and plot them.
Arduino Serial Code
If you have used the Serial class of the Arduino before this won’t be new to you. Simply print some data with
the Serial.print() function.
1. int i=0;
2. void setup()
3. {
4. Serial.begin(9600);
5. }
6.
7. void loop()
8. {
9. Serial.println(i);
10. i++;
11. }
For those who haven’t used the serial class before all am doing in the code above is sending the value of the
variable i at a baud rate of 9600.
MATLAB Serial Class
This is just as easy the Arduino.
First we want to create an object of the class serial. Set the port to the port you are using to program the
Arduino, am using COM4 to program it. Next set the baud rate you set in the Arduino code above.
1. arduino=serial('COM4','BaudRate',9600);
http://allaboutee.com/2011/07/04/how-to-send-data-from-the-arduino-to-matlab/ 1/5
٢٠١٨/١/٦ How To Send Data From The Arduino To MATLAB Communication
Now we need to open the arduino as if it were a file.
Veeam Explorer for SharePoint
Restore anything in minutes
Quick and easy granular eDiscovery
and recovery for SharePoint. Free!
veeam.com
1. fopen(arduino)
We need to make a set of point for the x-axis of the plot
1. x=linspace(1,100);
To read data from the Arduino we use the fscanf function, these will be our y-axis values on the plot. The %d
means we are receiving a decimal number.
1. for i=1:length(x)
2. y(i)=fscanf(arduino,'%d');
3. end
Now we close the Arduino communication and plot the data.
1. fclose(arduino);
2. disp('making plot..')
3. plot(x,y);
Putting All The MATLAB Code Together
This is the exact code I used.
1. clear all
2. clc
3.
4. arduino=serial('COM4','BaudRate',9600);
5.
6. fopen(arduino);
7.
8. x=linspace(1,100);
9.
10. for i=1:length(x)
11. y(i)=fscanf(arduino,'%d');
12. end
13.
14. fclose(arduino);
15. disp('making plot..')
16. plot(x,y);
The Result
If everything worked you should get the following plot. Let us know how you are planning to use MATLAB
and Arduino in the comments below.
http://allaboutee.com/2011/07/04/how-to-send-data-from-the-arduino-to-matlab/ 2/5
٢٠١٨/١/٦ How To Send Data From The Arduino To MATLAB Communication
MATLAB generate plot
the plot generated by MATLAB using the variable i from the Arduino.
Veeam Explorer for SharePoint
Restore anything in minutes
Quick and easy granular eDiscovery
and recovery for SharePoint. Free!
veeam.com
Related posts:
How To Send Data From MATLAB To Your Arduino
ESP8266 Arduino LED Control (Control The Digital Pins Via WiFi, Send Data From Webpage
to Arduino)
ESP8266 Arduino Code and Schematic to Send AT Commands and Print Output
Arduino ADK Board: How Send Data From The Board To The Android Device
Arduino GPS Tutorial: Get Latitude and Longitude Coordinates
Previous post: Arduino Proximity Sensor With Ultrasonic Range Finder
Next post: How To Send Data From MATLAB To Your Arduino
Search:
To search, type and hit enter
AllAboutEE Facebook Page:
http://allaboutee.com/2011/07/04/how-to-send-data-from-the-arduino-to-matlab/ 3/5
٢٠١٨/١/٦ How To Send Data From The Arduino To MATLAB Communication
You like thisBe the first of your friends to like
this
All About EE
865 likes
Like PageLiked
Share
Categories
Circuits
ESP8266
LabVIEW
Microcontrollers
Arduino
ARM
AVR
PIC
PIC18 Explorer Board
Phone Development
Android
Uncategorized
http://allaboutee.com/2011/07/04/how-to-send-data-from-the-arduino-to-matlab/ 4/5
٢٠١٨/١/٦ How To Send Data From The Arduino To MATLAB Communication
Get smart with the Thesis WordPress Theme from DIYthemes.
WordPress Admin
http://allaboutee.com/2011/07/04/how-to-send-data-from-the-arduino-to-matlab/ 5/5