DHT11 Humidity & Temperature Sensor

Post details of your projects here.
Post Reply
Goey
Posts: 6
Joined: Fri Oct 19, 2012 12:48 pm
Location: Austin,TX

DHT11 Humidity & Temperature Sensor

Post by Goey »

As a first ARMbasic project, I thought I would try to get a DHT11 Humitity/Temperature sensor working with the Basic Chip.

The DHT11 sensor is a cheap import available on Ebay and elsewhere for less than $3.00 (US). It uses a propriatary one-wire interface where after a 18ms low start signal is received, it returns 40 "pulses" or bits of data. Each pulse represents a bit value where a 27us pulse is a "0" and a 72us pulse is a "1". Each pulse is separated by a 50us low period. 16 humidity bits are sent first followed by 16 temperature bits and then 8 checksum bits. The DHT11 has a resolution 1% RH and 1 degree Celsius and therefore sends all zeros for humidity bits 8 - 15 and temperature bits 8 - 15. So with the DHT11 we only need to process the 8 MSBits for both temperature and humidity and 8 bits for the checksum if error checking is used.

Included is the code that I came up with. It works well, but I am sure it could be improved since I struggled with the bit fiddling. I am used to Picaxe Basic where there are simple "setbit" and "clearbit" commands that operate on byte and word variables. In any case, I managed to muddle my way through it with ARMbasic, but likely in an inefficient manner.

Connecting the the DHT11 to the Basic Chip is straight forward. The output (Pin2) of the DHT11 is pulled up to 3.3v via a 4K7 resistor and goes to IO(7) on the Basic Chip. The DHT1l is supplied by 3.3v with a 100nf decoupling cap across the supply Pins.

Although the code works well, the performance of the DHT11 is less than stellar. It is slow to respond to changes in both temperature and humidity but is probably good enough for use in applications where fast response is not needed.

DHT11 Datasheet: http://www.micropik.com/PDF/dht11.pdf

EDIT: Removed unnecessary line : IO(7) = 1

Code: Select all

'************************************
'*** DHT-11 Humidity Sensor Demo ****
'************************************

#include <pulse.bas>

DIM a(8) as byte  'Array for bit twiddling  
DIM temperature as integer
DIM humidity as integer 
DIM checksum as integer 
DIM i as integer
DIM c as integer
DIM tempvar as integer

MAIN:
	wait(200)  'Stabilize 

	'========  Exercise DHT11 ==========================
	gosub Sendstart  'Two cycles to exercise DHT11      
	wait(1000)       'at at startup. This should also  
	gosub sendstart  'be done if there are long pauses
	wait (1000)      'between reads. eg minutes or hours
	'===================================================

   DO  
        
        c = c + 1          'Loop counter 
        gosub sendstart
       
        a(0) = Pulsin(7,1)  'We don't don't use this bit 
                                    'but we need to read it  

        '===== Read Humidity Bits =========
	   humidity = 0
           for i = 7 downto 0  
                 a(i) = pulsin (7,1)   
                  if a(i) < 30  then       'Bit is a "0" 
	              a(i)= 0   
                  else if a(i) >= 60 then  'Bit is a "1" 
                      a(i) = 1   
                 end if
            a(i) = a(i) << i   'shift bit  
            humidity = humidity + a(i)   'SUM the bits
        next i 
        '=================================
	
        '=================================
        for i = 7 downto 0         'Dummy reads. 
     	   a(i) = pulsin (7,1)      'With DHT22 we would read
        next i                          'Decimal bits here 
        '===================================
	
        '====== Read Temperature bits ======
           temperature = 0
           for i = 7 downto 0  
                a(i) = pulsin (7,1)   
               if a(i) < 30  then       'Bit is a "0"
                   a(i)= 0   
              else if a(i) >= 60 then  'Bit is a "1"  
                   a(i) = 1   
              end if
           a(i) = a(i) << i       
           temperature = temperature + a(i)    'SUM the bits
        next i 
        '====================================
         
        '=====================================
          for i = 7 downto 0           'Dummy reads. 
                a(i) = pulsin (7,1)     'With DHT22 we would read
          next i                             'Decimal bits here 
        '===================================
        
        '===== Checksum =====================
           checksum = 0
           for i = 7 downto 0  
	        a(i) = pulsin (7,1)   
                if a(i) < 30  then       'Bit is a "0" 
	            a(i) = 0   
                else if a(i) >= 60 then  'Bit is a "1" 
                     a(i) = 1   
               end if
               a(i) = a(i) << i 
              checksum = checksum + a(i)   'SUM
        next i 
        '=======================================
        
        tempvar = temperature + humidity 
        Print "Loop ";c
	Print "Humidity = ";humidity;chr(37);"RH"
	Print "Temperature = ";temperature;chr(186);"C" 
        if tempvar = checksum then
           print "Checksum Good"        
        else 
           print "Checksum Bad"
        end if 
        Print  'Blank line
	wait(2000)  'Minimum of 1000ms between reads 
	
   LOOP

SendStart:
       WAIT(20)
       PULSOUT(7,18000)    '18ms "low" start signal to DHT11
       Input(7)                     'Pullup to high    
return
Attachments
Schematic
Schematic
BASIC-DHT11.JPG (13.64 KiB) Viewed 23930 times
Last edited by Goey on Fri Oct 26, 2012 6:19 pm, edited 1 time in total.


Goey

Pompey2
Posts: 16
Joined: Fri Oct 19, 2012 12:10 pm

Re: DHT11 Humidity & Temperature Sensor

Post by Pompey2 »

Nice work Goey.

Just a couple of observations.

Code: Select all

for i = 7 downto 0 
                 a(i) = pulsin (7,1)   
                  if a(i) < 30  then       'Bit is a "0"
                 a(i)= 0   
                  else if a(i) >= 60 then  'Bit is a "1"
                      a(i) = 1   
                 end if
I know it shouldn't happen but IF a(i) is in the range 30-59 then you'll leave a(i) unchanged from its previous value. I guess the checksum would catch it anyway.

Code: Select all

  I/O(7) = 1


Think this should be IO(7) = 1
As written it doesn't generate a syntax error, but I'm not sure what is does!!

I'll let someone else chip in for the bit-twiddling, gosubs and other tips.

basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: DHT11 Humidity & Temperature Sensor

Post by basicchip »

I'll have to look into why I/O(0)=1 was not flagged as an error in the compiler.

Does the DHT11 conform to the Dallas one-wire protocol? If it does, try the one wire library ONEWIRE.bas

Goey
Posts: 6
Joined: Fri Oct 19, 2012 12:48 pm
Location: Austin,TX

Re: DHT11 Humidity & Temperature Sensor

Post by Goey »

basicchip wrote:I'll have to look into why I/O(0)=1 was not flagged as an error in the compiler.

Does the DHT11 conform to the Dallas one-wire protocol? If it does, try the one wire library ONEWIRE.bas
That I/0(7) = 1 was due to me editing the code after the Bulletin Board reformated the margins/ spacing in the post preview.
It does not like mixed tabs and spaces. I simply retyped it wrong when editing. So don't waste any time looking into the compiler

The DHT11/22 does not conform to Dallas one-wire.

Actually If a(i) is in the range of 30us to 67us or also less than about 23us then something is probably wrong. I allowed some leeway in case of deviations from device to device but I only have one to mess with. It might be prudent for anyone using this code in an actual application to add another If or two that causes a restart of the read loop if the data is out of range. There is 50us time between bits which should be ample for the necessary added code to flag an error.

I am accustomed to Programming Picaxe Chips where even at 64Mhz each command takes approx 30us to 60us to process.
Last edited by Goey on Thu Oct 25, 2012 7:58 pm, edited 1 time in total.

Pompey2
Posts: 16
Joined: Fri Oct 19, 2012 12:10 pm

Re: DHT11 Humidity & Temperature Sensor

Post by Pompey2 »

That I/0(7) = 1 was due to me editing the code after the Bulletin Board reformated the margins/ spacing in the post preview.
It does not like mixed tabs and spaces. I simply retyped it wrong when editing. So don't waste any time looking into the compiler
But I compiled your code as posted - with the typo. So there IS a compiler issue....

basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: DHT11 Humidity & Temperature Sensor

Post by basicchip »

Yes there is a bug with I/O(3). A "feature" of recursive decent compilers is that you have to check for illegal code which in this case with the new compound operators not enough checks were added. That has been fixed and will get into the next release.

Goey
Posts: 6
Joined: Fri Oct 19, 2012 12:48 pm
Location: Austin,TX

Re: DHT11 Humidity & Temperature Sensor

Post by Goey »

That was fast ....

I'm not sure what I/0(x) actually does compared to IO(x) but when I remove the line altogether the code still works fine. This is because the pin was changed to an input and is pulled high after the DHT is done. So the DHT11 still sees the transition from high to low and the 18ms low start signal. So that line is, in effect, redundant and can be removed.

Bill
Goey

Pompey2
Posts: 16
Joined: Fri Oct 19, 2012 12:10 pm

Re: DHT11 Humidity & Temperature Sensor

Post by Pompey2 »

I'll let someone else chip in for the bit-twiddling, gosubs and other tips.
Some thoughts and hints sent in an email - Bill.
Not sure where they would fit with this new forum setup?

Goey
Posts: 6
Joined: Fri Oct 19, 2012 12:48 pm
Location: Austin,TX

Re: DHT11 Humidity & Temperature Sensor

Post by Goey »

I'm sad to report that the Basic Chip gave up the ghost. It was working fine with the DHT11 code and then
all of a sudden started drawing 800ma on the Vdd Pin (21). Supply was BK Precison 1621A set to 3.3v.

Bill
Goey

brucee
Site Admin
Posts: 33
Joined: Thu Dec 18, 2008 8:45 pm

Re: DHT11 Humidity & Temperature Sensor

Post by brucee »

I can't really tell you why any single IC fails, but it does happen.

In the past there have been many reasons I've found that to happen, but normally it doesn't get investigated until there is a pattern. When we started shipping BASICchips, NXP had already shipped over 30 thousand LPC1114 chips to the field. All the same silicon, but most in the TSSOP28 package. If there had been some defect, I would have heard about it, and NXP would be investigating.

Among reasons that chips can fail, include power supply surges, driving IOs with low impedance sources before the chip has power supplied and static electricity and infant mortality.

The industry has gotten very serious about static, while chips are hardened against it and different manufacturers have similar protections, they still take quite a few precautions on the factory floor. All people handling devices outside of packaging wear static control smocks and have a grounding strap around their wrist at the work bench.

Do hobbyists or even engineers at their bench do these things. Usually not. In my lab, all carpets have been removed, and I walk around on hardwood floors normally either barefoot or in socks, as I find less static buildup that way. I also have an exposed GND handy to touch before I start working on electronics, for instance most PC cases are grounded, or the metal stand of the microscope at the soldering station.

Post Reply