Azzera filtri
Azzera filtri

Reading Serial Data from arduino and plotting it continuously vs time

20 visualizzazioni (ultimi 30 giorni)
Right now I am using an arduino uno to read analog serial data from a GSR(skin conductance) sensor. I have a code in arduino IDE that alters the serial data, so I am hoping to take this data directly from the IDE and use it with matlba continously. Also, Arduino Uno has a circuit that automaticaly converts the voltage into a digital value from 0-1024, but when I do readVoltage() it will read the analog signal. I borrowed this code from some ones in the answers page, but every time I run the code I get an error:
"No appropriate method, property, or field 'analogRead' for class 'arduino'.
Error in ArduinoSerial (line 36)
dat = a.analogRead(0); %* 0.48875855327; %Data from the arduino"
Is the a.analogRead in the download support package or do i need to make my own?
clear
clc
%User Defined Properties
a = arduino('/dev/cu.usbmodem1411','uno'); % define the Arduino Communication port
plotTitle = 'Arduino Data Log'; % plot title
xLabel = 'Elapsed Time (s)'; % x-axis label
yLabel = 'Skin Conductance'; % y-axis label
legend1 = 'Measured Skin Conductance';
% legend2 = 'Temperature Sensor 2';
% legend3 = 'Temperature Sensor 3';
yMax = 1000; %y Maximum Value
yMin = 0; %y minimum Value
plotGrid = 'on'; % 'off' to turn off grid
min = 0; % set y-min
max = 1000; % 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)% 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 Commento
Lokeshwaran S
Lokeshwaran S il 6 Gen 2021
Thanks @Walter Roberson
The syntax is different from arduino to MATLAB.
Solution:
Take a look at the following example code to read the analog voltage value from the arduino and use according to your need.
a = arduino;
readVoltage(a,'A4')
So the change needed to be done is:
dat = readVoltage(a,'A0');
If you dont want to print the dat value in the above code which keeps scrolling at my screen, insert ; at the end of that line.

Accedi per commentare.

Risposte (1)

Walter Roberson
Walter Roberson il 9 Apr 2018
  2 Commenti
Jacob Schoeb
Jacob Schoeb il 9 Apr 2018
I download it and it is still popping up with the same error. I think it is only compatible for some older versions of Matlab. IS there any suggestions on solutions to this?

Accedi per commentare.

Categorie

Scopri di più su Instrument Control Toolbox in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by