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.
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 nowSTART 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|
|DHT11_Check_Response –> Checks the response.|
I am skipping the actual check. Just wait for the Pins to go high and LOW
|uint8_t read_data() –> Reads 1 Byte of data and returns it|
YOU CAN DOWNLOAD FULL CODE AT THE END OF THIS POST
- STM32 GPIO OUTPUT Config using REGISTERS November 24, 2020
- STM32 Clock Setup using Registers November 17, 2020
- USB CDC Device and HOST in STM32 November 14, 2020
- ST7735 1.8″ TFT Display with STM32 November 6, 2020
- How to communicate between HC-12 using STM32 October 5, 2020
- How to use STM32 as a KEYBOARD September 28, 2020