Project
Reading Temperature and Humidity with DHT22 on ESP32
Overview
Learn how to read temperature and humidity from a DHT22 sensor using an ESP32 and Arduino code. This project is useful for weather stations, room monitoring, and simple climate-control projects.
Instructions
Step by step
1. Connect the DHT22 VCC pin to 3.3V.
2. Connect the DHT22 GND pin to GND.
3. Connect the DHT22 Data pin to D4.
4. Run the code and open the Serial Monitor.
5. Change the temperature and humidity values in the simulator.
6. Check if the printed temperature and humidity values change.
7. If the values show as NaN, check the wiring and make sure the data pin matches the code.
Code
Source code
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
Serial.println("DHT22 Temperature and Humidity Test");
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
delay(2000);
}
Related