how to plot the real time data from arduino in matlab.
290 views (last 30 days)
Show older comments
prajwal prabhu on 6 Apr 2017
Commented: Walter Roberson on 4 Jun 2022
the sensor that i am using is giving some output.how can i plot that in matlab
Nick on 7 Apr 2017
Hi this is some old code I have that will plot the Arduino's analogread output. It will read continuously until it is stopped. I should also say that when I used the Arduino, the support package was not available so I was grabbing my data from the Arduino a different way (analogRead) than you might depending how you are connected to the Arduino.
%This is a script that will plot Arduino analogRead values in real time
%Modified from http://billwaa.wordpress.com/2013/07/10/matlab-real-time-serial-data-logger/
%The code from that site takes data from Serial
%User Defined Properties
a = arduino('Com13') % define the Arduino Communication port
plotTitle = 'Arduino Data Log'; % plot title
xLabel = 'Elapsed Time (s)'; % x-axis label
yLabel = 'Temperature (C)'; % y-axis label
legend1 = 'Temperature Sensor 1'
legend2 = 'Temperature Sensor 2'
legend3 = 'Temperature Sensor 3'
yMax = 40 %y Maximum Value
yMin = 0 %y minimum Value
plotGrid = 'on'; % 'off' to turn off grid
min = 0; % set y-min
max = 40; % set y-max
delay = .01; % make sure sample faster than resolution
%Define Function Variables
time = 0;
data = 0;
data1 = 0;
data2 = 0;
count = 0;
%Set up Plot
plotGraph = plot(time,data,'-r' ) % every AnalogRead needs to be on its own Plotgraph
hold on %hold on makes sure all of the channels are plotted
plotGraph1 = plot(time,data1,'-b')
plotGraph2 = plot(time, data2,'-g' )
axis([yMin yMax min max]);
while ishandle(plotGraph) %Loop when Plot is Active will run until plot is closed
dat = a.analogRead(0)* 0.48875855327; %Data from the arduino
dat1 = a.analogRead(2)* 0.48875855327;
dat2 = a.analogRead(4)* 0.48875855327;
count = count + 1;
time(count) = toc;
data(count) = dat(1);
data1(count) = dat1(1)
data2(count) = dat2(1)
%This is the magic code
%Using plot will slow down the sampling time.. At times to over 20
%seconds per sample!
axis([0 time(count) min max]);
%Update the graph
disp('Plot Closed and arduino object has been deleted');
Walter Roberson on 4 Jun 2022
The sampling rate possible for temperature sensors depends on the sensor. I am finding rates of 1 sample/second or 1 sample per 2 seconds for some. I cannot seem to find statements on the sampling time of LM35 type sensors
More Answers (3)
Nguyet Minh on 25 Sep 2019
Can I ask about the number "Data from Arduino". What is it means?
Thanks a lot
Walter Roberson on 17 Nov 2021
See https://www.mathworks.com/matlabcentral/answers/333946-how-to-plot-the-real-time-data-from-arduino-in-matlab#comment_1314367 where I show how the meaning of that constant.
Tarun Shrivastava on 14 Oct 2019
these are the physical pins of mega 2560
anil simsek on 6 May 2020
Arduino kodum var, hemen matlab'da çizmek istiyorum. Bana yardımcı olabilir misiniz
Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!