Electronic projects

[Electronic projects][bleft]

Arduino Projects

[Arduino Projects][bleft]

Metal detectors

[Metal detectors][bleft]

Home projects

[Home projects][bleft]

MATLAB data recording, analysis and visualization from Arduino and DHT11 sensor

 


Graphical representations always help to visualize data, and by looking at them it becomes very easy to find trends and patterns. There are many software that can be used to draw graphics based on input values, but in embedded systems, MATLAB is one of the most popular software. It can not only display the results in a graphical format, but also can be easily integrated with hardware and micro-controllers.


In this article, we will learn how to use MATLAB to record and display data in graphical format. Here, we use Arduino and MATLAB to record the real-time temperature and humidity data of DHT11.


Required components

● Notebook computer with MATLAB installed

● Arduino UNO development board

● DHT11 temperature and humidity sensor


Circuit schematic

 


Arduino code for DHT11 connected with MATLAB

After connecting the DHT11 sensor to the Arduino, use the Arduino IDE to upload the code to the Arduino. At the end of the article, the complete Arduino code is given to record and visualize temperature and humidity data into MATLAB. Let us understand how the code works.


First include the library for DHT11 sensor, namely'DHT.h'.

  1. #include <DHT.h>  

Then define the DHT11 data pin connected to the Arduino. In this example, pin 4 is used.

  1. #define DHTPIN 4

Initialize the serial port and DHT11 sensor in the void setup() function.

  1. void setup() {
  2.   Serial.begin(9600);
  3.   delay(2000);
  4.   dht.begin(); // initialise DHT11 sensor
  5. }

In the void loop() function, set the temperature and humidity values ​​to float type. Use the commands float temp = dht.readTemperature() and float humi = dht.readHumidity() to read temperature and humidity data from the Arduino. Then print these values ​​on the serial monitor so that MATLAB can continuously read the values.

  1. void loop() {                                            
  2.   float temp = dht.readTemperature(); //read temperature data
  3. float humi = dht.readHumidity(); //read temperature data
  4. Serial.print(temp);
  5.   Serial.print(humi);
  6.   delay(2000);
  7. }

MATLAB code for recording and plotting data

Open MATLAB in the system, and then start writing code in the editor window. You can open the editor window by clicking "New Script" in MATLAB, as shown in the figure below.

The complete MATLAB code for DHT11 sensor data recording is given at the end of this article.


First define a serial communication variable from MATLAB to Arduino. COM18 is the port where the Arduino is connected, you can change it accordingly.

  1. s = serial('COM18');

In the code below, fopen() is used for serial communication between Arduino and MATLAB. Then we save the serial data into a variable named'out'. The out variable is a nine-digit string, where the first four digits store temperature data, and the remaining digits store humidity data. Therefore, by using Temp(i)=str2num(out(1:4)) and Humi(i)=str2num(out(5:9)) , we read the temperature and humidity data respectively.

  1. fopen(s)
  2. out = fscanf(s)
  3. Temp(i)=str2num(out(1:4));
  4. Humi(i)=str2num(out(5:9));

Now, paste the complete code into the editor window. Click the "Run" button to process the code, as shown in the figure below.


Wait until MATLAB shows busy in the lower left corner of the screen, as shown in the figure below. This indicates that MATLAB is processing the code.

 

If there are no errors when the code is executed, the real-time graphics window will appear on the screen as shown below. Here, the data will be updated every two seconds because it takes two seconds for the DHT11 sensor to send data to the serial port.

 

To check the real-time recorded values ​​of temperature and humidity, double-click the corresponding variable in the working area window, as shown in the figure below.

 

 

A dialog box will appear containing all the saved values ​​in the specific variable.

 


The above is how you use MATLAB to record, visualize and draw graphs based on any data. If you encounter any problems, please reply below this post.


Code

The complete code used in this article is as follows (including Arduino and Matlab code) 

Arduino Code


#include <DHT.h>                                                            
#define DHTPIN 4                                                           
#define DHTTYPE DHT11                                               
DHT dht(DHTPIN, DHTTYPE);
void setup() {
  Serial.begin(9600);
  delay(2000);
  dht.begin();   // initialise DHT11 sensor
}
void loop() {                                    
  float temp = dht.readTemperature();   //read temperature data
float humi = dht.readHumidity();   //read temperature data
 Serial.print(temp);
  Serial.print(humi);
  delay(2000);
}


MATLAB Code
s = serial('COM18');
time=100;
i=1;
while(i<time)
fopen(s)
fprintf(s, 'Your serial data goes here')
out = fscanf(s)
Temp(i)=str2num(out(1:4));
 subplot(211);
 plot(Temp,'g');
 axis([0,time,20,50]);
title('Parameter: DHT11 Temperature');
xlabel('---> time in x*0.02 sec');
ylabel('---> Temperature');
grid
Humi(i)=str2num(out(5:9));
 subplot(212);
 plot(Humi,'m');
axis([0,time,25,100]);
title('Parameter: DHT11 Humidity');
xlabel('---> time in x*0.02 sec');
ylabel('---> % of Humidity ');
grid
fclose(s)
i=i+1;
drawnow;
end
delete(s)
clear s

 

 

 

Arduino Projects

[Arduino Projects][twocolumns]