How to plot the real time data from arduino?

I have an Arduino program loaded to the Arduino. I want to plot the real time data from arduino in the MATLAB too. My MATLAB program can get the data from the Arduino board and plot successfully. However, it seems the MATLAB will write something to the Arduino board to override the Arduino program. Is it true? If the answer is positive, may I have a help that how can I get the data from Arduino without affecting the Arduino program? (P.S. both Arduino program and MATLAB program will read the same pin such as A0). Many thanks!

 Akzeptierte Antwort

Arun Kumar
Arun Kumar am 12 Jun. 2019

1 Stimme

Hi Tommy,
Your observation is correct.
When you do a=arduino, an arduino server is built and is deployed to the hardware.
If you already have code running on arduino, I would suggest using serial read to read the data from serial port for plotting. Refer this page for details about reading data from com port.

4 Kommentare

Tommy Fu
Tommy Fu am 12 Jun. 2019
Many thanks Arun Kumar. My MATLAB program is used serial to read the data by 'readVoltage' command. Please see my code as below which modified from https://www.mathworks.com/matlabcentral/answers/333946-how-to-plot-the-real-time-data-from-arduino-in-matlab
Thus, how I can to use fscanf based on the given reference link to read the analog value for a specific Arduino pin such as pin A0, A1, etc?
%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('Com14','Nano3'); % define the Arduino Communication port
b = arduino('Com15','Nano3');
c = arduino('Com15','Nano3');
plotTitle = 'Arduino Data Log'; % plot title
xLabel = 'Elapsed Time (s)'; % x-axis label
yLabel = 'Power (W)'; % y-axis label
legend1 = 'Perturb & Observe';
legend2 = 'Fractional Open Circuit Voltage';
legend3 = 'Fractional short Circuit Current';
yMax = 20; %y Maximum Value
yMin = 0; %y minimum Value
plotGrid = 'on'; % 'off' to turn off grid
min = 0; % set y-min
max = 20; % 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 = readVoltage(a,'A0')* 0.029296875 * readVoltage(a,'A1')*0.026393581; %Data from the arduino
dat1 = readVoltage(b,'A0')* 0.029296875 * readVoltage(b,'A1')*0.026393581;
dat2 = readVoltage(c,'A0')* 0.029296875 * readVoltage(c,'A1')*0.026393581;
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');
Arun Kumar
Arun Kumar am 13 Jun. 2019
Hi,
I can see that in your code you are connecting to arduino object using a=arduino, which will program a arduino server code to the device. In which case, the existing code on arduino will be overwritten.
In order to read values from Analog pin, you can do either of these:
  1. Continue with the existing code which you are using. You can read analog values from arduino but the existing program will be overwritten.
  2. Write the analog read function using arduino IDE so that arduino sends the analog values over serial. And read those values using serial object. In this case you can have other algorithms running on your arduino.
  3. There is one more way of doing this which uses simulink. You can implement your existing algorithm in simulink and can use Analog read blocks along with serial transmit blocks to read and transmit analog values.
HTH
Thanks.
Tommy Fu
Tommy Fu am 13 Jun. 2019
Thank You! I tried a simple code as below. The program in Arduino can run properly right now. However, no data is obtained at Matlab. May I have a help on this?
// ---- Arduino Code ----
void setup()
{
Serial.begin(9600);
}
void read_loop()
{
if(Serial.available()>0)
{
Serial.println(sol_amps);
}
}
% ----- MATLAB Code -----
s=serial('COM14','BaudRate',9600);
fopen(s);
go = true;
while go
readData=fscanf(s);
fprintf(s,readData);
end
fclose(s);
delete(s);
anil simsek
anil simsek am 6 Mai 2020
I have Arduino code, I want to draw it instantly in matlab. Can you help me

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Sylvain
Sylvain am 17 Jan. 2020

0 Stimmen

With a bit of delay, the issue is in your Arduino code:
Serial.available will return the number of bytes you can read. So you are telling your arduino to scan arriving signals:
anil simsek
anil simsek am 24 Feb. 2020

0 Stimmen

Hi. I have an arduino code like this. this code prints the number of passes of this object on the screen when an object passes in front of the sensor.I also have to create an instant graphical simulation in Matlab that instantly shows the total number of passes of the object.how do i do this in matlab? can you help me?

1 Kommentar

anil simsek
anil simsek am 24 Feb. 2020
this is my arduino code;
//===============================
int ledpin=12;
bool turn=0;
int sayac=0;
void setup() {
Serial.begin(9600);
pinMode(2,INPUT);
pinMode(ledpin,OUTPUT);
attachInterrupt(0,kesme,RISING);
Serial.println("Started!!!");
}
void loop() {
digitalWrite(ledpin,LOW);
Serial.println(sayac);
turn =0;
}
void kesme() {
digitalWrite(ledpin,HIGH);
if(!turn)
sayac++;
while( digitalRead (2)==1){turn=1; }
Serial.println(sayac);
}
//===========================

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB Support Package for Arduino Hardware finden Sie in Hilfe-Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by