DHT22 not giving correct results
in Propeller 1
Hello all-
I'm using Jonnymac's jm_dht11_demo spin file with a dht22( I believe I read on a thread that his spin file will read both sensors) and am getting the results in the attached picture. I have a 10K resistor between power and signal. Does anyone have any ideas I can try?
Thanks
Shawn
I'm using Jonnymac's jm_dht11_demo spin file with a dht22( I believe I read on a thread that his spin file will read both sensors) and am getting the results in the attached picture. I have a 10K resistor between power and signal. Does anyone have any ideas I can try?
Thanks
Shawn
Comments
Edit- I modified his original program in the OBEX to suit my set up. Program attached.
'' SPECIAL VERSION FOR TESTING DUE TO VARIANCES IN SENSORS '' WTM 01/22/2016 '' ''****************************************** ''* Title: DHTnn_Test for DHT11_Object * ''* Modified from DHT21_Demo - * ''* Walter T. Mosscrop 2014 * ''* Author: Gregg Erickson 2012 * ''* See MIT License for Related Copyright * ''* See end of file and objects for . * ''* related copyrights and terms of use * ''* This object draws upon code from other* ''* OBEX objects such as servoinput.spin * ''* and DHT C++ from Adafruit Industries * ''* * ''* The object reads the temperature and * ''* humidity from an DHT11 Sensor * ''* using a unique 1-wire serial protocol * ''* with 5 byte packets where 0s are 26uS * ''* long and 1s are 70uS. * ''* * ''* The object automatically returns the * ''* temperature and humidiy to variables * ''* memory every few seconds as Deg C and * ''* relative percent respectively. It also* ''* return an error byte where true means * ''* the data received had correct parity * ''* * ''****************************************** CON _clkmode = xtal1 + pll16x 'Set clock speed and mode _xinfreq = 5_000_000 VAR long ReadStatus ' Status of available data ' -1 = updating values, try again ' 0 = bad data (parity error) ' 1 = good data long Temperature ' Calculated temperature in degrees C as float long Humidity ' Calculated humidity in % relative humidity as float OBJ serial : "FullDuplexSerial" DHT : "DHTnn_Object" f : "FloatMath" fp : "FloatString" Pub Main serial.start(31, 30, 0, 38_400) ' Initialize Serial Communication to Serial Terminal fp.SetPrecision(4) ' pin, device (11 or 22), temperature address, humidity address, read status address DHT.StartDHTnn(15 , 12 , @Temperature , @Humidity , @ReadStatus) repeat readStatus := -1 DHT.DHTnn_Read repeat until ReadStatus => 0 if ReadStatus == 1 temperature := f.FMul(temperature, 9.0) ' Convert °C to °F temperature := f.FDiv(temperature, 5.0) temperature := f.FAdd(temperature, 32.0) serial.str(fp.FloatToString(temperature)) serial.str(@fahr) serial.str(@space) serial.str(fp.FloatToString(humidity)) serial.str(@percent) serial.str(@crlf) else serial.str(@invalid) serial.str(@crlf) Dat Space byte " ",0 ' Strings for use in printing Percent byte " %",0 Fahr byte " F",0 Valid byte " Read OK",0 Invalid byte " Read Error",0 crlf byte $0A, $0D, 0 {{ ┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ │ TERMS OF USE: MIT License │ ├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤ │Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation │ │files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, │ │modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software│ │is furnished to do so, subject to the following conditions: │ │ │ │The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.│ │ │ │THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE │ │WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR │ │COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │ │ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ └──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ }}
That is weird I have 12 instead of 22 and its working. I changed it to 22 with the same results though.
{HELP DHT@ ( pin -- 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
}
So if you are using pin 15 then just type "15 DHT<cr>" and when you print the first result returned then that's the temperature in Celius*10 and the second item is the humidty *10.
I just connected up a DHT22 to one of my boards on P16 and this is all I did to test it
16 DHT ok . 269 ok . 401 ok 16 DHT . 268 ok . 395 ok 16 DHT . SPACE . 266 476 ok
So the first time through I read 269 which is 26.9'C and 401 which is 40.1% RH. Finally after huffing on the sensor I get a reading of 26.6'C and 47.6% RH.
Not just happy with that I do a quick one-liner and place a cube of ice over the sensor resting on tissue paper:
BEGIN CR 16 DHT . SPACE . 3 seconds KEY UNTIL
This is what it showed, the temperature dropping and the humidity rising, as it should.265 655 178 340 175 341 173 343 172 344 170 346 169 349 167 352 166 352 164 354 163 356 162 358 161 360 160 362 159 364 158 365 157 367 156 368 155 371 154 372 153 376 153 378 151 379 ok
After a couple of minutes:
121 454 119 454 119 456 118 456 118 457 117 458 117 459 117 459 ok
closely based in @Peter's DHT2 code.
pub DHT11 ( 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 20 ms DUP FLOAT --- Generate reset pulse DHTBIT DROP --- wait for start of response DHTBIT 3 / 2* htref W! --- use start bit as reference DHTBYTE DHTBYTE DROP --- 2 bytes humidity reading DHTBYTE DHTBYTE DROP --- 2 bytes temperature reading htck C@ DHTBYTE = --- if checksum does not match then use last reading IF htsav W! rh W! ELSE 2DROP THEN --- latch readings if valid THEN DROP rh W@ htsav W@ --- return with latest valid result ; pub .DHT11 ( pin -- ) DHT11 ." Temp = " . ." Degrees Celcius RH = " . ." %" CR ; pub LOOPDHT11 ( pin -- ) BEGIN DUP .DHT11 5 seconds KEY UNTIL DROP ;
Other than 20ms instead of 1ms reset pulse there isn't any real difference except you are only using 8-bits. I've checked the datasheet for the DHT11 and it seems that the data format is the same so I've just allowed for a longer reset pulse for a DHT11 as it doesn't make much sense to have a different driver just for this one small detail.
DHT11
5. Communication Process: Serial Interface (Single-Wire Two-Way) Single-bus data format is used for communication and synchronization between MCU and DHT11 sensor. One communication process is about 4ms. Data consists of decimal and integral parts. A complete data transmission is 40bit, and the sensor sends higher data bit first. Data format: 8bit integral RH data + 8bit decimal RH data + 8bit integral T data + 8bit decimal T data + 8bit check sum. If the data transmission is right, the check-sum should be the last 8bit of "8bit integral RH data + 8bit decimal RH data + 8bit integral T data + 8bit decimal T data".
DHT22
DHT22 send out higher data bit firstly! DATA=8 bit integral RH data+8 bit decimal RH data+8 bit integral T data+8 bit decimal T data+8 bit check-sum If the data transmission is right, check-sum should be the last 8 bit of "8 bit integral RH data+8 bit decimal RH data+8 bit integral T data+8 bit decimal T data".
Edit: I'm using the latest tachyon and extend builds: tachyon 300_160804 and the extend in the dropbox.
24 DHT . SPACE . 229 474 ok
Try another I/O pin or DHT22 and always make sure you use a current limit resistor if running from 5V. I notice that many diagrams show a pullup but the one wire that the DHT22 uses is not the same as the "1-wire" bus.
Testing the DHT22 it shows that internally it has a 4k7 pullup to supply which means that the pullup they show in the datasheet is redundant. The DHT22 works directly from 3.3V so no current limit or pullup resistors are necessary.
Hint: If you need to post terminal output just copy and paste as code rather than screenshots unless it is something fancy.
btw, here's a simple routine to sample and display the results:
pub .DHT ( pin -- ) DHT SPACE 0 1 .DP PRINT" 'C @" 0 1 .DP PRINT" %rh " ;
and running it24 .DHT 21.3'C @46.6%rh ok
Just had a look at how they do it Arduino style and the code is a little more than one line plus it needs floats:
/* How to use the DHT-22 sensor with Arduino uno Temperature and humidity sensor More info: http://www.ardumotive.com/how-to-use-dht-22-sensor-en.html Dev: Michalis Vasilakis // Date: 1/7/2015 // www.ardumotive.com */ //Libraries #include <DHT.h>; //Constants #define DHTPIN 2 // what pin we're connected to #define DHTTYPE DHT22 // DHT 22 (AM2302) DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino //Variables int chk; float hum; //Stores humidity value float temp; //Stores temperature value void setup() { Serial.begin(9600); dht.begin(); } void loop() { //Read data and store it to variables hum and temp hum = dht.readHumidity(); temp= dht.readTemperature(); //Print temp and humidity values to serial monitor Serial.print("Humidity: "); Serial.print(hum); Serial.print(" %, Temp: "); Serial.print(temp); Serial.println(" Celsius");
24 .DHT 23.3'C @52.7%rh ok
Check the extra notes I previously added to my last post.
The data format is the same. But the DHT11 always reports the decimal bytes as zeroes (less accurate).
Peter- Just downloaded extend, then verified communication by hitting enter a couple times and getting "ok". Then hit reset on my PPDB, bang, no more communication with prop.
BTW, It is quite possible too with V3 JUNO that it may not work with older 32k EEPROM systems as it assumes all systems would have 64k which is the standard these days.
(I could put a test in there for this to prevent it trying to save ROMs to upper EEPROM if there is only 32k). (DONE)
BTW, It is quite possible too with V3 JUNO that it may not work with older 32k EEPROM systems as it assumes all systems would have 64k which is the standard these days.
(I could put a test in there for this to prevent it trying to save ROMs to upper EEPROM if there is only 32k). (DONE)[/quote]
Okay! The PPDB board I'm using only comes with 32K even new!! So I need to get a 64K DIP chip before I continue! Thanks Peter, it never would have occurred!
Okay! The PPDB board I'm using only comes with 32K even new!! So I need to get a 64K DIP chip before I continue! Thanks Peter, it never would have occurred!
[/quote]
But if you try again with the newer version of EXTEND it will detect the 32k EEPROM and not try to backup the ROMS which you aren't using anyway.
The one I modified immediately after mentioning this problem and then appending a (DONE). Mind you though I haven't checked it on a 32k system as I would have to dig one up plus I'm pretty sure it will work because one of the things that happens when EXTEND is loading is that it does a quick check to see if you have 32k or more so that it can optimize the page size for writing blocks of EEPROM and all I added to SAVEROMS was "ep 128 => 0EXIT" which effectively does an early exit if the page size is smaller as in the case of 32k EEPROMs.
this code says
ep 128 =
means 64k
so your line above would also 0EXIT for 64k EEPROMs ...
0 $8000 E! $8000 E@ 0 E@ <> IF 128 ELSE 64 THEN == ep pub .DEVICE ( adr -- ) SWITCH $A0 CASE ep 128 = IF PRINT" 64K" ELSE PRINT" 32K" THEN PRINT" BOOT EEPROM " BREAK
: SAVEROMS ep 128 => 0EXIT " ROMS" U@ @NAMES 3 ANDN ' TACHYON 3 ANDN
For 32k ep = 64 (the eeprom page size) so 64 128 => gives a false or 0 result and exits early.Testing this plus confirm my 64k EEPROM has an ep of 128.
64 128 => . 0 ok ep . 128 ok
sorry - the 0EXIT trap again ... :-(
0EXIT === NOT IF EXIT THEN
btw. I like the
" ROMS" U@
trick :-)
... now everybody else than you can find out, what this trick is ;-) and maybe learn s.th.
EDIT: wrong link replaced
aliexpress.com/item/Free-Shipping-10PCS-24C512-Encapsulation-DIP-2-wire-Serial-EEPROM/32450744005.html?spm=2114.30010308.3.76.4H5Oe4&ws_ab_test=searchweb201556_7,searchweb201602_2_10057_10056_10037_10055_10049_10044_10043_10059_10033_10058_10032_10017_10041_10060_10042_10061_10062_10064,searchweb201603_1&btsid=1134ca8f-7062-4b16-9418-a604028f143d
for this price you can take 5 or 10 or 20 ;-)
upgrade the PPDB and have lot's of spares ...
Mouser. Drop in replacement:
http://www.mouser.com/ProductDetail/Microchip-Technology/24LC512-I-PG/?qs=sGAEpiMZZMuVhdAcoizlRSvfTHyGQYYpbLAyXVdZPQc=
Okay peter solved my problems with communications after reset - thank you! Now everytime I reset or hit ctrl-b I get
VER: Propeller .:.:--TACHYON--:.:. Forth V3.0 JUNO 300160804.1730 PCB: UNKNOWN (0) FREQ: 80MHZ (PLLEN OSCEN XTAL1 PLL16X) NAMES: $5AFE...74D0 for 6,610 bytes (+3,981) CODE: $0930...3755 for 11,813 bytes (+8,085) RAM: 9,129 bytes free BUILD: FIRMWARE BUILD DATE 160804:180000 BOOTS: 4 runtime 41 BOOT: EXTEND.boot MODULES LOADED: 17C0: EXTEND.fth Primary extensions to TACHYON kernel - 160809-1840 ROMS ---------------------------------------------------------------- ok ok pub .DHT ( pin -- ) DHT SPACE 0 1 .DP PRINT" 'C @" 0 1 .DP PRINT" %rh " ; ok ok ok 14 .DHT
But when I enter the pub .DHT and tachyon says okay, I enter 14 .DHT (I moved the data line to 14 from 15) I get no response and the propeller hangs. I have this hooked o 3.3V so I have no 1k resistor.
Sweet! Thanks for the link
Don't use that part, it is 64 k bit EEPROM, not 64 k BYTE. Normally k bits are written as kb and k bytes as kB (or KB) but beware the typos accidental or not. The link though that Publison gave is correct although there are many others too. A 64KB part will normally have a 512 as part of its part number to indicate 512k bits or 64k x8 whereas the old standard 32kB was a 24LC256 or 256 k bit EEPROM.
But as I said before you don't need 64kB normally, it is only if you going to be using those "ROMS" or extend Tachyon to the point with FAT32 and perhaps Ethernet that you need the upper 32kB for the dictionary.
btw, the problem with the DHT22 could be a faulty DHT and the code hangs waiting for a response. Do a "14 DHT? PRINT" to check the DHT22 which will return with a -1 for yes and a 0 for no. (I favor using the PRINT alias in place of the dot symbol since the dot gets lost or may be interpreted as a full stop etc). Default radix should be decimal but just in case force it with a # prefix like this: #14 or #P14
If it looks faulty then just post a photo of the the way the sensor is connected up.
1 VDD
2 DATA
3
4 GND
Ahem, I uh found the problem with my setup. During playing I removed the wire from the 3.3v to the breadboard *ahem*
Here is the ouput now:
29.5'C @56.5%rh 29.6'C @55.4%rh 29.5'C @54.4%rh 29.4'C @53.9%rh 29.3'C @53.7%rh 29.2'C @53.5%rh 29.2'C @53.7%rh 29.0'C @53.6%rh 29.0'C @53.8%rh 28.8'C @53.8%rh 28.8'C @53.8%rh 28.6'C @53.9%rh 28.6'C @54.1%rh 28.5'C @54.1%rh 28.4'C @54.0%rh ok
Thanks Peter for all the patience!! lol so as a learning experiment how would I convert the Celsius to Fahrenheit in the one liner example?
BTW, its good to know my sensor is good! Which was the original point of this thread.
Take 100'C and convert to F interactively
100 9 * 5 / 32 + . 212
So you see if you multiply by 9 divide by 5 and then add 32 you have F. Make this a function.pub C>F ( c -- f ) 9 * 5 / 32 + ;
Playing this is what I'm getting:
Propeller .:.:--TACHYON--:.:. Forth V3.0 JUNO 300160804.1730 VER: Propeller .:.:--TACHYON--:.:. Forth V3.0 JUNO 300160804.1730 PCB: UNKNOWN (0) FREQ: 80MHZ (PLLEN OSCEN XTAL1 PLL16X) NAMES: $5AFE...74D0 for 6,610 bytes (+3,981) CODE: $0930...3761 for 11,825 bytes (+8,097) RAM: 9,117 bytes free BUILD: FIRMWARE BUILD DATE 160804:180000 BOOTS: 12 runtime 41 BOOT: EXTEND.boot MODULES LOADED: 17C0: EXTEND.fth Primary extensions to TACHYON kernel - 160809-1840 ROMS ---------------------------------------------------------------- pub c>f 9 * 5 / 320 + ; ok pub .dht dht space c>f 0 1 .dp print" 'F @" 0 1 .dp print" %rh " ; ok ok 14 ok begin cr 14 .dht 3 seconds key until 73.0'F @66.6%rh 73.7'F @66.6%rh 73.7'F @66.6%rh 73.7'F @66.5%rh 73.7'F @66.5%rh 73.7'F @66.5%rh 73.7'F @68.3%rh 74.1'F @87.2%rh 74.6'F @92.3%rh 75.2'F @93.5%rh 75.7'F @94.0%rh 76.2'F @94.0%rh 76.8'F @93.3%rh 77.3'F @81.5%rh 77.5'F @72.2%rh 77.9'F @66.5%rh 78.0'F @64.6%rh 78.0'F @63.6%rh 78.2'F @62.8%rh 78.2'F @62.3%rh ok
Does tachyon have graphing capabilities? This is pretty cool!