I have code that fetches analog input values from terminals A0, A1 and A2 of an Arduino Mega 2560. When I plot the data, it does not give the desired waveform.
The desired waveform is also measured with a the digital storage oscilloscope.
I was thinking to make use of an Arduino Mega 2560 as an oscilloscope that can have 16 channels, because the DSO has only 4 and for a project, I urgently need 16 channels. Would be great if the community people suggest something.
Below is the desired data from the oscilloscope that is measured in parallel to avoid any confusion about the circuit.
%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 all
close all
clc
%User Defined Properties
a = arduino('Com5') % define the Arduino Communication port
arduino=serial('COM5','BaudRate',9600);
s1 = serial('COM5'); % Define here YOUR serial port
s1.BaudRate=9600;
plotTitle = 'Arduino Data Log'; % plot title
xLabel = 'Time (s)'; % x-axis label
yLabel = 'Voltage (V)'; % y-axis label
legend1 = 'Voltage 1'
legend2 = 'Voltage 2'
legend3 = 'Voltage 3'
yMax = 5 %y Maximum Value
yMin = 0 %y minimum Value
plotGrid = 'on'; % 'off' to turn off grid
min = 0; % set y-min
max = 5; % 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.48875855327; %Data from the arduino
% dat1 = readVoltage(a,'A1')* 0.48875855327;
% dat2 = readVoltage(a,'A2')* 0.48875855327;
dat = readVoltage(a,'A0')* 1; %Data from the arduino
dat1 = readVoltage(a,'A1')* 1;
dat2 = readVoltage(a,'A2')*1;
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');`
readVoltage()
function come from? It isn't anywhere in the link, where you modified the code from. From your images it is obvious, that you are sampling way too slow for seeing the wave form.