Friday, February 10, 2017

DHT11 Library Temperature and Humidity Sensor

Contents

 

Interfacing DHT11 sensor with an AVR Microcontroller

DTH11 is a low cost hobby digital sensor used to measure temperature and relative humidity. Digital means that the sensor incorporates an 8 bit microcontroller inside that takes care of ADC measurements for you. With an analog sensor you would have to set up an ADC and measure the sensor directly and interpret the data. The DHT11 sensor uses a proprietary 1-wire protocol which is described down below.

The DHT11 sensor comes in a single row 4-pin package, breadboard friendly, and operates from 3.5 to 5.5V. It can measure temperature from 0-50 °C with an accuracy of ±2°C and relative humidity ranging from 20-95% with an accuracy of  ±5%. During measurement it draws 0.3mA and in standby 60uA. The sampling rate is 1 Hz, meaning it can provide a new reading once every second.

DHT11 pinout

DHT11 Pinout

Wiring up the DHT11 sensor

Wiring up DHT11

Pin 1 is connected to power supply VCC.
Pin 2 is the DATA LINE that communicates with the MCU and in this example is connected to pin PC5. However you can connect it to any digital pin on the microcontroller. A 4.7k resistor is used to pull up the pin according to the datasheet.
Pin 3 is not connected. You can let it flapping in the breeze.
Pin 4 is connected to ground. 

A capacitor of around 0.22uF - 0.1uF between VCC and GND is recommended. 

DHT11 serial 1-wire bi-directional communication protocol

DHT11 communication protocol
DHT11 communication protocol (click to enlarge)

In the idle state DHT11 sensor data line must be kept high by the pull-up resistor.

  • The reading starts when the microcontroller pulls the bus LOW for at least 18 milliseconds and then waits for about 20 - 40 microseconds for the sensor to pull the bus LOW. During this time the MCU pin is in reading mode and the bus line is HIGH as can be seen in the above diagram.

  • The DHT11 sensor then respond by pulling the line LOW for 80 us and then HIGH for 80 us.

  • From now the sensor starts transmitting 40 bits (5 bytes) of data. BIT 0 is transmitted by pulling the line LOW for 50 us and then HIGH for 26 - 28 us. BIT 1 is transmitted by pulling the line LOW for 50 us and then HIGH for 70 us.

  • After all 40 bits are sent, the sensor pulls the line bus LOW for 50 us and then the line is pulled HIGH by the pull-up resistor until the microcontroller initiates a new reading.

  • DHT11 data format:

The bits are sent starting with MSB (Most Significant Bit).

40 bits = 8bit humidity integer data + 8bit humidity decimal data + 8 bit temperature integer data + 8bit decimal temperature data + 8 bit parity bit.

Because DHT11 temperature accuracy is ±2°C and humidity is ±5% the decimal bits will always be 0.

The checksum is calculated by adding the first 4 bytes and the sum of them must be equal to the 5'th byte - the parity bit.

DHT11 library

The library incorporates safety checks to prevent the MCU remaining stuck in a while loop while waiting for sensor responses, in case the sensor breaks and remains in a HIGH or LOW state.

Configuration

In the header file, specify the location of the data pin where the sensor is connected. 

Read sensor

int8_t DHT11Read(void)

Reads sensor temperature and relative humidity and places received data in a global array.

Returns 0 on success, 1 if sensor didn't respond and timeout occurred and -1 on checksum mismatch.

Read temperature

int8_t DHT11ReadTemperature(void)

Return temperature in degrees Celsius, previously read using DHT11Read().

Read RH

int8_t DHT11ReadHumidity(void)

Return relative humidity, previously read using DHT11Read().

Code example

#include "DHT11.h"

int main(void){
    int8_t temperature = 0;
    int8_t humidity = 0;
 
    while(1){	
	if(DHT11Read() == 0){
	    temperature = DHT11ReadTemperature();
	    humidity = DHT11ReadHumidity();
	}else{
	    // Error
	}
	
        // Minimum sampling rate is 1 second
	_delay_ms(1000);
    }

    return 1;
}

Tip: don't put the sensor near a voltage regulator or other heat sources.


Links

v1.1 DHT11 library
Changelog
v1.1 (2025-07-03)
Some code improvements

No comments:

Post a Comment