how to plot the real time data from arduino in matlab.

the sensor that i am using is giving some output.how can i plot that in matlab

2 commentaires

I have Arduino code, I want to draw it instantly in matlab. Can you help me
Could you please give more details on what is it that you are trying to achieve exactly , so that it will be easier for people to answer

Connectez-vous pour commenter.

 Réponse acceptée

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
clear
clc
%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' )
title(plotTitle,'FontSize',15);
xlabel(xLabel,'FontSize',15);
ylabel(yLabel,'FontSize',15);
legend(legend1,legend2,legend3)
axis([yMin yMax min max]);
grid(plotGrid);
tic
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!
set(plotGraph,'XData',time,'YData',data);
set(plotGraph1,'XData',time,'YData',data1);
set(plotGraph2,'XData',time,'YData',data2);
axis([0 time(count) min max]);
%Update the graph
pause(delay);
end
delete(a);
disp('Plot Closed and arduino object has been deleted');

17 commentaires

when i run the code it is showing like this.how to rectify this Serial Port Object : Serial-COM13
Communication Settings
Port: COM13
BaudRate: 9600
Terminator: 'LF'
Communication State
Status: closed
RecordStatus: off
Read/Write State
TransferStatus: idle
BytesAvailable: 0
ValuesReceived: 0
ValuesSent: 0
Nick
Nick le 13 Avr 2017
How are you connecting to Arduino? Arduino might not be COM13 on your computer it is on mine. Also I was using this code before the arduino support package was available so the way to connect to Arduino has been slightly modified.
arduino was on com13 in my computer.can u please tell as to how to rectify this error.
Nick
Nick le 15 Avr 2017
Which method are you using to connect the Arduino? Have you been able to connect to the Arduino before? That shows that the arduino is not connected as the port is close
this is my code:
clc; clear all; close all; delete(instrfind(('Port'),('com13'))); a=arduino('com13','uno'); ai_pin=0; tic;i=0; count=0; s=0; while toc<100 i=i+1; time(i)=toc;v(i)=a.analogRead(ai_pin);v2(i)=5; plot(time,v,'color','r');hold on; pause(0.30); if v(i)>5 s=1; else if s==1 s=0; count=count+1; end end end disp('no. of pulses=') disp(count)
Hello Nick! Your code works fine for me, but when I close the plot the script stays inside the while loop, there is no exit from it so I have to manually end it. Do you know why this could be caused?
I have Arduino code, I want to draw it instantly in matlab. Can you help me
Hello Nick,
Your code was really helpful. Thanks for the code. I used this and try to read 3 analog signals. However the waveforms do not match with the ones on the oscilliope (measured parallely). It would be great if you can have a look at my question here https://www.mathworks.com/matlabcentral/answers/591376-why-is-readvoltage-not-getting-me-the-correct-output?s_tid=srchtitle Thanks
First of all, thank you for the code.
But, how do we limit the elapse time?
Please explain all the lines following of and dat= a.analogread(0)*0.48875855327;
It reads a voltage from the indicated analog pin. The return value is an integer 0 to 1023 representing a proportion between none (0) and full (1023). You then have to scale that 10-bit value to an absolute voltage, which is done by multiplying by a scaling factor. In this case the scaling factor 0.488etc corresponds to 500/1023 -- so if the reading was the full 1023, the result of the multiplication would be 500.
Hi Nick, thank you for the code, it's help a lot! When I try your code, I get live plot like x axis keeps increasing, so that all previous plots are shown in the same page, which seems compressed a bit. Do you have any idea how to change the code to make the plot starts to scroll? In more details, the number of index in x axis is fixed, and the plot can become scrollable? Thank you for kind help!
can anyone tell me why the arduino object is deleted at the end?
Hi,
What is the sampling rate of data?
When you create an arduino object and ask to analog read, then sampling rate is not a relevant question. The while loop runs however quickly it runs, almost certainly not consistent. Each iteration, the MATLAB host sends an analog read request along with information about what to read. That goes out over USB whenever USB gets around to it, and the command is interpreted by the arduino, which figures out what is being requested and executes the corresponding code, and then the arduino creates a response and sends it back over USB. All of this is asynchronous and irregular timing, so sample rate is not a relevant concept, unless you want to measure min, max, average on a particular set up (knowing it will change if Windows decides it is time to fetch email or defragment the hard drive or run a virus scan.)
If you to know about sample rate, you need to write code for the arduino side that is dedicated to reading the device and reporting back.
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

Connectez-vous pour commenter.

Plus de réponses (3)

Nguyet Minh
Nguyet Minh le 25 Sep 2019

0 votes

Can I ask about the number "Data from Arduino". What is it means?
Thanks a lot
Data from the arduino.PNG
anil simsek
anil simsek le 6 Mai 2020

0 votes

Arduino kodum var, hemen matlab'da çizmek istiyorum. Bana yardımcı olabilir misiniz

Catégories

En savoir plus sur MATLAB Support Package for Arduino Hardware dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by