banner



How To Update Variable Time Interval In Matlab

Graphs are always helpful to visualize the data and it becomes very piece of cake to notice trends and patterns by looking at them. In that location are many software bachelor to plot graphs based on the input values, today we volition use MATLAB to plot graph based on the temperature data from LM35 sensor. This tutorial will give yous a basic idea most how to plot real time graph using MATLAB. Arduino Uno is used here to get temperature data from LM35 temperature sensor.

Earlier proceeding farther, if you are new to MATLAB yous can cheque our previous MATLAB tutorials for ameliorate understating:

  • Getting started with MATLAB: A Quick Introduction
  • Interfacing Arduino with MATLAB - Blinking LED
  • Serial Communication between MATLAB and Arduino
  • DC Motor Command Using MATLAB and Arduino
  • Stepper Motor Command using MATLAB and Arduino

Creating MATLAB Graphical User Interface for Plotting Graph

First nosotros accept to build GUI (Graphical User Interface) for Plot a Graph with the temperature data. To launch the GUI, type the below control in the control window

          guide        

A popup window volition open, then select new blank GUI as shown in beneath prototype,

At present nosotros have to choose one push, two axes and one text box for MATLAB graphical interface. Push button volition exist used for start the temperature sensing, 2 axes for plotting the graph and Text box to bear witness the current value of temperature.

Designing GUI for Plot Graph using MATLAB

To resize or to modify the shape of the push button, Axes or edit text button, just click on it and you will exist able to elevate the corners of the button. By double-clicking on any of these y'all will exist able to alter the color, string and tag of that particular button. Afterwards customization it will look similar this

Editing GUI for Plot Graph using MATLAB

GUI for Plot Graph using MATLAB

Yous can customize the buttons as per your choice. At present when you lot save this, a lawmaking is generated in the Editor window of MATLAB. To make your Arduino to perform any item task related to your project, you always have to edit this generated code. Then below nosotros have edited the MATLAB code. You can acquire more about Control window, editor window etc in Getting started with MATLAB tutorial.

MATLAB Code for Plotting the Graph

Complete MATLAB lawmaking for Designing a Thermometer using LM35 and Arduino, is given at the end of this project. Further we are including the GUI file (.fig) and code file(.yard) hither for download, using which you lot tin customize the buttons or Axes size equally per your requirement. We have edited the generated code as explained below.

Copy and paste the below code on line no. 74 to brand sure that the Arduino is talking with MATLAB every time you lot run the m-file.

          clear all;          global a;          a =                    arduino          ();        

When y'all scroll downwards, you will see that at that place are two functions created for Pushbutton and Edit Text in the GUI, no office will exist created for Axes. Now write the code in the Pushbutton (kickoff push) function according to the job you desire to perform.

In Start push button'southward role, copy and paste the below code but earlier the ending of the function to beginning the temperature sensing. For continuously sensing, displaying and graphical plotting of the temperature, we are using while loop. Nosotros accept provided pause of 1 second after every iteration so temperature value will be updated every 2nd.

          x = 0;          get = true;          global a;          while go                              value = readVoltage(a,'A1');          temp = (value*100);          disp(temp);          x = [x temp];          plot(handles.axes1,x);          grid on;          xlabel('Time(seconds)')          ylabel('Temperature(°C)');          championship('Real-Time Temperature Graph');          drawnow          gear up(handles.edit1,'String',num2str(temp));          pause(i);          end        

Code for Plot Graph using MATLAB

At present, lets see how the lawmaking is working. In below 3 lines line we have set the initial value of 'x' to nil, defining 'go' as true to start the while loop and declared 'global a' for calling the Arduino in the function.

          10 = 0;          go = true;          global a;        

Below line of code is used to sense the voltage value from the analog pin A1 of the Arduino which is continued to the 'OUT' pivot of the LM35 Temperature Sensor. The output will exist the voltage value not the analog value.

          value = readVoltage(a,'A1');        

Here nosotros tin directly catechumen the voltage value into the temperature value (degree Celsius), by multiplying it by 10

          temp = (value*100);        

At present, to plot the graph 'plot(handles.axes1,ten)' function is used, where axes1 is the reference or proper noun of the Graph area. So if you lot are plotting more than one graph then y'all just take to alter the proper noun of the axes, similar if you lot plot another graph you can write plot(handles.axes2,x)'

'Grid on' is used to On the Grid view of the graph, 'xlabel', 'ylabel' and 'championship' are used for naming the x-axis, y-centrality and championship.

          plot(handles.axes1,ten);          grid on;          xlabel          ('Time(seconds)')          ylabel          ('Temperature(°C)');          title('Real-Time Temperature Graph');        

'Drawnow' is used to update the graphical representation in real-fourth dimension.

                      drawnow                  

To display the value of the temperature in the edit text box at every 2nd below command is used,

          fix(handles.edit1,'String',num2str(temp));        

Textile Required

  • MATLAB installed Laptop (Preference: R2016a or above versions)
  • Arduino UNO
  • LM35 – Temperature Sensor
  • Connecting Wires
  • Breadboard

Circuit Diagram

Circuit Diagram for Plot Graph using MATLAB

Plot the Graph with MATLAB

Later on setting the hardware according to excursion diagram, merely click on the run button to run the edited code in .chiliad file

MATLAB may take few seconds to answer, do not click on any GUI buttons until MATLAB is showing decorated message in the lower left corner as shown beneath,

When everything is fix, click on the showtime button and y'all will outset getting the temperature data on the Graphical Surface area and in the Edit Text box. The value volition automatically update in every one second. This interval of ane second yous can change in the MATLAB code accordingly.

The output volition look like the image shown below,

Circuit Hardware for Plot Graph using MATLAB

This is how you tin plot the graph for any incoming value from the Arduino using MATLAB.

Check the demonstration video below for proper agreement.

Code

MATLAB Code:

part varargout = Temperature(varargin)

gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
'gui_Singleton',  gui_Singleton, ...
'gui_OpeningFcn', @Temperature_OpeningFcn, ...
'gui_OutputFcn',  @Temperature_OutputFcn, ...
'gui_LayoutFcn',  [] , ...
'gui_Callback',   []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{ane});
end

if nargout
[varargout{ane:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
cease

function Temperature_OpeningFcn(hObject, eventdata, handles, varargin)

handles.output = hObject;

guidata(hObject, handles);

part varargout = Temperature_OutputFcn(hObject, eventdata, handles)

varargout{ane} = handles.output;
clear all;
global a;
a = arduino;

part start_Callback(hObject, eventdata, handles)

x = 0;
go = truthful;
global a;
while become

value = readVoltage(a,'A1');
temp = (value*100);
disp(temp);
10 = [ten temp];
plot(handles.axes1,x);
grid on;
xlabel('Fourth dimension(seconds)')
ylabel('Temperature(°C)');
championship('Real-Time Temperature Graph');
drawnow
set(handles.edit1,'String',num2str(temp));
pause(ane);
end

part edit1_Callback(hObject, eventdata, handles)

function edit1_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(become(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

Source: https://circuitdigest.com/microcontroller-projects/how-to-plot-real-time-temperature-graph-using-matlab

Posted by: woodaboody80.blogspot.com

0 Response to "How To Update Variable Time Interval In Matlab"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel