Skip to content

DHT11

Component DHT11
Type Sensor
Function Measure temperature and humidity

Introduction

The DHT11 is a low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air and outputs a digital signal on the data pin. The sensor is commonly used in weather stations, home automation systems, and other environmental monitoring applications.

Pin Description

The DHT11 sensor has four pins: VCC, Data, NC (not connected), and GND. The VCC pin is connected to the power supply (3.3V to 5V), the GND pin is connected to ground, and the Data pin is connected to a digital pin on a microcontroller. The sensor communicates with the microcontroller using a single-wire protocol.

Code Example

The following code example demonstrates how to read the temperature and humidity values from the DHT11 sensor using an Arduino board.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "DHT.h"

#define DHT_PIN 2     // Digital pin connected to the DHT sensor
#define DHT_TYPE DHT11   // DHT 11

DHT dht(DHT_PIN, DHT_TYPE);

void setup() {
  Serial.begin(9600);
  dht.begin();
}

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" *C");
  delay(2000);
}

(This example uses the DHT library to read the temperature and humidity values from the sensor. The values are then printed to the serial port. You can view the output using the Serial Monitor in the Arduino IDE. Make sure to install the DHT library before using this code example. You can find the library in the Arduino Library Manager.)