Plotting force sensor data from Arduino in matlab

14 views (last 30 days)
I'm trying to plot live data from a force sensor resistor in matlab using an arduino. I'm using the code I found on mathworks, but I cannot figure out how to adapt it correctly for it to read voltage data.
I need the command a=arduino; b = readVoltage(a,'A0');
But than I'm struggling with the fread function, since the fread function should than read the voltage, but this gives me the error 'In an assignment A(:) = B, the number of elements in A and B must be the same'
when I put data(i) = fread(b);
s = serial('COM3');
s.InputBufferSize = 1; % read only one byte every time
catch err
error('Make sure you select the correct COM Port where the Arduino is connected.');
%% Create a figure window to monitor the live data
Tmax = 1000; % Total time for data collection (s) figure,
grid on,
xlabel ('Time (s)'), ylabel('Voltage'),
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);
t(i) = toc;
%%Plot live data
if i > 1
line([t(i-1) t(i)],[data(i-1) data(i)])
I hope my question is clear. Thanks in advance

Answers (0)


Find more on MATLAB Support Package for 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!

Translated by