Plotting live data from Arduino in Matlab

15 views (last 30 days)
Hi all,
Apologies if this has already been covered - I couldn't find any information and I am new to Matlab and Arduino! I want to plot water level data obtained from my Arduino Uno plugged into a Milone eTape water depth sensor. I've got the code running successfully in a freeware software called SerialPlot but I want to plot this data into Matlab. The code I am using on the Arduino is below. How do I go about plotting the data in real time on Matlab?
Thanks so much in advance!
Dan
// the value of the 'other' resistor
#define SERIESRESISTOR 560
// What pin to connect the sensor to
#define SENSORPIN A0
void setup(void) {
Serial.begin(38400);
}
void loop(void) {
float Reading;
float level;
float Reading2;
float mapped;
Reading = analogRead(SENSORPIN);
// Serial.print("Analog reading ");
// Serial.println(Reading);
//convert the value to resistance
Reading2 = (1023 / Reading) - 1;
Reading2 = SERIESRESISTOR / Reading2;
//Serial.print("Sensor resistance ");
//Serial.println(Reading2);
mapped = map(Reading2, 658.89, 1695.43 , 150, 0);
// map (Reading2, Highest Depth Resistance, Lowest Depth Resistance, Depth Difference, Lowest Depth)
// Serial.print("");
Serial.println(mapped);
delay(10);
}

Answers (1)

Madhu Govindarajan
Madhu Govindarajan on 3 Feb 2016
have you tried animatedline function in MATLAB? I doubt you can do 100% real-time with Arduino but I think this is what you are looking for.
  3 Comments
Daniel Green
Daniel Green on 4 Feb 2016
Edited: Daniel Green on 5 Feb 2016
Hi Madhu,
Thanks for your reply. I've installed the MATLAB support package to bring data from the Arduino into MATLAB but was wondering what I would need to input into the MATLAB command window to get the live data to show in a graphical format with 'time (s)' along the x axis and 'Water depth (mm) ' from the Arduino on the y axis. I'm new to MATLAB so not familiar with what I would need to do to create a graph showing this data.
I believe I need to type something similar to this in the MATLAB command prompt although this doesn't export the series values obtained for Water Depth (mm):
%%Create serial object for Arduino
s = serial('COM9');
%%Connect the serial port to Arduino
s.InputBufferSize = 1; % read only one byte every time
try
fopen(s);
catch err
fclose(instrfind);
error('Make sure you select the correct COM Port where the Arduino is connected.');
end
%%Create a figure window to monitor the live data
Tmax = 1000; % Total time for data collection (s)
figure,
grid on,
xlabel ('Time (s)'), ylabel('Water depth (mm)'),
axis([0 Tmax+1 0 200]),
%%Read and plot the data from Arduino
Ts = 1; % Sampling time (s)
i = 0;
data = 0;
t = 0;
tic % Start timer
while toc <= Tmax
i = i + 1;
%%Read buffer data
data(i) = fread(s);
%%Read time stamp
% If reading faster than sampling rate, force sampling time.
% If reading slower than sampling rate, nothing can be done. Consider
% decreasing the set sampling time Ts
t(i) = toc;
if i > 1
T = toc - t(i-1);
while T < Ts
T = toc - t(i-1);
end
end
t(i) = toc;
%%Plot live data
if i > 1
line([t(i-1) t(i)],[data(i-1) data(i)])
drawnow
end
fclose(s);
Thanks so much in advance,
Dan
anil simsek
anil simsek on 6 May 2020
I have Arduino code, I want to draw it instantly in matlab. Can you help me

Sign in to comment.

Categories

Find more on 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!