The DHT11 sensor is a widely used digital temperature and humidity sensor module capable of measuring ambient temperature and relative humidity in various applications.
Temperature Measurement: The DHT11 sensor can measure temperature within a range of 0°C to 50°C (32°F to 122°F) with a typical accuracy of ±2°C.
Humidity Measurement: It can measure relative humidity within a range of 20% to 90% RH with a typical accuracy of ±5%.
Digital Output: The sensor provides a digital output, making it easy to interface with microcontrollers and digital systems. It uses a single-wire communication protocol, where the data is transmitted serially and can be read using a simple software routine.
The goal is to create a circuit that allows us to determine the Temperature and Humidity of the environment by using the DHT11 sensor and display it on a 16*2 LCD.
P89V51RD2
Breadboard
16*2 LCD
Potentiometer(10K ohm)
Jumper wires
DHT11 sensor
Copy and paste the following code into main.c or download it from here:
// DHT11 Interfacing with 8051
#include <reg51.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "LCD16x2_4bit.h" // User defined header file
sbit DHT11=P2^1; // Connect DHT11 Sensor 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 start pulse or 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);
}
}
//Note: Save the below give code as LCD16x2_4bit.h header file. This is a user defined header file.
#include <reg51.h>
sfr LCD_Port=0x90; // P1 port as data port
sbit rs=P0^0; // Register select pin
sbit rw=P0^1; // Read/Write pin
sbit en=P0^2; // Enable pin
// Function to provide delay Approx 1ms with 11.0592 Mhz crystal
void delay(unsigned int count)
{
int i,j;
for(i=0;i<count;i++)
for(j=0;j<112;j++);
}
void LCD_Command (char cmnd) // LCD16x2 command funtion
{
LCD_Port =(LCD_Port & 0x0F) | (cmnd & 0xF0); // sending upper nibble
rs=0; // command reg.
rw=0; // Write operation
en=1;
delay(1);
en=0;
delay(2);
LCD_Port = (LCD_Port & 0x0F) | (cmnd << 4); // sending lower nibble
en=1; // enable pulse
delay(1);
en=0;
delay(5);
}
void LCD_Char (char char_data) // LCD data write function
{
LCD_Port =(LCD_Port & 0x0F) | (char_data & 0xF0); //sending upper nibble
rs=1; //Data reg.
rw=0; //Write operation
en=1;
delay(1);
en=0;
delay(2);
LCD_Port = (LCD_Port & 0x0F) | (char_data << 4); // sending lower nibble
en=1; // enable pulse
delay(1);
en=0;
delay(5);
}
void LCD_String (char *str) // Send string to LCD function
{
int i;
for(i=0;str[i]!=0;i++) // Send each char of string till the NULL
{
LCD_Char (str[i]); // Call LCD data write
}
}
void LCD_String_xy (char row, char pos, char *str) // Send string to LCD function
{
if (row == 0)
LCD_Command((pos & 0x0F)|0x80); // Command of first row and required position<16
else if (row == 1)
LCD_Command((pos & 0x0F)|0xC0); // Command of first row and required position<16
LCD_String(str); // Call LCD string function
}
void LCD_Init (void) // LCD Initialize function
{
delay(20); // LCD Power ON Initialization time >15ms
LCD_Command (0x02); // 4bit mode
LCD_Command (0x28); // Initialization of 16X2 LCD in 4bit mode
LCD_Command (0x0C); // Display ON Cursor OFF
LCD_Command (0x06); // Auto Increment cursor
LCD_Command (0x01); // clear display
delay(3);
LCD_Command (0x80); // cursor at home position
}
//Add both files into Source Group 1 //
The LCD displays the surrounding temperature and humidity. The interfacing of the DHT11 sensor with the 8051 microcontroller facilitates the integration of temperature and humidity sensing capabilities into embedded systems. It empowers applications in various fields, including weather monitoring, home automation, agriculture, HVAC systems, and industrial monitoring. The DHT11-8051 interface allows for reliable and accurate environmental data acquisition, enhancing the functionality, control, and efficiency of the overall system.
Water Sensor<- Previous Next-> Motors