In this post we are going to create a real time live plot from the Arduino ADC Readings into the MATLAB. We will open up the serial communication in MATLAB and the received signal will be plotted on the MATLAB in Real TIME.

Arduino ADC Reading

First of all, let’s read the ADC in Arduino and send the readings onto the serial. This way we can fetch these values later in the MATLAB. We initialize the Serial port with 9600 baudrate. Later in the loop() function we are simply printing the adc reading to the serial port and then take a 500 millisecond delay and repeat that process. We can also add some filtration if we need before passing the values to the MATLAB.

 
void setup(){
 Serial.begin(9600); 
}

void loop(){
  Serial.println(analogRead(A0));
  delay(500);
  
}

Code language: Arduino (arduino)

Read Arduino Data in MATLAB

Now it is time to read that Arduino data into the MATLAB. This is two step process. First we need to register a callback. Inside this callback we will record the data and will maintain a window size which we need to plot. Once we have our window we plot this at each iteration. To register this callback, all we have to do is to create a serial object and provide a proper callback name. Here is how we can register our serial callback.

global time;
global s;
clear all; close all; clc;

time=zeros(1,15);
fclose(instrfind);
s=serial('com14');
s.BytesAvailableFcnMode = 'terminator';
s.BytesAvailableFcn={@callbackSerial};

fopen(s);

Code language: PHP (php)

Here we are creating the global serial object. we are just calling it s. You can name it whatever you want. We are also creating a row vector for maintaining the time. This is also known as our window. Because we are using 15 size for the window, we have to maintain this to rotate the data if we overflow the window. We are also mentioning the terminator as BytesAvailableFcnMode which tells MATLAB to call the callback once the terminator is received in the serial buffer. By default the terminator is \r\n which is a line feed which is sent on Arduino side when we send any data in Serial.println() function.

Serial Callback to Plot Realtime

Here is how we structured our callback to handle the incoming data from the serial port. Remember that this callback would be called we receive the line break in the serial buffer. If you never sent the println() command from your Arduino, then it means your callback will never be called. Here is the complete callback which handle the incoming data maintain a window size and plot in real time in MATLAB.

function callbackSerial(ser,~)
    global time;
    val = fscanf(ser);
    numval = str2double(val);
    time(16) = numval;
    time(1)=[];
    disp(time);
    plot(time);
endCode language: Matlab (matlab)

To maintain a 15 size window we are inserting new variable into the 16 location of the time vector and removing the 1st item which result in 15 element size window with first item being lost. This will make an effect of real time data plotting which goes into the left size when new data is added into the buffer received from the serial port.

By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.