Arduino Realtime graph plot using Matlab

Arduino Real-Time Plotting with MATLAB

Introduction

Analytics is one of the best tools which help you gain information about what is happening, and help you to see the trends in the data. Companies like Facebook, Google, Uber, Amazon, and many others use the help of analytics to perform certain tasks, such as ads. Arduino Real-Time Plotting with MatLab project will help you to analyze the data. This project will help you connect arduino and matlab.

This project will help you do analysis on your sensor data in real-time. In this project, we are going to take sensor values and send it on MATLAB script running on our laptop serially and will plot those serial values in real time.

Matlab array starts from 1.
Arduino Real-Time Plotting with MATLAB
Matlab array starts from 1

MATLAB has been used for multiple purpose and in this tutorial we are going to use it for real time data plotting.

Plotting with MATLAB is fun

We will read the analog values of potentiometer and our favorite development board Arduino will help us do that and the value will be sent to MATLAB serially.

Arduino is our favorite board.
Arduino Real-Time Plotting with MATLAB
Arduino is our favorite board.

Check out how to do the same task with Python

Set Up MATLAB Arduino Hardware Support:

MATLAB being the matrix lab and the tool for mathematical processing have 100s of tools, some of them comes pre-installed and you will have to install some. So, our first task will be to install the MATLAB Arduino support and you can do that by following the method discussed below,

  • First, start MATLAB and click the Add-Ons drop down menu. In the drop-down menu click Get Hardware Support Packages. It will start the package installer window.
  • Select Install from internet and then Next.
  • In the next window you will see all the available packages for MATLAB and Simulink. Select the Arduino package, then check all the packages displayed and click Next to continue installation.
  • Next, the installer will ask you to log in to your Math Works account. If you don’t have an account, you can create one during installation. Accept the license agreement on the next screen and continue to download the packages. Now you have to wait for MATLAB to download and install all the required packages.

Package Testing:

Once the packages are installed, connect your Arduino board to your PC and type the following command in MATLAB command window,

>> a = arduino()

if you have more than one Arduino connected to your PC, you have to specify the board type you will be communicating with:

>> a = arduino(‘com3’, ‘uno’)

MATLAB will then attempt to communicate with your board. If successful, MATLAB will display the properties of the Arduino board connected to your PC.

Make Circuit Diagram and Write MATLAB Script:

Then all you have to do is to make circuit diagram which is quite simple and is attached and then write MATLAB code (attached) and run the program.

Circuit diagram. Arduino with potentiometer.
Circuit diagram. Arduino with a potentiometer.

clc

global a;

if ~isempty(instafind)
fclose(instrfind);
delete(instrfind);
end

% The above if-condition will help you find the COM port.

a= arduino();
%make "a" object of arduino

interv = 200;
%defining time

init_time = 1;
%initial time

x=0;

while (init_time<interv)
  b=readVoltage(a,'A0');
%reading voltages from pin A0 , reads voltages not adc value ,
%voltages = (ADC*5)/1024
  x=[x,b];
  plot(x)
  grid ON
  init_time=init_time+1;
  drawnow
end

%the above loop will run for 200 values, if you want to run it for more time change the interv value  
  

Video:

Don’t Forget to Subscribe and Like us:

if you need any help in this project you can contact us.

Please Subscribe us on youtube: www.youtube.com/c/highvoltages

Facebook: www.facebook.com/highvoltagestech

Instagram: www.instagram.com/highvoltagestech

1 thought on “Arduino Real-Time Plotting with MATLAB”

  1. Pingback: How to plot real-time data in MATLAB over MQTT - High Voltages

Leave a Comment

Your email address will not be published. Required fields are marked *