Index
Introduction
The DHT11 module is a popular temperature and humidity sensor module that can measure the air temperature and relative humidity. It outputs a digital signal and is simple to connect to an Arduino, making it ideal for basic weather station projects, home automation, and environmental monitoring.
Required Components
- Arduino UNO R3 SMD (or any other Arduino board)
- DHT11 Temperature and Humidity Module (with built-in pull-up resistor)
- Jumper wires (male-to-male)
Pinout
Circuit Diagram / Wiring
Follow these steps to connect the DHT11 module to the Arduino:
- DHT11 VCC → 5V (Arduino)
- DHT11 GND → GND (Arduino)
- DHT11 SIGNAL → Pin A0 (Arduino)
Arduino Code / Programming
- Go to the “Libraries” tab on the left-hand side of the screen.
- Click on the “Library Manager” button (book icon) at the top of the Libraries tab.
- In the Library Manager window, type “SimpleDHT” in the search bar.
- Locate the “SimpleDHT” library click on the “Install” button next to it.
- Wait for the library to be installed, and you’re ready to use the SimpleDHT library in your projects.
Use the following code to read temperature and humidity data from the DHT11 module:
#include <SimpleDHT.h>
// Define the pin connected to DHT11
#define DHTPIN A0
SimpleDHT11 dht11;
void setup() {
Serial.begin(9600);
Serial.println("DHT11 Example");
}
void loop() {
int err;
byte temperature = 0;
byte humidity = 0;
// Read the DHT11 sensor
if ((err = dht11.read(DHTPIN, &temperature, &humidity, NULL)) != 0) {
Serial.print("Read DHT11 failed, err=");
Serial.println(err);
delay(1000);
return;
}
// Print temperature and humidity
Serial.print("Humidity: ");
Serial.print((int)humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print((int)temperature);
Serial.println(" °C");
delay(2000); // Wait a few seconds between readings
}
Explanation of the Code
- DHT Library: The code uses the
SimpleDHT
library to handle communication with the DHT11 sensor. - Setup: The
setup()
function initializes the serial communication and the DHT sensor. - Loop: In the
loop()
, the sensor readings for temperature and humidity are taken every 2 seconds and displayed on the Serial Monitor. - Error Handling: If the readings fail, an error message is printed.
Testing and Troubleshooting
- Check Connections: Ensure the wiring is correct and secure.
- Power Supply: Make sure the module is powered properly (5V).
- Code Errors: Double-check that you have included the
DHT
library in your code.