
Graphs always help to visualize data and it becomes very easy to find trends and patterns by looking at them. There are many software available for plotting graphs based on input values. Today we will use MATLAB to plot graphs based on the temperature data from the LM35 sensor. This article will introduce the basic ideas on how to plot real-time graphs using MATLAB. This article also uses the Arduino Uno development board to get the temperature data from the LM35 temperature sensor.
Creating a MATLAB GUI for Plotting
First, we have to build a GUI (Graphical User Interface) for plotting graphs using the temperature data. To start the GUI, type the following command in the command window
Then a window pops up, select New Blank GUI in the window, as shown below,
Now we have to select a button, two axes, and a text box for the MATLAB graphical interface. The button will be used to start sensing the temperature, the two axes will be used to plot the graph, and the text box will be used to display the current value of the temperature.
To resize or change the shape of a button, axis or edit text button, just click on it and you will be able to drag the corners of the button. By double clicking on any of them, you will be able to change the color, string and label of that particular button. After customization, it will look like this
You can customize the buttons as per your choice. Now when you save it, a code is generated in the Editor window of MATLAB. To make Arduino perform any specific task related to your project, you always have to edit this generated code. So below we have edited the MATLAB code. You can learn more about the Command Window, Editor Window, etc. in the Getting Started with MATLAB tutorial.
MATLAB code for drawing the graph
The complete MATLAB code for making a thermometer using LM35 and Arduino development board is given at the end of this article. In addition, we also include the GUI file (.fig) and code file (.m) for download, you can customize the button or axis size as needed. We edited the generated code as described below.
Copy and paste the following code on line 74 to ensure that Arduino is communicating with MATLAB every time you run the m-file.
As you scroll down, you will see two functions created for Button and Edit Text in GUI and no function is created for Axes. Now write the code in Button (Start Button) function according to the task you want to perform.
In the function of the Start button, copy and paste the following code at the end of the function to start the temperature sensing. For continuous sensing, display, and graphing of temperature, we use a while loop. We provide a 1 second pause after each iteration, so the temperature value will be updated every second.
Now, let’s see how the code works. In the following three lines of code, we set the initial value of ‘x’ to zero, define ‘go’ as true to start the while loop, and declare ‘global a’ to be used by the Arduino in the calling function.
The following line of code is used to detect the voltage value of the analog pin A1 of Arduino which is connected to the “OUT” pin of the LM35 temperature sensor. The output will be a voltage value instead of an analog value.
Here we can convert the voltage value directly to temperature value (Celsius) by multiplying by 10
Now, to plot the graph, we use the ‘ plot(handles.axes1,x) ‘ function where axes1 is the reference or name of the Graph region. So if you want to plot multiple graphs then you just need to change the name of the axis, like to plot another graph you can write plot (handles.axes2,x) ‘
‘Grid on’ is used for grid view of the plot, and ‘xlabel’, ‘ylabel’ and ‘title’ are used to name the x-axis, y-axis and title.
‘ Drawnow ‘ is used to update the graphical representation in real time.
To display the temperature value in the edit text box every second using the below command,
Required Materials
● Laptop with MATLAB installed (preferred: R2016a or above)
● Arduino UNO development board
● LM35 – Temperature sensor
● Connecting cable
● Breadboard
Circuit Schematic
Plotting Graphics with MATLAB
After setting up the hardware according to the circuit diagram, just click the run button to run the edited code in the .m file
MATLAB may take a few seconds to respond, do not click any GUI buttons until MATLAB displays a busy message in the bottom left corner as shown below,
Once everything is ready, click the Start button and you will start getting temperature data in the graph area and in the edit text box. The value will automatically update every second. You can change the one second interval accordingly in the MATLAB code.
The output will be as shown below,
That’s how you can use MATLAB to plot a graph of any incoming value in Arduino.
Code
The following is the MATLAB code used in this article