Here we illustrate Real-time data acquisition from arduino to matlab. For the purpose of demonstration we will acquisition varying analog signal due to LDR(photoresistor) connected to arduino analog pin A0. The analog converted data is transferred to the PC and Matlab will read those data and display the varying voltage. First we show the circuit schematic, then the Matlab Program Code and finally the demonstration via Video.
Circuit schematic for real-time data acquisition from arduino to matlab
Following is circuit schematic showing you how you should connect the LDR to Arduino. The LDR or photoresistor is connected to the analog pin A0 of the Arduino. The tutorial Connect and Read Photoresistor with Arduino explains in more details if you have any problem with this step.
Matlab Program for real-time data acquisition from arduino
The program code for real time data acquisition from arduino to matlab is below. The program code needs to be run after you have connected Arduino to your PC using USB cable.
a = arduino();
tmax = 50;
title('LDR Analog Voltage Real Time Data Graph')
xlabel ('Time (s)'), ylabel('Voltage');
axis([0 tmax+1 -0.5 5.5]);
k = 0;
v = 0;
t = 0;
while toc<= tmax
k = k + 1;
v(k) = readVoltage(a,'A0');
t(k) = toc;
if k > 1
line([t(k-1) t(k)],[v(k-1) v(k)]);
In the program code, we have first defined an Arduino object using the statement a = arduino(). The tmax refers to how long we want to acquisition the data. Here it is set to 50 seconds but you can change it. Following that we have set up our graph, setting the x and y axis limits, titles etc. Then we have initialized some required values- k, v and t. k is the just the index number for matrix to store the received data and for the corresponding time. v and t are variables for voltage and time which are both set to 0 as starting value. Then the main idea to plot the real time signal is to use the tic and toc feature of matlab. They are used to set the internal timer in matlab. tic is used to start the timer and toc is used to refer to the time elapsed after tic has been set. toc is used to set the time instances values as you can see in the program. Using the matlab function readVoltage() we read in the analog signal values(digital values) sent over by Arduino to Matlab. This function readVoltage() is the counter part of analogRead() function in Arduino IDE. For analogRead() and how it works you can see the tutorial how to read analog signal with Arduino.
If you run this code you will get real time graph similar to the one below.
Video demonstration of real-time data acquisition from arduino to matlab
Now we demonstrate via Video how to acquisition real-time data using arduino and display in matlab. Watch the following video.
So in this way you can acquire in real time analog signal or digital data and plot it in Matlab using Ardunio as instrumentation device. Here we collected data from LDR or photoresistor. We can also collect audio signal or simple potentiometer. See Real Time Analog Voltage in Matlab using Arduino for how to do this for audio signal and potentiometer.
I hope you liked it and you did, share it. Also don’t forget to subscribe.