How to plot real time data from arduino in matlab

 Many times we want to use Matlab to plot real time data from Arduino because Matlab has rich built in function for mathematical calculation and plotting graphs. We can plot real time data from Arduino in Matlab in many different ways. In this tutorial we will show one way how to plot real time data from Arduino in Matlab. Here we will display the analog sampled value in Matlab using the arduino object in matlab and use the datetime built in matlab function to display the analog signal acquisition time in seconds. That way we can actually measure time duration of the signal. For simplicity we will just use Potentiometer connected to the analog pin of Arduino Nano.

real time data from arduino in matlab

Before you proceed you need to install MATLAB Support Package for Arduino Hardware. The Programming Arduino using Matlab/Simulink Setup tutorial shows how to setup MATLAB Support Package for Arduino Hardware step by step.


Hardware Setup

 To stimulate analog signal or sensor signal we will use a 100KOhm potentiometer and connect the middle pin of it to the analog pin A1 of the Arduino Nano. You can use 10Kohm POT also and you can use any other Arduino board but in the Matlab code you have to specify the board.

The following picture shows the schematic of connecting Potentiometer to the Arduino Nano.

POT with arduino nano schematic diagram

In the above wiring diagram, the middle pin of the POT is connected to the A1 pin of Arduino Nano. One end of the POT is connected to the ground(black wire) and the other end is connected to the +5V pin of the Arduino(the red wire).

The following shows the Arduino Nano connected to 100KOhm potentiometer on a breadboard.

POT with arduino nano

Matlab Code

The following is the Matlab code that reads in the analog voltage using the serial port and display the voltage value on a time scrolling graph.


%Press ctrl+C to exit"

% create instance of arduino object
a = arduino('COM33','Nano');

%set-up graph
h = animatedline;
% set axis using get current axis(gca)
ax = gca;
% set x-axis label
% set x-axis label
% set title for graph
title('Potentiometer Voltage Graph');
% set y-axis limit from 0 to 5.2V
ax.YLim = [0 5.2];
% turn on grid
grid on;

% set start time
startTime = datetime('now');

while 1
    % Read current voltage value
    v = readVoltage(a,'A1');   
    % Get current time
    t =  datetime('now') - startTime;
    % Add points to animation
    % Update X-axis limit
    ax.XLim = datenum([t-seconds(10) t]);
    drawnow limitrate;

    %in case of error or arduino gets disconnected

To create an instance of arduino object we have provide the com port to which the arduino is connected to and the type of Arduino board used. This might be different for you, so you have check and specify the correct com port and specify the appropriate arduino board.

Then in the while loop we have used the following statement to read the voltage value coming into matlab from arduino by specifying the arduino instance a and the analog pin A1 and store in variable v.

v = readVoltage(a,'A1');

 To create time scrolling and real time display on x-axis we have made use of the animatedline function in matlab and use the datetime function to get the current time and the elapsed time. Before we enter the forever while loop we first get the start time using the datetime function with 'now' as argument and store in the variable startTime.

startTime = datetime('now');

 Within the while loop we get the data t for x-axis using the following line,

t =  datetime('now') - startTime;

Then for each data input coming from Arduino serial port we add the value of the serial port data and the current time into the animated line axis using the addpoints() function,


We update the x-axis limit to display the values of serial data from current time to next 10 secs using the following line,

ax.XLim = datenum([t-seconds(10) t]);

The datetick() function with keeplimits argument for x-axis is required to keep the x-axis to the specified limits.


The draw function is used with limitrate to draw the data points on the animated graph as fast as possible.

drawnow limitrate;

Video Demonstration

The following video demonstrates how to read and plot real time data from Arduino in Matlab.

Thus this tutorial illustrated how one can plot real time data using matlab and arduino. This can be used to read sensor data from arduino to matlab by replacing the potentiometer used in this tutorial.

Other alternative methods are as follows:

- How to use Matlab Simulink as Oscilloscope

- How to display Sensor data in real time on Web

Real-Time Serial Graph with P5.js

- Arduino Simulink as Oscilloscope to Capture Square Wave

- DIY Oscilloscope using Arduino and Matlab Simulink

Post a Comment

Previous Post Next Post