ArduinoMatlab

Real-time temperature graph using MATLAB

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.

Real-time temperature graph using MATLAB

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

  1. guide

Copy code

Then a window pops up, select New Blank GUI in the window, as shown below,

Real-time temperature graph using MATLAB

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.

Real-time temperature graph using MATLAB

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

Real-time temperature graph using MATLAB

Real-time temperature graph using MATLAB

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.

  1. clear all;
  2. global a;
  3. a = arduino();

Copy code

Real-time temperature graph using MATLAB

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.

  1. x = 0;
  2. go = true;
  3. global a;
  4. while go              
  5. value = readVoltage(a,’A1′);
  6. temp = (value*100);
  7. disp(temp);
  8. x = [x temp];
  9. plot(handles.axes1,x);
  10. grid on;
  11. xlabel(‘Time(seconds)’)
  12. ylabel(‘Temperature(°C)’);
  13. title(‘Real-Time Temperature Graph’);
  14. drawnow
  15. set(handles.edit1,’String’,num2str(temp));
  16. pause(1);
  17. end

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.

  1. x = 0;
  2. go = true;
  3. global a;

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.

  1. value = readVoltage(a,’A1′);

Copy code

Here we can convert the voltage value directly to temperature value (Celsius) by multiplying by 10

  1. temp = (value*100);

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.

  1. plot(handles.axes1,x);
  2. grid on;
  3. xlabel(‘Time(seconds)’)
  4. ylabel(‘Temperature(°C)’);
  5. title(‘Real-Time Temperature Graph’);

‘ Drawnow ‘ is used to update the graphical representation in real time.

  1. drawnow

To display the temperature value in the edit text box every second using the below command,

  1. set(handles.edit1,’String’,num2str(temp));

Required Materials

● Laptop with MATLAB installed (preferred: R2016a or above)

● Arduino UNO development board

● LM35 – Temperature sensor

● Connecting cable

● Breadboard

Circuit Schematic

Real-time temperature graph using MATLAB

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

Real-time temperature graph using MATLAB

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,

Real-time temperature graph using MATLAB

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

  1. MATLAB Code:
  2. function varargout = Temperature(varargin)
  3. gui_Singleton = 1;
  4. gui_State = struct(‘gui_Name’, mfilename, …
  5.                    ‘gui_Singleton’, gui_Singleton, …
  6.                    ‘gui_OpeningFcn’, @Temperature_OpeningFcn, …
  7.                    ‘gui_OutputFcn’, @Temperature_OutputFcn, …
  8.                    ‘gui_LayoutFcn’, [] , …
  9.                    ‘gui_Callback’, []);
  10. if nargin && ischar(varargin{1})
  11.     gui_State.gui_Callback = str2func(varargin{1});
  12. end
  13. if nargout
  14.     [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
  15. else
  16.     gui_mainfcn(gui_State, varargin{:});
  17. end
  18. function Temperature_OpeningFcn(hObject, eventdata, handles, varargin)
  19. handles.output = hObject;
  20. guidata(hObject, handles);
  21. function varargout = Temperature_OutputFcn(hObject, eventdata, handles)
  22. varargout{1} = handles.output;
  23. clear all;
  24. global a;
  25. a = arduino;
  26. function start_Callback(hObject, eventdata, handles)
  27. x = 0;
  28. go = true;
  29. global a;
  30. while go
  31.                   
  32. value = readVoltage(a,’A1′);
  33. temp = (value*100);
  34. disp(temp);
  35. x = [x temp];
  36. plot(handles.axes1,x);
  37. grid on;
  38. xlabel(‘Time(seconds)’)
  39. ylabel(‘Temperature(°C)’);
  40. title(‘Real-Time Temperature Graph’);
  41. drawnow
  42. set(handles.edit1,’String’,num2str(temp));
  43. pause(1);
  44. end
  45. function edit1_Callback(hObject, eventdata, handles)
  46. function edit1_CreateFcn(hObject, eventdata, handles)
  47. if ispc && isequal(get(hObject,’BackgroundColor’), get(0,’defaultUicontrolBackgroundColor’))
  48.     set(hObject,’BackgroundColor’,’white’);
  49. end
Back to top button

Adblock Detected

Please consider supporting us by disabling your ad blocker