You are on page 1of 7

DHT11 Sensor Interfacing with ATmega16 / 32.

DHT11 is a single wire digital humidity and temperature sensor, which gives relative
humidity in percentage and temperature in degree Celsius.
Introduction

DHT11 is a single wire digital humidity and temperature sensor, which provides
humidity and temperature values serially.

DHT11 sensor provides humidity value in percentage in relative humidity (20 to 90%
RH) and temperature values in degree Celsius (0 to 50 C)

DHT11 sensor uses resistive humidity measurement component, and NTC


temperature measurement component.

Pin Description:

DHT11 is 4 pin sensor, these pins are VCC, DATA, GND and one pin is not in use,
shown in fig below.

Fig) Pin diagram of DHT11

Pin No.

Pin Name

Pin Description

Power supply
1

VCC
3.3 to 5.5 Volt DC

DATA

Digital output pin

Pin No.

Pin Name

Pin Description

NC

Not in use

Gnd

Ground

Communication with microcontroller

DHT11 uses only one line for communication. The voltage levels with certain time
value defines the logic one or logic zero on this pin.

The communication process is divided in three steps, first is to send request to DHT11
sensor then sensor will send response pulse and then it starts sending data of total 40
bits to the microcontroller.

Fig) Communication process


Start pulse (Request)

To start communication with DHT11, first we have to send the start pulse to the
DHT11 sensor.

To provide start pulse, pull down the data pin minimum 18ms and then pull up, as
shown in diag.

Response

After getting start pulse from the controller, DHT11 sends the response pulse to the
microcontroller which will indicate that DHT11 received start pulse.

The response pulse is low for 54us and then goes high for 80us.

After sending the response pulse, DHT11 sensor sends the data, which contains
humidity and temperature value along with checksum.

The data frame is of total 40 bits long, it contains 5 segments (byte) and each segment
is 8-bit long.

In this 5 segments first two segments content humidity value in decimal integer form.
This Value gives us Relative Percentage Humidity. 1st 8-bits are integer part and next
8 bits are fractional part

Next two segment content temperature value in decimal integer form. This value gives
us temperature in Celsius form.

Last segment is the check sum which holds check sum of first four segment.

Here checksum byte is direct addition of humidity and temperature value. And we can
verify it in microcontroller whether it is same as checksum value or not. If it is not
equal, then there is some error in the data value otherwise the data is correct.

Once microcontroller receives data, DHT11 pin goes in low power consumption mode
until the microcontroller do not sends start pulse again.
End of frame

After sending 40-bit data, DHT11 sensor sends 54us low level and then goes high.
After this DHT11 goes in sleep mode.

Circuit diagram

Fig) Circuit diagram

The above circuit diagram shows interfacing of ATmega16 / 32 to DHT11 sensor.

In that, LCD is interfaced to PORTB in 4-bit mode, and DHT11 sensor is connected to
PD6 (Pin no 20).

Programming of DHT11

First initialize the LCD16x2_4bit library.

Define pin no. to interface DHT11 sensor, in our program we define PD6 (Pin no. 20).

Send the start pulse to DHT11 sensor, making low to high.

Receive the response pulse from the DHT11 sensor.

After receiving the response, receive 40-bit data serially from DHT11 sensor.

Display these received data on LCD16x2 along with error indication.

/*
* ATmega16_DHT11_Project_File.c
*
* http://www.electronicwings.com
*/
#include <avr/io.h>
#include <stdlib.h>
#include <stdio.h>
#include "LCD16x2_4bit.h"
#define DHT11_PIN 6
uint8_t c=0,I_RH,D_RH,I_Temp,D_Temp,CheckSum;
void Request()
/* Microcontroller send start pulse or request */
{
DDRD |= (1<<DHT11_PIN);
PORTD &= ~(1<<DHT11_PIN);
/* set to low pin */
_delay_ms(20);
/* wait for 20ms */
PORTD |= (1<<DHT11_PIN);
/* set to high pin */
}
void Response()
/* receive response from DHT11 */
{
DDRD &= ~(1<<DHT11_PIN);
while(PIND & (1<<DHT11_PIN));
while((PIND & (1<<DHT11_PIN))==0);
while(PIND & (1<<DHT11_PIN));
}
uint8_t Receive_data()
/* receive data */
{
for (int q=0; q<8; q++)
{
while((PIND & (1<<DHT11_PIN)) == 0); /* check received bit 0 or 1 */
_delay_us(30);
if(PIND & (1<<DHT11_PIN))
/* if high pulse is greater than 30ms */
c = (c<<1)|(0x01);
/* then its logic HIGH */
else
/* otherwise its logic LOW */
c = (c<<1);
while(PIND & (1<<DHT11_PIN));
}
return c;
}
int main(void)
{
char data[5];
lcdinit();
lcd_clear();
lcd_gotoxy(0,0);
lcd_print("Humidity =");
lcd_gotoxy(0,1);
lcd_print("Temp = ");

/* initialize LCD */
/* clear LCD */
/* enter column and row position */

while(1)
{
Request();
Response();
I_RH=Receive_data();
D_RH=Receive_data();
I_Temp=Receive_data();
D_Temp=Receive_data();
CheckSum=Receive_data();

/* send start pulse */


/* receive response */
/* store first eight bit in I_RH */
/* store next eight bit in D_RH */
/* store next eight bit in I_Temp */
/* store next eight bit in D_Temp */
/* store next eight bit in CheckSum */

if ((I_RH + D_RH + I_Temp + D_Temp) != CheckSum)


{
lcd_gotoxy(0,0);
lcd_print("Error");
}
else
{
itoa(I_RH,data,10);
lcd_gotoxy(11,0);
lcd_print(data);

lcd_print(".");
itoa(D_RH,data,10);
lcd_print(data);
lcd_print("%");
itoa(I_Temp,data,10);
lcd_gotoxy(6,1);
lcd_print(data);
lcd_print(".");
itoa(D_Temp,data,10);
lcd_print(data);
lcddata(0xDF);
lcd_print("C ");
itoa(CheckSum,data,10);
lcd_print(data);
lcd_print(" ");
}
_delay_ms(10);
}
}

You might also like