Read temperature and humidity with ESP32 and DHT22 temperature and humidity sensor
If you need to setup your Arduino IDE with the ESP32 microcontroller, you can follow this blogpost.
What we'll need
Hardware setup
- Put your ESP32 microcontroller on a breadboard.
- Place the DHT22 temperature and humidity sensor onto your breadboard.
- Connect the GND (or -) pin of the DHT22 sensor to the GND pin of your ESP32 microcontroller.
- Connect the VCC (or +) pin of the DHT22 sensor to the 5V pin of your ESP32 microcontroller.
- Connect the data pin of the DHT22 sensor to the G14 pin of your ESP32 microcontroller.
Add libraries to Arduino IDE
- To read the temperature and humidity from the DHT22 sensor, we'll need to add some libraries to the Arduino IDE. You can do this by going to Tools > Manage Libraries... in the Arduino IDE.
- Search for DHT and install the DHT sensor library developed by Adafruit.
- Install all the library dependencies.
Code for sketch
Create a new sketch in the Arduino IDE and add following code
#include
#define DHT_SENSOR_PIN 14 // ESP32 pin GPIO14 connected to DHT22 sensor
#define DHT_SENSOR_TYPE DHT22
DHT dht_sensor(DHT_SENSOR_PIN, DHT_SENSOR_TYPE);
void setup() {
Serial.begin(9600);
dht_sensor.begin(); // initialize the DHT sensor
}
void loop() {
// read humidity
float humi = dht_sensor.readHumidity();
// read temperature in Celsius
float tempC = dht_sensor.readTemperature();
// read temperature in Fahrenheit
float tempF = dht_sensor.readTemperature(true);
// check whether the reading is successful or not
if ( isnan(tempC) || isnan(tempF) || isnan(humi)) {
Serial.println("Failed to read from DHT sensor!");
} else {
Serial.print("Humidity: ");
Serial.print(humi);
Serial.print("%");
Serial.print(" | ");
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.print("°C ~ ");
Serial.print(tempF);
Serial.println("°F");
}
// wait 1 second between readings
delay(1000);
}
Code Explanation:
1. Include Statements:
#include
The code starts by including the necessary library for the DHT sensor. This library provides functions to communicate with and retrieve data from the DHT sensor.
2. Macro Definitions:
#define DHT_SENSOR_PIN 14
#define DHT_SENSOR_TYPE DHT22
Here, two macros are defined: DHT_SENSOR_PIN
specifies the GPIO pin to which the DHT22 sensor is connected (GPIO14 in this case), and DHT_SENSOR_TYPE
indicates the type of DHT sensor being used (DHT22).
3. DHT Object Creation:
DHT dht_sensor(DHT_SENSOR_PIN, DHT_SENSOR_TYPE);
An instance of the DHT class is created called dht_sensor
using the specified pin and sensor type.
4. Setup Function:
void setup() {
Serial.begin(9600);
dht_sensor.begin();
}
In the setup()
function, the serial communication is initialized at a baud rate of 9600 (bits per second). This is used to send data from the ESP32 to your computer for debugging purposes. Additionally, dht_sensor.begin()
initializes communication with the DHT sensor.
5. Loop Function:
void loop() {
float humi = dht_sensor.readHumidity();
float tempC = dht_sensor.readTemperature();
float tempF = dht_sensor.readTemperature(true);
Inside the loop()
function, three variables humi, tempC, and tempF are declared to store humidity, temperature in Celsius, and temperature in Fahrenheit, respectively. These values are obtained by calling corresponding functions from the dht_sensor
object.
6. Data Validation and Printing:
if (isnan(tempC) || isnan(tempF) || isnan(humi)) {
Serial.println("Failed to read from DHT sensor!");
} else {
Serial.print("Humidity: ");
Serial.print(humi);
Serial.print("%");
Serial.print(" | ");
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.print("°C ~ ");
Serial.print(tempF);
Serial.println("°F");
}
The obtained sensor data is checked for validity using the isnan()
function. If any of the readings are invalid, an error message is printed. Otherwise, the humidity, temperature in Celsius, and temperature in Fahrenheit are printed to the serial monitor.
7. Delay:
delay(1000);
After printing the data, there is a 1-second delay using the delay()
function to control the rate of sensor readings. This prevents overwhelming the sensor with too frequent requests.
Compile and upload the sketch to the ESP32
Upload the code from the Arduino IDE to the ESP32 microcontroller by clicking the upload button.
Read the data!
Observe the data in action by viewing the demonstration below:
Want to do something with LEDs? Then read this blogpost: how to control LED lights using the ESP32.
Want to know more?
See all my other posts about the ESP32 microcontroller.