How to Use Arduino with DHT11 Sensor for Temperature and Humidity
How to Use Arduino with DHT11 Sensor for Temperature and Humidity
This tutorial teaches you how to connect Arduino with DHT11 sensor to read temperature and humidity values from the air. Perfect for beginners who want to build weather monitoring projects.
Required Components
- Arduino UNO R3 with USB cable
- DHT11 Digital Temperature and Humidity Sensor (module with pins)
- Breadboard 830 Point
- Male-to-Female Jumper Wires 20cm
- Resistor 4.7k Ohm 1/4W 1%
- Power Adapter 9V 2A (optional for standalone operation)
Wiring Diagram
Proper wiring is critical. Incorrect connections will prevent the sensor from reading data.
Wiring steps:
- Connect 5V from Arduino to pin 1 of DHT11
- Connect GND from Arduino to pin 4 of DHT11
- Connect Digital pin 2 from Arduino to pin 2 of DHT11
- Connect 4.7k Ohm resistor between 5V and pin 2 (Data) of DHT11
Note: If using a DHT11 module with built-in PCB, you may not need to add an external resistor as it already has a pull-up resistor on board.
Installing the Library
Arduino needs a library to easily read data from DHT11. Install as follows:
- Download the Adafruit Library
- Open Arduino IDE
- Go to Sketch > Include Library > Add .ZIP Library
- Select the downloaded library file
Libraries to install:
- Adafruit Unified Sensor Driver
- DHT Sensor Library (by Adafruit)
Example Code
#include <DHT.h>
#define DHTPIN 2 // Pin connected to DHT11 Data pin
#define DHTTYPE DHT11 // Declare sensor type as DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("Starting DHT11 reading!");
dht.begin();
}
void loop() {
// Wait 2 seconds between measurements
delay(2000);
// Read humidity
float humidity = dht.readHumidity();
// Read temperature in Celsius
float temperatureC = dht.readTemperature();
// Read temperature in Fahrenheit
float temperatureF = dht.readTemperature(true);
// Check if reading failed
if (isnan(humidity) || isnan(temperatureC) || isnan(temperatureF)) {
Serial.println("Failed to read from DHT11!");
return;
}
// Display temperature and humidity
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.print(" *C ");
Serial.print(temperatureF);
Serial.println(" *F");
}
How to Upload and View Results
- Connect USB cable between Arduino and computer
- Select board and port in Tools > Board and Tools > Port
- Click Upload button to upload the code
- Open Serial Monitor by going to Tools > Serial Monitor
- Set baud rate to 9600
Additional Testing
Try blowing warm air from a hair dryer at the sensor. The temperature reading will increase. If you blow from very close, the humidity will decrease. This is an easy way to verify that the sensor is working correctly.
Using the Values in Your Code
From the example code, you can use the variables temperatureC and humidity for further processing, for example:
// Example: Turn on fan when temperature exceeds 30 degrees
if (temperatureC > 30) {
digitalWrite(LED_BUILTIN, HIGH); // Turn on LED or fan
}
Adjust the logic according to your project needs.
DHT11 Limitations
- Accuracy: Temperature ±2°C, Humidity ±5%
- Measurement range: Temperature 0-50°C, Humidity 20-90% RH
- For higher precision, consider using DHT22 instead
อยากทำโปรเจคแบบนี้?
รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน
If you need Arduino project service or urgent IoT development, see full service details on the home page
จ้างทำโปรเจคเลย