DTH Sensor Programming


DHT11 and DHT22 sensors are sensors that can be attached to a microcontroller on a breadboard and can measure the humidity and temperature of a place. The differences between these sensors are that DHT22 is more expensive, has better temperature reading ranges, and is more accurate. These sensors can be connected to microcontrollers such as ESP32 or Arduino and can be used by programs to read the values. In this article, using two different examples, the use of DHT11 and DHT22 Sensors is demonstrated using ESP32. The circuit is made using ESP32 microcontroller and DHT sensors in actual and on wokwi simulator and the programs to measure the temperature or humidity are written using Arduino software.

Let’s explore the articles with a few examples.

Example 1 − Measuring the temperature and humidity of a place using DHT11 sensor

Example 2 − Showing the readings from DHT22 sensor using Wokwi Simulator and LCD.

Making the connections of DHT11 with ESP32:

Make a circuit by connecting the pins as given below −

Fig 1: Showing the pin coonections of DHT11 with ESP32

Example 1: Measuring the temperature and humidity of a place using DHT11 sensor

The DHT11 Sensor is used to measure the temperature and humidity. The C language program here uses "DHT.h for communications between the ESP32 and the sensor DHT11. The result can be seen using the Serial Monitor.

Circuit Design Steps and Coding

Step 1 − First the microcontroller ESP32 is placed on the breadboard.

Step 2 − Next the DHT11 sensor is inserted into the breadboard.

Step 3 − The DHT11 sensor’s Vin is to be connected to 3V3 / Vin pin of ESP32. Connect the DHT11 sensor’s GND to the GND pin of ESP32. Connect the data pin of DHT11 sensor to the D15 pin of ESP32.

Step 4 − Write the C program using Arduino for measuring the temperature.

Step 5 − Attach the ESP32 to the computer using a USB data cable.

Step 6 − Compile and transfer the code into ESP32 and check the result on the serial monitor.

The Code

// Read the temperature and humidity of the place using DHT11 sensor
#include "DHT.h"
// connected to ESP32 pin 15
#define DHT_PIN 15
#define DHT_TYPE DHT11

// making the DHT sensor object
DHT dhtt(DHT_PIN, DHT_TYPE);
void setup() {
   //set the baud rate
   Serial.begin(9600);
   Serial.println("Using the DHT sensor!");
   // start the sensor for temperature and humidity
   dhtt.begin();
}
void loop() {
   //read the humidity value
   float hum = dhtt.readHumidity();
   //read the temperature value
   float tem = dhtt.readTemperature();
   // if unable to get reading correctly
   if (isnan(hum) || isnan(tem)) {
      Serial.println("Failed to read from DHT sensor!");
      return;
   }
   // print Humidity
   Serial.print("Humidity: ");
   Serial.print(hum);
   Serial.print(",");
   // print Temperature
   Serial.print("Temperature: ");
   Serial.println(tem);
   delay(2000);
}

Viewing The Result

The result can be seen in Serial Monitor as soon the code is compiled and transferred/uploaded to the ESP32.

Fig: Showing the result of temperature and humidity readings using Serial Monitor

Example 2: Showing the readings from the DHT22 sensor using Wokwi Simulator and LCD display

Here, the DHT22 Sensor is used to measure the temperature and humidity. The C language program is written here using Wokwi simulator. It uses "DHT.h for communications between DHT22 and the ESP32 and LiquidCrystal_I2C.h for using the LCD. The result can be seen using the Wokwi Simulator.

Circuit Design Steps and Coding:

Step 1 − First login to Wokwi. Now start an ESP32 new project.

Step 2 − Click the plus sign to add components and select a DHT22 and an led display. Next, the DHT22 sensor is connected to ESP32.

Step 3 − The DHT22 sensor’s Vin is to be connected to the 3V3 / Vin pin of ESP32. Connect the DHT22 sensor’s GND to the GND pin of ESP32. Connect the data pin of the DHT22 sensor to the D15 pin of ESP32.

Step 4 − Click the plus sign to add components and select an LCD type 16x2 (16 col and 2 rows) and connect this display device with ESP32.

Step 5 − Write the C program using Wokwi Simulator and add the required libraries using that libraries.txt tab.

Step 6 − Press the play button to compile the code and check the result on the lcd display.

The Code

//The DHT library
#include <DHT.h>
//The LCD library
#include <LiquidCrystal_I2C.h>
// connected to ESP32 pin 15
#define DHT_PIN 15

//Here DHT22 Type is used
#define DHT_TYPE DHT22

// making the DHT sensor object
DHT dhtt(DHT_PIN, DHT_TYPE);

float tem ;
float hum;

//making the myLcd object
LiquidCrystal_I2C myLcd(0x27, 16, 2);

void setup(){
   //set the baud rate
   Serial.begin(9600);
   Serial.println("Using the DHT sensor!");
   // start the sensor for temperature and humidity
   dhtt.begin();
   //initialise the lcd
   myLcd.init();
   //clear the lcd
   myLcd.clear();
   //set the position to start the print
   myLcd.setCursor(2, 0);
   //print message
   myLcd.print("DHT Sensor!");
   delay(500);
}
void loop() {
   //read the temperature value
   tem = dhtt.readTemperature();
   //read the humidity value
   hum = dhtt.readHumidity();
   delay(1000);
   myLcd.clear();
   myLcd.setCursor(1, 0);
   //print the temperature value
   myLcd.print("Temperature: "+ String(tem));
   myLcd.setCursor(1, 1);
   //print the humidity value
   myLcd.print("Humidity: "+ String(hum));
   delay(1000);
}

Viewing The Result - Example 2

Fig 3: Showing the result for DHT22 readings using Wokwi.

In this article, using two different examples, the ways to use a DHT11 sensor/ DHT22 with ESP32 is given. In the first example, the DHT11 Sensor is used to measure the temperature and humidity by making the actual circuit. In the second example, the DHT22 Sensor is used to measure the temperature and humidity on an lcd display using the Wokwi Simulator.

Updated on: 18-Apr-2023

673 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements