This tutorial teaches you how to plot analog signal in Matlab using Arduino in real time.

You can read analog signal into Matlab from Arduino connected analog sensor and this was shown in couple of tutorials here. If you very new to programming Arduino with Matlab then we suggest to read those simple example tutorials.

Here our aim is to plot the analog signal after we have read it into Matlab. It is quite simple. But before explaining how to do that, you might want to look at the graph of analog signal plotted in matlab so that you know what to expect.

This real time graph was obtained by changing the potentiometer values at the analog pin A2 of the Arduino. This is the same potentiometer connection that was used in the earlier tutorial Control of LED brightness using Arduino Matlab. So the above graph is the voltage sensed by Arduino over time. By changing the potentiometer value we can see the changes in the voltage on the graph in real time. You can also watch the video demonstration of this tutorial.

Although we used a simple potentiometer as our analog signal source, everything is the same for other analog sensors whether it is a temperature sensor or light detector or any others.

Now we tell you how to obtain the real time graph using matlab. You should connect your analog sensor which is a potentiometer in this case to one of the six analog pins on Arduino. Here we have used the analog pin A2. For those who want the schematic see below.

This schematic was drawn in Proteus for those who might wonder.

Now you have to connect the hardware. The one used here is shown below.

The red and black are the power and ground wires and the yellow is connected the the A2 pin.

Once your hardware is set up the next thing is the programming. The Matlab program code for reading and plotting analog signal in Matlab using Arduino in real time is below.

A bit of explanation of the program code: a = arduino(); creates an Arduino object which is mandatory. Then we have used the tic and toc feature of Matlab. Basically what they are is that they are functions that starts and stops an internal watchdog timer to record events in Matlab. When matlab runs the code and encounters tic it starts its internal timer. Then we are using the toc as variable in the while statement to execute the while code until toc is 299(<300). At each while iteration the toc is set to time t. This is for the time scale. Also at iteration, we store the read voltage in variable v. k is just the index for t and v(for voltage). After that we just invoke the figure command to plot the voltage vs time using the plot function. The axis limits are set for x-axis from 0 to inf and for voltage or the y-axis from 0 to 6, meaning from 0 to 6Volts. The pause is just a delay of 0.1 second.

Thats it!

Now connect the USB cable and run the code. The graph will automatically show up. It will show the current analog voltage reading. Then change the potentiometer value up and down. You will see corresponding voltage changes on the graph. In this way the above figure obtained. This is shown again because it nice.

So in this way you can plot your analog signal in Matlab. This is a great feature. For those who does not have matlab stay tuned and subscribe to our blog because next time around we will show you how you can do this with Python which is similar to Matlab and is open source. Meanwhile check out our other tutorials here. See for example Reading analog signal using Arduino Video demonstration.

Next see Video showing How to plot analog signal in Matlab using Arduino in real time.

You can read analog signal into Matlab from Arduino connected analog sensor and this was shown in couple of tutorials here. If you very new to programming Arduino with Matlab then we suggest to read those simple example tutorials.

Here our aim is to plot the analog signal after we have read it into Matlab. It is quite simple. But before explaining how to do that, you might want to look at the graph of analog signal plotted in matlab so that you know what to expect.

This real time graph was obtained by changing the potentiometer values at the analog pin A2 of the Arduino. This is the same potentiometer connection that was used in the earlier tutorial Control of LED brightness using Arduino Matlab. So the above graph is the voltage sensed by Arduino over time. By changing the potentiometer value we can see the changes in the voltage on the graph in real time. You can also watch the video demonstration of this tutorial.

Although we used a simple potentiometer as our analog signal source, everything is the same for other analog sensors whether it is a temperature sensor or light detector or any others.

Now we tell you how to obtain the real time graph using matlab. You should connect your analog sensor which is a potentiometer in this case to one of the six analog pins on Arduino. Here we have used the analog pin A2. For those who want the schematic see below.

This schematic was drawn in Proteus for those who might wonder.

Now you have to connect the hardware. The one used here is shown below.

The red and black are the power and ground wires and the yellow is connected the the A2 pin.

Once your hardware is set up the next thing is the programming. The Matlab program code for reading and plotting analog signal in Matlab using Arduino in real time is below.

clear all close all a = arduino(); tic; % starts a stopwatch timer to measure performance, % it internally records starting time % toc read elapsed time from stopwatch k = 0; while (toc < 300) k = k+1; t(k) = toc; v(k) = readVoltage(a,'A2'); figure(1) plot(t,v); axis([0,inf,0,6]); xlabel('time') ylabel('voltage') grid on pause(0.1); end

A bit of explanation of the program code: a = arduino(); creates an Arduino object which is mandatory. Then we have used the tic and toc feature of Matlab. Basically what they are is that they are functions that starts and stops an internal watchdog timer to record events in Matlab. When matlab runs the code and encounters tic it starts its internal timer. Then we are using the toc as variable in the while statement to execute the while code until toc is 299(<300). At each while iteration the toc is set to time t. This is for the time scale. Also at iteration, we store the read voltage in variable v. k is just the index for t and v(for voltage). After that we just invoke the figure command to plot the voltage vs time using the plot function. The axis limits are set for x-axis from 0 to inf and for voltage or the y-axis from 0 to 6, meaning from 0 to 6Volts. The pause is just a delay of 0.1 second.

Thats it!

Now connect the USB cable and run the code. The graph will automatically show up. It will show the current analog voltage reading. Then change the potentiometer value up and down. You will see corresponding voltage changes on the graph. In this way the above figure obtained. This is shown again because it nice.

So in this way you can plot your analog signal in Matlab. This is a great feature. For those who does not have matlab stay tuned and subscribe to our blog because next time around we will show you how you can do this with Python which is similar to Matlab and is open source. Meanwhile check out our other tutorials here. See for example Reading analog signal using Arduino Video demonstration.

Next see Video showing How to plot analog signal in Matlab using Arduino in real time.

## No Comment to " How to plot analog signal in Matlab using Arduino in real time "