Getting Started with DHT Sensors and Arduino
The DHT sensor family (DHT11, DHT22, etc.) are popular choices for measuring temperature and humidity in Arduino projects. These sensors are low-cost, easy to use, and provide reliable readings for a variety of applications. This tutorial will guide you through connecting a DHT sensor to your Arduino and reading the temperature and humidity data.
About DHT11 Temperature and Humidity Sensor
- Operating Voltage: 3 to 5V
- Temperature Range: 0°C to 50°C
- Temperature Accuracy: ±2°C
- Humidity Range: 20% to 80%
- Humidity Accuracy: 5%
- Reading Rate: 1Hz (once every second)
About DHT22 Temperature and Humidity Sensor
- Operating Voltage: 3 to 5V
- Temperature Range: -40°C to 80°C
- Temperature Accuracy: ±0.5°C
- Humidity Range: 0% to 100%
- Humidity Accuracy: ±2% to 5%
- Reading Rate: 0.5 Hz (once every 2 seconds)
Components
Arduino Uno
Breadboard
DHT11 or DHT22
Jumper wires
Connect the DHT sensor
- Identify the pins on your DHT sensor. Typically, there will be four pins: VCC (power), GND (ground), DATA (data signal), and NC (no connect).
- Connect the VCC pin of the DHT sensor to the 5V pin on your Arduino.
- Connect the GND pin of the DHT sensor to the GND pin on your Arduino.
- Connect the DATA pin of the DHT sensor to a digital pin on your Arduino. For this tutorial, we’ll use pin 2.
DHT sensor library
The Code!
//Libraries #include <dht.h> dht DHT; //Constants #define DHT11_PIN 2 // or DHT 22 and pin number //Variables float hum; //Stores humidity value float temp; //Stores temperature value void setup() { Serial.begin(9600); } void loop() { int chk = DHT.read11(DHT11_PIN); //Read data and store it to variables hum and temp hum = DHT.humidity; temp= DHT.temperature; //Print temp and humidity values to serial monitor Serial.print("Humidity: "); Serial.print(hum); Serial.print(" %, Temp: "); Serial.print(temp); Serial.println(" Celsius"); delay(2000); //Delay 2 sec. }
View sensor readings:
- Open the Arduino IDE serial monitor (Tools > Serial Monitor).
- You should see the temperature and humidity readings displayed every 2 seconds.