Plotting Data Continuously From Arduino Serial

조회 수: 57 (최근 30일)
Noa Reisner-Stehman
Noa Reisner-Stehman 2017년 4월 5일
편집: Technical 2023년 1월 9일
I am currently attempting to read in data from an alcohol sensor in Arduino. The code works with a for loop, with the data being stored as a vector.
I would like to plot this data against time, for an infinite amount of time. I have tried using an infinite while loop, but it failed to update the graph each time (no points show up).
The Code looks like this:
arduino= serial('/dev/cu.usbserial-DA01P0H8', 'BaudRate', 9600); fopen(arduino);
pause on;
x = 0; y = 1;
while 1 == 1
alcohol = fscanf(arduino, '%f');
time = x;
plot(x, alcohol);
pause(5);
x = x + 5;
y = y + 1;
end
Any help would be appreciated! Thanks :)
  댓글 수: 1
Technical
Technical 2023년 1월 9일
편집: Technical 2023년 1월 9일
Why it is coming like this?

댓글을 달려면 로그인하십시오.

답변 (2개)

Nick
Nick 2017년 4월 7일
편집: Nick 2017년 4월 7일
This is an old code I had before Arduino had a support package so I'm getting data from the Arduino a different way but this will plot until you stop it.
%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')
  댓글 수: 1
Jacob Schoeb
Jacob Schoeb 2018년 4월 6일
How do you get a.analogRead. It keeps having an error at that step

댓글을 달려면 로그인하십시오.


Tommy Fu
Tommy Fu 2019년 6월 10일
편집: Tommy Fu 2019년 6월 10일
Nick's program works. But when run the MATLAB program, it seems MATLAB will write something into the Arduino board to override the Arduino program in the board. I would like to consult that how I can solve this problem. Thank you!
  댓글 수: 1
Akash Kalghatgi
Akash Kalghatgi 2021년 2월 3일
For this, you have to update the following lines
device = serialport("com9", 115200)
data = readline(device) %if your device uses serial print
%OR
data = read(device,1,"double") %if your device uses serial write

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by