Shop OBEX P1 Docs P2 Docs Learn Events
DHT11 Temp/Humidity sensor help — Parallax Forums

DHT11 Temp/Humidity sensor help

Has anybody had any luck with the DHT11 obex example code and diver? I kept thinking my dht11 sensors were no good, as I could not get them to read consistently with the example. About 9 of 10 resets on any of my Quickstart boards would show Read Error, and when it would show a temp and humidity, it would not change unless I reset the board. I got an Arduino Uno the other day and tried the sensors on it with the Adafruit library, and the sensors reads perfect. I want to use it on a Propeller though.

So, what is wrong with this code? The way it is written, I can't make heads nor tails of it (no surprise, I'm not that good of a programmer). I tried different pins, same results.
Any help I can get would be greatly appreciated!

Comments

  • (no surprise, I'm not that good of a programmer)
    Tip: Become a better programmer so you don't have to depend of code written by others -- including me! :)

    I wrote my own DHT11 driver; it's attached in a demo.
  • Here is pointer to a thread I started some time ago.
  • JonnyMac, my hero!
    Works nice, object code I can read and really cool display too!
    thanks again!

  • JonnyMac, my hero!

    I rule!
    And thank you for spelling my name correctly!

    I'm working of a PASM version that can report a bad sensor. In this version if the readings count doesn't change then you know something is amiss.
  • Too bad we aren't "voting members", we'd probably be getting JonnyMac "SWAG bags" ;)
  • My new manager is working hard to get me out so that you can see me on TV as often as you see me in the forums!
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2016-01-21 02:38
    Just for reference this is what I use with my DHT22/DHT11 in Tachyon Forth. It's not really an object but more of a subroutine that's called directly from the main program using DHT@ which returns the temperature and humidity on the stack. If the device is polled too soon it returns with the last latched value. At present I have this set to 2 seconds but I have seen some reference that says this only needs to be 0.5 seconds?

    The main code including variables etc is just 59 bytes
    --- Define DHTPIN - to change at runtime just <mypin> ' DHTPIN 1+ !
    #P14	== DHTPIN
    
    LONG httime						--- last time DHT was polled
    WORD htref,rh,dht					--- timing reference and last valid readings
    BYTE htck						--- checksum 
    
    --- Read a single bit and return with the cnt value
    pri DHTBIT ( -- cnt )	
    	(WAITPEQ)					--- wait for it to go high
    	0 COGREG@					--- get captured CNT
    	(WAITPNE)					--- until it goes low
    	0 COGREG@ SWAP -				--- calculate timing of high period
    	;
    
    --- Read in 8 bits using the intial start pulse as a ref in htref for timing
    pri DHTBYTE ( -- byte )
    	0 8 FOR 2* DHTBIT htref W@ > 1 AND OR NEXT 
    	DUP htck C+!
    	;
    
    {HELP DHT@ ( -- rh temp | -1 )
    Read the relative humidity and temperature from the DHT22
    Return with rh and temp in Celsius
    If an error occurred in the checksum then return last valid reading
    If the sensor is polled more than once every 2 seconds then use last valid readings
    Typical acquisition time ~6ms
    }
    pub DHT@ ( -- rh temp )
     	runtime @ httime @ - 2000 =>  			--- don't poll more than once every 2 seconds
     	IF
     	  runtime @ httime !				--- mark runtime when this was polled
     	  htck C~
    	  DHTPIN MASK 3 COGREG! 
    	  DHTPIN LOW 1 ms DHTPIN FLOAT			--- Generate reset pulse
    	  DHTBIT DROP					--- wait for start of response
    	  DHTBIT 3 / 2* htref W!			--- use start bit as reference
    	  DHTBYTE 8 SHL DHTBYTE +			--- 2 bytes humidity reading 
    	  DHTBYTE 8 SHL DHTBYTE +			--- 2 bytes temperature reading 
    	  htck C@ DHTBYTE = 				--- if checksum does not match then use last reading
     	    IF dht W! rh W! ELSE 2DROP THEN		--- latch readings if valid
      	THEN
    	rh W@ dht W@					--- return with latest valid result
    	;
    

    A version of DHT@ that lets you pass the pin number as a parameter:
    pub DHT@ ( pin -- rh temp )
     	runtime @ httime @ - 2000 =>  			--- don't poll more than once every 2 seconds
     	IF
     	  runtime @ httime !				--- mark runtime when this was polled
     	  htck C~
    	  DUP MASK 3 COGREG! 
    	  DUP LOW 1 ms FLOAT				--- Generate reset pulse
    	  DHTBIT DROP					--- wait for start of response
    	  DHTBIT 3 / 2* htref W!			--- use start bit as reference
    	  DHTBYTE 8 SHL DHTBYTE +			--- 2 bytes humidity reading 
    	  DHTBYTE 8 SHL DHTBYTE +			--- 2 bytes temperature reading 
    	  htck C@ DHTBYTE = 				--- if checksum does not match then use last reading
     	    IF dht W! rh W! ELSE 2DROP THEN		--- latch readings if valid
      	THEN
    	DROP rh W@ dht W@				--- return with latest valid result
    	;
    
  • Geez, now I have to get some dht11's. Are the dht22's worth the extra price for hobby work?
  • What's a burger and fries worth? More than a DHT22 that's for sure. If you are actually trying to measure temperature and humidity properly the DHT22 has a much wider range and is far more accurate. Skip all the junk and just go for the DHT22.
  • The SHT21 (or knockoff HTU21D) are also pretty nice high-precision T/RH measurement devices. Also simple I2C :smile:
  • geo_leeman wrote: »
    The SHT21 (or knockoff HTU21D) are also pretty nice high-precision T/RH measurement devices. Also simple I2C :smile:

    True, and I prefer I2C normally, but not when it comes to distance as the single wire sensor can be run up to 30 metres away which is more usual for these types of sensors rather than to have them close to the PCB.
  • Well I finally got a dht22, and time to hook it up. Is it possible to get a bad one? With your driver Jonnymac, all I get back is 32 deg F, and 0% humidity. Tachyon just hangs, as does another driver I found under search. I have a 4.7K resistor between pins 1 and 2.

    Thanks
  • For what it's worth, I rewrote my code for Roxanna77. The timing routines are now much more accurate (PASM instead of Spin) and I believe it will work properly with the DHT11.

    Unfortunately, she hasn't been able to test my new version.

    I'd be happy to share the new version with anyone who's interested in verifying my fix...

    Thanks,

    Walter
Sign In or Register to comment.