DHT11 Sensor
The DHT11 sensor is a digital humidity and temperature sensor with a single-wire interface, delivering humidity and temperature data serially.
It measures relative humidity from 20% to 90% RH and temperature from 0°C to 50°C.
The DHT11 has four pins: two for power supply, one unused, and one data pin for communication. Data is transmitted through the data pin, which uses pulses of varying TON and TOFF to represent logic 1, logic 0, or specific control signals like the start and end of a data frame.
For detailed information on the DHT11 sensor and guidance on its use, refer to the “DHT11 Sensor” section within the sensors and modules topic.
Connection Of DHT11 Sensor with 8051
The circuit diagram illustrates the interfacing of the 8051 microcontroller with the DHT11 sensor. In this setup, the DHT11 sensor is connected to Pin P2.1 (Pin 22) of the microcontroller.
DHT11 Sensor 8051 Programming Steps
Begin by initializing the LCD16x2_4bit.h
library.
Define the pin for interfacing with the DHT11 sensor; in this program, we use P2.1 (Pin 22).
Send a start pulse to the DHT11 sensor by toggling the data pin from low to high.
Then, wait to receive the response pulse from the DHT11 sensor.
Once the response is received, read the 40-bit data serially from the sensor.
Finally, display the received data on the 16×2 LCD, including any error indications if they occur.
DHT11 Sensor 8051 Code
/*
* DHT11 Interfacing with 8051
* http://www.electronicwings.com
*/
#include<reg51.h>
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include "LCD16x2_4bit.h"
sbit DHT11=P2^1; /* Connect DHT11 output Pin to P2.1 Pin */
int I_RH,D_RH,I_Temp,D_Temp,CheckSum;
void timer_delay20ms() /* Timer0 delay function */
{
TMOD = 0x01;
TH0 = 0xB8; /* Load higher 8-bit in TH0 */
TL0 = 0x0C; /* Load lower 8-bit in TL0 */
TR0 = 1; /* Start timer0 */
while(TF0 == 0); /* Wait until timer0 flag set */
TR0 = 0; /* Stop timer0 */
TF0 = 0; /* Clear timer0 flag */
}
void timer_delay30us() /* Timer0 delay function */
{
TMOD = 0x01; /* Timer0 mode1 (16-bit timer mode) */
TH0 = 0xFF; /* Load higher 8-bit in TH0 */
TL0 = 0xF1; /* Load lower 8-bit in TL0 */
TR0 = 1; /* Start timer0 */
while(TF0 == 0); /* Wait until timer0 flag set */
TR0 = 0; /* Stop timer0 */
TF0 = 0; /* Clear timer0 flag */
}
void Request() /* Microcontroller send request */
{
DHT11 = 0; /* set to low pin */
timer_delay20ms(); /* wait for 20ms */
DHT11 = 1; /* set to high pin */
}
void Response() /* Receive response from DHT11 */
{
while(DHT11==1);
while(DHT11==0);
while(DHT11==1);
}
int Receive_data() /* Receive data */
{
int q,c=0;
for (q=0; q<8; q++)
{
while(DHT11==0);/* check received bit 0 or 1 */
timer_delay30us();
if(DHT11 == 1) /* If high pulse is greater than 30ms */
c = (c<<1)|(0x01);/* Then its logic HIGH */
else /* otherwise its logic LOW */
c = (c<<1);
while(DHT11==1);
}
return c;
}
void main()
{
unsigned char dat[20];
LCD_Init(); /* initialize LCD */
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_String_xy(0,0,"Error");
}
else
{
sprintf(dat,"Hum = %d.%d",I_RH,D_RH);
LCD_String_xy(0,0,dat);
sprintf(dat,"Tem = %d.%d",I_Temp,D_Temp);
LCD_String_xy(1,0,dat);
LCD_Char(0xDF);
LCD_String("C");
memset(dat,0,20);
sprintf(dat,"%d ",CheckSum);
LCD_String_xy(1,13,dat);
}
delay(100);
}
}