Introduction
The DHT11 sensor is a low-cost digital sensor for measuring temperature and humidity. This tutorial shows you how to connect the DHT11 to an Arduino and write code to read and display humidity values in real time.
Required Components
- DHT11 sensor
- Arduino board (e.g., Uno, Mega, or Nano)
- Breadboard
- Jumper wires
- Power source (USB or external)
Wiring Diagram
Connect the DHT11 sensor as follows:
- VCC: Connect to the 5V pin on the Arduino.
- Data: Connect to digital pin 2 (with a 10k pull-up resistor).
- GND: Connect to the Arduino GND pin.
Arduino Code
#include
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("% Temperature: ");
Serial.print(temperature);
Serial.println("°C");
delay(2000);
}
Upload this code to your Arduino and open the Serial Monitor to see humidity and temperature readings every 2 seconds.
Applications
- Weather monitoring systems
- Smart home humidity control
- Greenhouse monitoring
Conclusion
With the DHT11 sensor, you can easily monitor humidity and temperature in various environments. Experiment with adding displays or sending data to a server for advanced projects!