Description

100%
100%

DHT11 digital temperature and humidity sensor is a composite Sensor contains a calibrated digital signal output of the temperature and humidity. The sensor includes a resistive sense of wet components and an NTC temperature measurement devices, and connected with a high-performance 8-bit microcontroller. Today in this tutorial we will be using DHT11 sensor with PIC18F4550.

DHT11 uses One wire protocol that means in order to send or receive data from the sensor, only one wire is used. Also we have to work with Delays in microseconds because that’s how we are going to differentiate between a ‘1’ or a ‘0’. You will get the better idea soon once I will explain you the entire process of it.

HOW TO

In order for the sensor to work, we need to follow the procedure below

  • Start the sensor
  • Check for Response
  • Read the 40 bit Temperature and Humidity Data

Let us see all these processes in details now

 START DHT11 

As according to the datasheet, to start the sensor we need to do the following:-

  • Set the I/O pin as output
  • Pull the pin LOW
  • wait for >18ms
  • Set the pin as INPUT

As you can see above, HOST (Microcontroller) sends a START signal by pulling the data pin low for around 18ms. After this we will set the Pin as Input and let the sensor control it.

 Check Response 

Once the Sensor receives the Start signal, It will transmit the response indicating it’s presence. The response consists of a 80us LOW, followed by 80us HIGH.

 DATA bits 

After sending the response, DHT11 will now transmit the 40 bit DATA. These 40 bits consists of the following

  • 8bit humidity integer data
  • 8bit the Humidity decimal data
  • 8 bit temperature integer data
  • 8bit fractional temperature data
  • 8 bit parity data

The Last Parity Byte is the SUM of all other 4 Bytes. It is for checking if the data received is correct or not.

Now the only thing we need to do is read the incoming bit whether it’s a ‘0’ or a ‘1’. This can be done as mentioned below:

A ‘0’ is transmitted by the sensor by First Pulling the pin LOW for 50us followed by Pulling HIGH for around 26-28us and A ‘1’ is transmitted by First Pulling the pin LOW for 50us followed by Pulling HIGH for around 70us.

it’s not efficient to check whether the pin is HIGH or LOW every 1us. So we will just check after let’s say 40us. If the Pin would be HIGH after 40us, we could say that it’s a ‘1’ or if it is LOW, a ‘0’. Better see the implementation below.

Some Insight into the CODE

DHT11_START –> Starts the sensor
void DHT11_Start (void)
{
	DHT11_Pin_Direction = 0;  // set the pin as output
	DHT11_Pin = 0;   // pull the pin low
	__delay_ms (18);  // wait for 18ms
	DHT11_Pin_Direction = 1;   // set the PIN as input
}
DHT11_Check_Response –> Checks the response.

I am skipping the actual check. Just wait for the Pins to go high and LOW

void DHT11_Check_Response (void)  // just ignore this but you have to put it in the main
{
    while (DHT11_Pin);  // wait for pin to go low
    while (!DHT11_Pin);   // wait for the pin to go HIGH
    while (DHT11_Pin);   // wait for the pin to go low
}
uint8_t read_data() –> Reads 1 Byte of data and returns it
uint8_t read_data (void)
{
	uint8_t i,j;
	for (j=0;j<8;j++)
	{
		while (DHT11_Pin == 0);   // wait for the pin to go high
		__delay_us (40);   // wait for 40 us
		if (DHT11_Pin == 0)   // if the pin is low 
		{
			i&= ~(1<<(7-j));   // write 0
		}
		else i|= (1<<(7-j));  // if the pin is high, write 1
		while (DHT11_Pin);  // wait for the pin to go low
	}
	return i;
}

YOU CAN DOWNLOAD FULL CODE AT THE END OF THIS POST

100%
100%

Connections

100%
100%
100%
100%

Result

100%
100%
Check out the VIDEO Below
100%
100%

DOWNLOAD

You can buy me a coffee sensor ūüôā

Or Just download the CODE below

100%
100%

Leave a Reply

avatar
  Subscribe  
Notify of
Menu