Measurement of temperature and relative humidity using DHT11

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Measurement of temperature and relative humidity using DHT11

Postby Enamul » Fri Nov 23, 2012 9:58 pm

Measurement and control of temperature and relative humidity finds applications in numerous areas. These days devices are available which have both temperature and humidity sensors with signal conditioning, ADC, calibration and communication interface all built inside them. The use of such smart sensors greatly simplifies the design and reduces the overall cost. These sensors are capable of measuring both temperature and relative humidity and provide fully calibrated digital outputs. While SHT1x/SHT7x are very accurate sensors, they are still expensive for hobbyists use. This article discusses the DHT11 sensor which also provides calibrated digital outputs for temperature and humidity but is relatively lot cheaper than the Sensirion sensors. The DHT11 sensor uses a proprietary 1-wire protocol which we will be exploring here and implementing with the PIC16F628A microcontroller that will receive the temperature and humidity values from the sensor and display them on a 16×2 character LCD.
About DHT11 sensor
The DHT11 sensor comes in a single row 4-pin package and operates from 3.5 to 5.5V power supply. It can measure temperature from 0-50 °C with an accuracy of ±2°C and relative humidity ranging from 20-95% with an accuracy of ±5%. The sensor provides fully calibrated digital outputs for the two measurements. It has got its own proprietary 1-wire protocol, and therefore, the communication between the sensor and a microcontroller is not possible through a direct interface with any of its peripherals. The protocol must be implemented in the firmware of the MCU with precise timing required by the sensor.
DHT11Sensor1.jpg
DHT11Sensor1.jpg (41.3 KiB) Viewed 21397 times

The following timing diagrams describe the data transfer protocol between a MCU and the DHT11 sensor. The MCU initiates data transmission by issuing a “Start” signal. The MCU pin must be configured as output for this purpose. The MCU first pulls the data line low for at least 18 ms and then pulls it high for next 20-40 μs before it releases it. Next, the sensor responds to the MCU “Start“signal by pulling the line low for 80 μs followed by a logic high signal that also lasts for 80 μs. Remember that the MCU pin must be configured to input after finishing the “Start“signal. Once detecting the response signal from the sensor, the MCU should be ready to receive data from the sensor. The sensor then sends 40 bits (5 bytes) of data continuously in the data line. Note that while transmitting bytes, the sensor sends the most significant bit first.
TimingDiagram1.jpg
TimingDiagram1.jpg (30.68 KiB) Viewed 21397 times

The 40-bit data from the sensor has the following structure.
Data (40-bit) = Integer Byte of RH + Decimal Byte of RH + Integer Byte of Temp. + Decimal Byte of Temp. + Checksum Byte
For DHT11 sensor, the decimal bytes of temperature and humidity measurements are always zero. Therefore, the first and third bytes of received data actually give the numeric values of the measured relative humidity (%) and temperature (°C). The last byte is the checksum byte which is used to make sure that the data transfer has happened without any error. If all the five bytes are transferred successfully then the checksum byte must be equal to the last 8 bits of the sum of the first four bytes, i.e.,
Checksum = Last 8 bits of (Integer Byte of RH + Decimal Byte of RH + Integer Byte of Temp. + Decimal Byte of Temp.)
Now let’s talk about the most important thing, which is signalling for transmitting “0″ and “1″. In order to send a bit of data, the sensor first pulls the line low for 50 μs. Then it raises the line to high for 26-28 μs if it has to send “0″, or for 70 μs if the bit to be transmitted is “1″. So it is the width of the positive pulse that carries information about 1 and 0.
TimingDiagram2.jpg
TimingDiagram2.jpg (20.81 KiB) Viewed 21397 times

CombinedTiming.jpg
CombinedTiming.jpg (34.16 KiB) Viewed 21397 times

At the end of the last transmitted bit, the sensor pulls the data line low for 50 μs and then releases it. The DHT11 sensor requires an external pull-up resistor to be connected between its Vcc and the data line so that under idle condition, the data line is always pulled high. After finishing the data transmission and releasing the data line, the DHT11 sensor goes to the low-power consumption mode until a new “Start” signal arrives from the MCU.
Circuit diagram
Here is the circuit diagram showing the DHT11 sensor and a HD44780-based character LCD interfaced to the PIC16F877A microcontroller. The microcontroller runs at 20.0 MHz clock using an external resonator connected between OSC1 and OSC2 pins. The use of 20.0 MHz clock makes the timing calculation easier as 1 machine cycle becomes 0.2 μs. The timing information will be used to calculate the width of the received data pulse from the sensor so that we could identify if it is carrying a 1 or 0. The clock frequency is very important for this sensor specially using Flowcode as there are couple of inherent delays in Flowcode components which fails the sensor to run properly at lower speed.
DHT11_PIC16F.jpg
DHT11_PIC16F.jpg (64.61 KiB) Viewed 21397 times

231120121827.jpg
231120121827.jpg (66.56 KiB) Viewed 21397 times

Software
Writing software for DHT11 sensor is little more challenging than the hardware part because of the timing conditions for 1s and 0s. I have written sub-routines in Flowcode v5.4 for PIC for initializing the DHT11 sensor and reading the 40-bit of data in sequence. I have used Timer0 module to keep track of the width of the received data pulse, which is required to identify if the received bit is 1 or 0. When a low-to-high pulse is detected at the beginning of any data bit, TMR0 is cleared and turned ON. Since the clock frequency used here is 20.0 MHz, the TMR0 increments by 1 in every 0.8 μs (prescaler 1:4). The TMR0 is stopped whenever the data pulse is low again. The value of the TMR0 register multiplied by 0.8 gives you the width of the data pulse in μs. I am using 30 μs (38*0.8=30 μs) as the threshold for identifying 0 and 1. If the TMR0 is greater than 38 (30 μs), it means the received bit is 1, else it is 0. Here is the complete source code written in Flowcode for PIC. It can be easily adapted to any other platform, but remember that if you are using a different clock frequency you should have to modify the timer operation accordingly and carefully.
Note: I have tried to use macro read byte to re-use the macro code..but failed I guess because there are some inherent delay in calling and returning from macro in Flowcode. That's why you will see I have just read bytes one after another.
DHT11v1.01.fcf
Code written in FCv5
(40.6 KiB) Downloaded 1179 times

Output
The accuracy of DHT11 is not as good as Sensirion’s SHT1X/7X series sensors, but it provides an easy and cheap solution to hobbyists for measuring relative humidity and temperature in parallel using a single device, which is sometime required in certain applications such as calculating the dew point.
LCD_output.jpg
LCD_output.jpg (32.91 KiB) Viewed 21397 times

Adapted from:http://embedded-lab.com/blog/?p=4333
These users thanked the author Enamul for the post (total 6):
nc56 (Fri Nov 23, 2012 10:51 pm) • belayat (Sat Nov 24, 2012 11:52 pm) • JohnCrow (Sun Nov 25, 2012 11:58 am) • medelec35 (Sun Nov 25, 2012 9:25 pm) • Oderlando (Fri Nov 23, 2018 4:59 pm) • chipfryer27 (Wed Jan 02, 2019 1:06 pm)
Rating: 31.58%
 
Enamul
University of Nottingham
enamul4mm@gmail.com

nc56
Posts: 60
Joined: Thu Nov 15, 2012 1:21 pm
Has thanked: 14 times
Been thanked: 4 times
Contact:

Re: Measurement of temperature and relative humidity using D

Postby nc56 » Fri Nov 23, 2012 10:49 pm

Goood work
really it provides an easy and cheap solution for whose didn't find the sht sensor like me
congratulation
Last edited by nc56 on Fri Nov 23, 2012 11:03 pm, edited 1 time in total.

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Measurement of temperature and relative humidity using D

Postby Enamul » Fri Nov 23, 2012 11:02 pm

Yes, you are absolutely right. If you buy from china it could cost £1.12 which is really cheap in comparison to separate temperature and humidity sensors.
http://www.ebay.co.uk/itm/New-DHT11-Dig ... 0737643115
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Measurement of temperature and relative humidity using D

Postby Enamul » Sun Nov 25, 2012 1:15 am

Make your own Weather Station:
When I was working for DHT11 temperature and humidity sensor I was thinking about my college friend who made a weather station using the sensors available locally. DHT11 and the Anemometer for wind speed measuring could make her project how much easy!
Anemometer:
The anemometer is made of brass, and is therefore very impervious to weather. The anemometer can be configured to individual specifications. The system is frictionless, which gives very high accuracy.
Measuring range: 2-30 m/s
Resolution: 0.1 m/s
Output Frequency: 10 Hz/m/s
Customized output: NPN, PNP, Namur or Fiber optic
Anemometer.png
Anemometer.png (149.72 KiB) Viewed 21385 times

Mode of Operation:
The anemometer is usually placed approx. 10 m above ground level. It must be placed in a position where it is unimpeded by buildings and other wind-suppressing obstacles, so that turbulence at the instruments is reduced as much as possible.
Anemometer and adaptor must be mounted under observance of reliable craftsmanship and must be fastened in a way so that they present no danger to persons or goods, even at extreme blasts of wind. The pole and other devices forming part of the system must be effectively mutually connected to earthing systems for lightning protection and equipotential bonding.
http://dwc-el.dk/products/products.aspx
Software:
The code is written using FCv5.4 where RB0 is used to detect the pulse coming from the Anemometer every second and to improve accuracy second measurement is done using TMR0. Wind speed is updated every second which is shown in a 2*16 LCD.
Attachments
Anemometer.fcf
(17.92 KiB) Downloaded 535 times
These users thanked the author Enamul for the post (total 2):
nc56 (Sun Nov 25, 2012 9:41 am) • medelec35 (Sun Nov 25, 2012 9:26 pm)
Rating: 10.53%
 
Enamul
University of Nottingham
enamul4mm@gmail.com

nc56
Posts: 60
Joined: Thu Nov 15, 2012 1:21 pm
Has thanked: 14 times
Been thanked: 4 times
Contact:

Re: Measurement of temperature and relative humidity using D

Postby nc56 » Sun Nov 25, 2012 9:56 am

Hi , it's great project
the RB0 pulse how it's generated ?
I think It's not useful if the lcd is attached in 10m high with the anemometer
so we can use zegbe module to send the result to a operating room

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Measurement of temperature and relative humidity using D

Postby Enamul » Sun Nov 25, 2012 10:13 am

the RB0 pulse how it's generated ?
I think It's not useful if the lcd is attached in 10m high with the anemometer
No, you don't need to attach the LCD or PIC hardware close to Anemometer. As 10 m is above ground level but you can use 2-3 meter wire to get the signal from Anemometer to PIC. If the wire length exceeds 10 m, you can simply use some kind repeater circuit (low noise amplifier ) as you have to carry power line for the Anemometer.

There is also an optical fiber interface facility (optional) in the Anemometer, so you can use that in that case you can use even further.
Enamul
University of Nottingham
enamul4mm@gmail.com

nc56
Posts: 60
Joined: Thu Nov 15, 2012 1:21 pm
Has thanked: 14 times
Been thanked: 4 times
Contact:

Re: Measurement of temperature and relative humidity using D

Postby nc56 » Sun Nov 25, 2012 10:20 am

what about the RB0 pulse ? how is it generated ?
power can be a battery placed with anemometer
so you don't need a cable
Last edited by nc56 on Sun Nov 25, 2012 10:22 am, edited 1 time in total.

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Measurement of temperature and relative humidity using D

Postby Enamul » Sun Nov 25, 2012 10:21 am

Actually the Anemometer is producing 10 pulse per second for 1 m/s wind speed. I have used RB0/INT to detect number of pulse in one second.
Enamul
University of Nottingham
enamul4mm@gmail.com

nc56
Posts: 60
Joined: Thu Nov 15, 2012 1:21 pm
Has thanked: 14 times
Been thanked: 4 times
Contact:

Re: Measurement of temperature and relative humidity using D

Postby nc56 » Sun Nov 25, 2012 10:31 am

sorry I mean what s the mecanism of the anemometer to generate pulse ?

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Measurement of temperature and relative humidity using D

Postby Enamul » Sun Nov 25, 2012 10:50 am

Here is the working principle of the 3-cup Anemometer..
Measures the speed of a wind powered turbine in the form of (usually hemispherical) cups mounted on radial spokes. Rotation speed can be measured by a magnet, affixed to the shaft, traversing past a fixed coil induces a pulse for each revolution, or a digital shaft encoder is used
These users thanked the author Enamul for the post:
nc56 (Sun Nov 25, 2012 10:54 am)
Rating: 5.26%
 
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
JohnCrow
Valued Contributor
Valued Contributor
Posts: 1367
Joined: Wed Sep 19, 2007 1:21 pm
Location: Lincolnshire
Has thanked: 381 times
Been thanked: 715 times
Contact:

Re: Measurement of temperature and relative humidity using D

Postby JohnCrow » Sun Nov 25, 2012 12:46 pm

Hi Enamul

Ive just ordered a couple of these sensors myself. Look forward to trying them with your software
These users thanked the author JohnCrow for the post:
Enamul (Sun Nov 25, 2012 12:48 pm)
Rating: 5.26%
 
1 in 10 people understand binary, the other one doesn't !

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Measurement of temperature and relative humidity using D

Postby Enamul » Sun Nov 25, 2012 12:50 pm

That's nice. I have struggled huge to make it working..Timing is really critical for the sensor. I feel the necessity of assembly after couple of years while working with the chip.
Enamul
University of Nottingham
enamul4mm@gmail.com

nc56
Posts: 60
Joined: Thu Nov 15, 2012 1:21 pm
Has thanked: 14 times
Been thanked: 4 times
Contact:

Re: Measurement of temperature and relative humidity using D

Postby nc56 » Mon Nov 26, 2012 10:41 pm

Hi Enamul
I changed the code to work with the pic 18f4550
so I used TMR2( 8bit )
but I think there is a problem of configuration or synchronisation
I had checksum EError
this is the code
can you help me please
Attachments
DHT11 184550.01(1).fcf
(40.89 KiB) Downloaded 454 times
Last edited by nc56 on Mon Nov 26, 2012 10:52 pm, edited 1 time in total.

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Measurement of temperature and relative humidity using D

Postby Enamul » Mon Nov 26, 2012 10:51 pm

Of course..I will have a look and let you know. Can I use TRM0 in place of TRM2?
Enamul
University of Nottingham
enamul4mm@gmail.com

nc56
Posts: 60
Joined: Thu Nov 15, 2012 1:21 pm
Has thanked: 14 times
Been thanked: 4 times
Contact:

Re: Measurement of temperature and relative humidity using D

Postby nc56 » Mon Nov 26, 2012 10:53 pm

yes you can use TMR0, it's not used in my application

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Measurement of temperature and relative humidity using D

Postby Enamul » Mon Nov 26, 2012 11:40 pm

Hi

I have modified the code for the TMR0 and TMR0 is 8-bit Timer here..so please don't change any thing. I have corrected the configuration so that it should work fine now.

please let me know how you are getting..
Attachments
DHT11_184550.fcf
(40.94 KiB) Downloaded 476 times
Enamul
University of Nottingham
enamul4mm@gmail.com

nc56
Posts: 60
Joined: Thu Nov 15, 2012 1:21 pm
Has thanked: 14 times
Been thanked: 4 times
Contact:

Re: Measurement of temperature and relative humidity using D

Postby nc56 » Mon Nov 26, 2012 11:52 pm

Hi
I was testing the code
now , it shows TEMP=0.0 C
RH = 0.0 %
the value are not correct

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Measurement of temperature and relative humidity using D

Postby Enamul » Mon Nov 26, 2012 11:59 pm

That's really strange! I don't have your chip in hand but I have ECIO-40 where I can test the code..I will test and let you know.
These users thanked the author Enamul for the post:
nc56 (Tue Nov 27, 2012 12:00 am)
Rating: 5.26%
 
Enamul
University of Nottingham
enamul4mm@gmail.com

nc56
Posts: 60
Joined: Thu Nov 15, 2012 1:21 pm
Has thanked: 14 times
Been thanked: 4 times
Contact:

Re: Measurement of temperature and relative humidity using D

Postby nc56 » Tue Nov 27, 2012 12:18 am

in the configuration why the CPU sys clock is div by 3 ?

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Measurement of temperature and relative humidity using D

Postby Enamul » Tue Nov 27, 2012 12:23 am

Yes, you picked the right one. It should be "No divide".
Enamul
University of Nottingham
enamul4mm@gmail.com

nc56
Posts: 60
Joined: Thu Nov 15, 2012 1:21 pm
Has thanked: 14 times
Been thanked: 4 times
Contact:

Re: Measurement of temperature and relative humidity using D

Postby nc56 » Tue Nov 27, 2012 8:19 pm

Hi Enamul ; It's working :)
these are the picture of the sensor and the test
and the final code
Attachments
DHT11_184550.fcf
(40.91 KiB) Downloaded 612 times
Nouveau dossier (3).rar
(141.8 KiB) Downloaded 630 times
These users thanked the author nc56 for the post:
Enamul (Tue Nov 27, 2012 8:51 pm)
Rating: 5.26%
 

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Measurement of temperature and relative humidity using D

Postby Enamul » Tue Nov 27, 2012 8:51 pm

That's great. Glad to know.
Enamul
University of Nottingham
enamul4mm@gmail.com

nc56
Posts: 60
Joined: Thu Nov 15, 2012 1:21 pm
Has thanked: 14 times
Been thanked: 4 times
Contact:

Re: Measurement of temperature and relative humidity using D

Postby nc56 » Tue Nov 27, 2012 8:56 pm

Thank you so much for your help :)

User avatar
JohnCrow
Valued Contributor
Valued Contributor
Posts: 1367
Joined: Wed Sep 19, 2007 1:21 pm
Location: Lincolnshire
Has thanked: 381 times
Been thanked: 715 times
Contact:

Re: Measurement of temperature and relative humidity using D

Postby JohnCrow » Sun Dec 02, 2012 1:07 pm

Hi Enamul

Just tried your code with my sensor. Works great :)
Thanks
These users thanked the author JohnCrow for the post:
Enamul (Sun Dec 02, 2012 1:15 pm)
Rating: 5.26%
 
1 in 10 people understand binary, the other one doesn't !

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Measurement of temperature and relative humidity using D

Postby Enamul » Sun Dec 02, 2012 1:16 pm

You are most welcome John :)
Enamul
University of Nottingham
enamul4mm@gmail.com