How to plot live data from Arduino UNO to App designer.
Show older comments
Hi everyone,
I'm using an arduino UNO and MATLAB to plot sine wave points using serial communciation. My live data plotting in MATLAB script works perfectly and is shown below. However, I'm looking to use app designer to show the live data logging on the axes and where the app would only run when the 'Start' button is selected. I'm not too sure where to start, but I've tried to implment my matlab script code into app designer but have had no luck.
clear all;
delete(instrfindall);
clear;
close all;
clc;
serialPort = 'COM3';
plotTitle = 'Sine Wave';
xLabel = 'Time (s)';
yLabel = 'Data';
plotGrid = 'on';
min = -1.5;
max = 1.5;
delay = .01;
%Define Function Variables
time = 0;
data = 0;
count = 0;
%Set up Plot
plotGraph = plot(time,data,'-r');
title(plotTitle,'FontSize',18);
xlabel(xLabel,'FontSize',15);
ylabel(yLabel,'FontSize',15);
axis([0 10 min max]);
grid(plotGrid);
%Open Serial COM Port
s= serial(serialPort);
disp('Close Plot to clear data');
fopen(s);
% start stopwatch timer
tic
%Loop when Plot is Active
while time <=50
%Read Data from Serial as Float
dat = fscanf(s,'%f');
if(~isempty(dat) && isfloat(dat)) %Make sure Data Type is Correct
count = count + 1;
time(count) = toc; %stop the stopwatch and extract time
data(count) = dat(1); %Extract 1st Data Element
set(plotGraph,'XData',time,'YData',data);
axis([0 time(count) min max]);
end
%Allow MATLAB to Update Plot
pause(delay);
end
%Close Serial COM Port and Delete useless Variables
fclose(s);
clear count dat delay max min plotGraph plotGrid plotTitle s ...
serialPort xLabel yLabel;
Accepted Answer
More Answers (1)
Fazli Wadood
on 22 Mar 2023
To plot Analoge voltage using app UIaxis below code is working copy code and paste in push button callblock
clc
global b
b = arduino;
x1=0;
global go
go=true;
while go
tempA1 = readVoltage(b, 'A0');
tempA2 = 32+(9/5)*(tempA1*100);
x1=[x1 tempA2];
plot(app.UIAxes,x1);
drawnow
pause(1);
end
Categories
Find more on Develop Apps Using App Designer 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!