How can I plot a continuous graphic of live recorded data with Arduino?

130 views (last 30 days)
Hello everybody, I am currently working on a project in which I have to record data (in this example, temperature) and I want to plot it live on a graphic. I am using an Arduino Uno, and after downloading and installing everything correctly I wrote this code:
i=0;
while(2>1) %For making the loop infinite
x=i
v1=readVoltage(a, 'A0'); %Analog input for the first NTC temperature sensor
v2=readVoltage(a, 'A2'); %Analog input for the second NTC temperature sensor
t1=temp(v1); %Here I obtain the temperature with
t2=temp(v2); %a function that I will post afterwards
y1=t1;
y2=t2;
pause(1.0); %line 11
i=i+1;
plot(x,y1, x,y2) %line 13
hold on
axis([0 inf 15 30]) %Chose those y-axis values because in normal conditions, temp is aprox 22ºC
end
The function for the temperature "temp()" is:
function [t]=temp(v)
rAux = 10000.0;
vcc = 5.0;
beta = 3977.0;
temp0 = 298.0;
r0 = 10000.0;
vm=(vcc / 5)*(v);
rntc = rAux / ((vcc/vm)-1);
R = rntc/r0;
tkelvin = beta/(log(R)+(beta/temp0));
t=tkelvin-273.15;
I know that the program is reading the temperature, but doesn't show anything in the graphic, it appears blank:
If I change the line 13 for:
plot(x,y1,'*', x,y2,'.')
It does write the data, but as every temperature is a different colour and the points aren't "connected", I think it is taking every point as a different graphic. Here is where my problem comes, because what I want to do is that all points are connected forming a line:
As you can see, it reads the variations of the temperature when I touch the sensor, so what I have is a problem when drawing, not when collecting data.
I also thought that it could be a problem because of the "pause" in line 11, but still doesn't draw anything in the graphic if I don't explicitly choose to write points or asterisks. What's more, in this case I cannot watch the graph live, just when I cancel the operation, the graphic appears with all the data collected from the start till the moment I cut it. So, if anybody could help me to plot it as a continuous line rather than a bunch os points or asterisks, I would be glad to hear your recommendations. Thanks in advance.

Accepted Answer

Ameer Hamza
Ameer Hamza on 22 Apr 2018
Edited: Ameer Hamza on 22 Apr 2018

You can refer to this answer related to the same problem. The way you are doing it is really inefficient and the plotting will become slow as the size of dataset increase because you are creating a new line handle everytime you call plot. You need line handle to update the plot, without calling plot() function again. For your specific case, you can do this as follow

% plot first point outside the while loop to obtain line handles.
i = 0;
v1=readVoltage(a, 'A0');  %Analog input for the first NTC temperature sensor
v2=readVoltage(a, 'A2');  %Analog input for the second NTC temperature sensor
t1=temp(v1);              %Here I obtain the temperature with 
t2=temp(v2);              %a function that I will post afterwards
y1=t1;
y2=t2;
ax = axes();
hold on;
line1 = line(i, y1);  %handle for line 1
line2 = line(i, y2);  %handle for line 2
axis([0 inf 15 30]) %Chose those y-axis values because in normal conditions, temp is aprox 22ºC
i = i+1;
while(1) %For making the loop infinite
  v1=readVoltage(a, 'A0');  %Analog input for the first NTC temperature sensor
  v2=readVoltage(a, 'A2');  %Analog input for the second NTC temperature sensor
  t1=temp(v1);              %Here I obtain the temperature with 
  t2=temp(v2);              %a function that I will post afterwards
  y1=t1;
  y2=t2;
  pause(1.0);        %line 11
  % now only update XData and YData property of line handle;
  line1.XData = [line1.XData i];
  line1.YData = [line1.YData y1];
  line2.XData = [line2.XData i];
  line2.YData = [line2.YData y2];
  i=i+1;
end
  2 Comments
Ameer Hamza
Ameer Hamza on 22 Apr 2018
Thanks for pointing out the mistakes. It wrote it directly in text editor since I don't have an Arduino to test. I have fixed the mistakes now.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!