Pantech.AI

How to Interface DHT11 Sensor with AVR ATmega16/ATmega32.

DHT11 Sensor Interfacing with AVR ATmega16/ATmega32.

Overview of DHT11

The DHT11 is a single-wire digital sensor designed to measure humidity and temperature, providing the values serially.

It can measure relative humidity in the range of 20% to 90% RH and temperature in the range of 0°C to 50°C.

The sensor has four pins: two for power supply, one unused, and one for data communication.

Communication is achieved through the data pin, where pulses with varying TON and TOFF durations represent logic 1, logic 0, start pulses, or the end of a data frame.

For detailed information about the DHT11 sensor and its usage, refer to the topic DHT11 Sensor in the Sensors and Modules section.

DHT11 Sensor

Connection Diagram of DHT11 with ATmega16/32

Interfacing DHT11 Sensor With AVR ATmega16/ATmega32

The above circuit diagram shows the interfacing of AVR ATmega16/ATmega32 to the DHT11 sensor.
In that, LCD is interfaced to PORTB in 4-bit mode, and a DHT11 sensor is connected to PD6 (Pin no 20).

DHT11 Code for ATmega16/32

First, initialize the LCD16x2_4bit library for displaying data.
Define the pin number to interface with the DHT11 sensor; in this case, we use PD6 (Pin 20) on the microcontroller.

Send a start pulse to the DHT11 sensor by transitioning the pin from low to high.
Wait for the response pulse from the DHT11 sensor.

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

Finally, display the received data on the LCD16x2 screen, and show an error indication if any issues occur during the communication.

Code
/*
 * 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/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();			/* Initialize LCD */
	lcd_clear();			/* Clear LCD */
	lcd_gotoxy(0,0);		/* Enter column and row position */
	lcd_print("Humidity =");
	lcd_gotoxy(0,1);
	lcd_print("Temp = ");
	
    while(1)
	{	
		Request();		/* send start pulse */
		Response();		/* receive response */
		I_RH=Receive_data();	/* store first eight bit in I_RH */
		D_RH=Receive_data();	/* store next eight bit in D_RH */
		I_Temp=Receive_data();	/* store next eight bit in I_Temp */
		D_Temp=Receive_data();	/* store next eight bit in D_Temp */
		CheckSum=Receive_data();/* 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);
	}	
}

Leave a Comment

Your email address will not be published. Required fields are marked *