กลับหน้าหลัก
views
How to Use Arduino with DHT11 Sensor for Temperature and Humidity
Last updated on

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.

Circuit diagram showing Arduino UNO connected to DHT11 on breadboard with 5V red wire, GND black wire, yellow data wire, and 4.7k resistor

Wiring steps:

  1. Connect 5V from Arduino to pin 1 of DHT11
  2. Connect GND from Arduino to pin 4 of DHT11
  3. Connect Digital pin 2 from Arduino to pin 2 of DHT11
  4. Connect 4.7k Ohm resistor between 5V and pin 2 (Data) of DHT11
DHT11 sensor with clearly labeled pins 1-4, where pin 1 is VCC, pin 2 is Data, pin 3 is not used, pin 4 is GND

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:

  1. Download the Adafruit Library
  2. Open Arduino IDE
  3. Go to Sketch > Include Library > Add .ZIP Library
  4. 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

  1. Connect USB cable between Arduino and computer
  2. Select board and port in Tools > Board and Tools > Port
  3. Click Upload button to upload the code
  4. Open Serial Monitor by going to Tools > Serial Monitor
  5. Set baud rate to 9600
Serial Monitor window displaying temperature and humidity readings in both Celsius and Fahrenheit

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

จ้างทำโปรเจคเลย

Project estimate

Want something like this? Open the estimate page.

The long form is now on a separate estimate page, so this article stays clean.

ความคิดเห็น

รีวิวจากคนใช้งานจริง

รีวิวจากลูกค้าและคนที่เคยใช้งาน

ถ้าเคยสั่งงาน เคยอ่านหน้านี้แล้วได้ประโยชน์ หรือมีข้อเสนอแนะ ฝากรีวิวไว้ได้เลย

กำลังโหลดรีวิว...