Using DHT22 - Please Help

ShadowGrass

New Member
I am attempting to figure out how to use this sensor, and I am not really getting anywhere. So, either no one is using it, or I am the only one having trouble with it. I am very new, so please excuse the questions if they are remedial. I am trying to learn, and I have come a long way, but having trouble here.

1. Sensor sends 40 bits. Ok, how do I get what I need from it? I need to take the first 16 bits for temp, and the second 16 for rh. Do I use a loop to read 8 bits at a time? If so, how do I avoid reading the same beginning 8 bits over and over.

2. How do I grab bits 17-32 for rh?

I think I can figure out the math if I can figure out how to separate the data into usable chunks.

Also, it says in the datasheet to set the pin low then wait minimum 10ms. Does this mean the input (DATA) pin? So if I have it connected to input 1, would that be something like:

low 1
Pause 10
high 2
Pause 10
readtemp 1, b1 ...????
'how do I get all 40 bits?
'b1 should be first 8 + second 8
'b2 should be 3rd 8 and 4th 8
'b3 should be last 8 (checksum)
'b4 should 1st 8, plus 2nd 8, plus 3rd 8, plus 4th 8 = last 8.

Then I have a 16 bit binary number that I need to convert, still looking into how to do this.

As you can see I am seriously confused. Any help AT ALL would be greatly appreciated.
 

hippy

Technical Support
Staff member
To echo inglewoodepete; please supply a link to the datasheet, or at least describe what the sensor is; forum members can't help if they don't know what it is they are helping with.

If it's this one ( the RHT03 is said to have the same protocol as DHT22 ) ...

http://www.humiditycn.com/pic/201111310333367286.pdf

"MaxDetect 1-wire bus is used for communication between MCU and RHT03. (MaxDetect 1-wire bus is specially designed by MaxDetect Technology Co., Ltd., it's different fromMaxim/Dallas 1-wire bus, so it's incompatible with Dallas 1-wire bus.)"

If that's true then you probably cannot use READTEMP nor any OWIN or OWOUT commands. As it's a very high-speed signal I don't think it can be used with the PICAXE.
 

Dippy

Moderator
If I were you I'd contact Thomas Liu and ask him for some code examples in BASIC and ask if PICAXE is fast enough.

The only Data Sheet I can find is this:-
http://www.sparkfun.com/datasheets/Sensors/Temperature/DHT22.pdf

The example code on the Sparkfun site may give you a clue:-
http://www.sparkfun.com/datasheets/Sensors/Temperature/DHT22.doc

It's poorly written for the Western market.
If what has been said is even partially true and/or your coding experience is limited then you are going to struggle.

Maybe if you wait someone may have done similar. In the meantime look around for a 'friendlier' sensor.
Lesson learnt: if all this is new to you then check here before you buy.
 

ShadowGrass

New Member
Lesson Learned

Ok, so with my very very limited knowledge, I probably cannot get that to work, or it cannot work with a Picaxe. So, now i am looking for one that will, but I am trying to find one that is easy for a newbie like me to use. What about an HS1101? https://www.parallax.com/Portals/0/Downloads/docs/prod/sens/27920_HS1101-Datasheet.pdf

This show a 555 timer circuit, I don't think I want to get involved in that. I using a 8m chip, and am looking for something EASY. Everything seems to need some sort of calibration, or is for ac. There is also the Honeywell HH-4030, http://www.sparkfun.com/datasheets/Sensors/Weather/SEN-09569-HIH-4030-datasheet.pdf, which is significantly more money. I did a search to see what people are using most commonly and I am not coming up with much.
 

Dippy

Moderator
Give us a clue about your budget.
What is your maximum spend?
Time scale?

How much is that sensor from Stan's favourite Company?
HopeRF
http://www.hoperf.com/upfile/HH10D.pdf
there are articles telling you how to use it.
http://www.siliconchip.com.au/cms/A_111451/article.html
and Stan will love you forvever if you use Hope stuff.
In fact, he'll probbaly do it for you and wash your car if you do ;)

All that help is worth money , so even if the device costs an extra buck or two then the FREE design is good huh?

The cheaper capacitive sensors need a little thought.
The delta-C is not huge but you have to measure it. See Data Sheets.
A 555 circuit uses it in the capacitive leg. You can then measure the change in pulse width.
You could incorporate it into a tuned oscillator circuit and do similar , but you'd probably have stability problem.

You MAY be able to do an RC measurement circuit using PICAXE I/O charge/discharge switching. Like a Stamp. Dunno. It may be a litle iffy at those levels of capacitance.


It would probably be a good idea to indicate your skill level so that people can adjust their reply level. Is suspect all this is new to you?

Have a search in one of the big suppliers for Data Sheets and prices.
e.g. Farnell , RS Components , Digikey etc.
 
The DHT is a great temp and humidity sensior at a low price. I too hoped and tried to interface it with a picaxe 18M2.

It is started with a low pulse against the bus pullup resistor of around 80 usecs which I did OK with Pulse out, then it responds after gap of around 80usec with a low going pulse of around 8o usecs then follows ths with 40 low going data pulses (25 usec long for a 1 and 70 usec for a 0 ) each of these having an interpulse gap of around 80usecs. I used 41 pulsein's stored directy using @bptrinc into ram. It was easy to read the 1's and 1's in the memory but with the 18m2 running at 32 MHz the intepulse gap was not long enough to process the pulse ins so many were missed leaving only about 20 out of 41 getting through. Lowering the clock speed made it even worse.


A. here is the code and results:

thumscan01: setfreq m32 ; bump speed up - get short timeout 80msec if no responce.
high B.1
pause 100

bptr = 150
pulsout B.1, 4000 ; output read start pulse approx 5 msec low

pulsin B.1,1,@bptrinc ; sensor ack pulse

pulsin B.1,1,@bptrinc ; tries to collect 40 bits of data pulses (1.25 usecs)
pulsin B.1,1,@bptrinc ; pulse time count approx 17 for 1 and and 70 for 0
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc

pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc

pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc

pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc

pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc

setfreq m4
bptr = 150
for wb2 = 1 to 41
sertxd (#@bptrinc,COM)
next wb2
sertxd (RET,LFEED)
pause 10000
goto results
;
; here are the results of the above at Picaxe clock 32Mhz - about the half rate needed

;63,17,17,17,17,57,17,17,57,17,17,17,17,57,17,57,17,57,57,17,57,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;63,17,17,17,17,57,17,17,57,17,17,17,17,57,17,57,57,57,57,18,17,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;62,17,18,17,17,57,17,17,17,17,18,17,17,57,17,57,57,57,57,57,57,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;63,17,17,17,17,57,18,17,18,18,17,17,17,57,18,57,57,57,57,57,57,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,


; here are the results of the above at Pcaxe clock 16 Mhz - even less get sampled (times half that above );

;8,8,28,29,8,8,8,9,9,28,8,28,9,8,9,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;9,9,28,28,9,8,8,9,9,28,9,28,8,8,8,28,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;8,8,28,29,8,28,8,8,8,29,8,29,9,8,9,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;8,8,28,28,8,29,9,8,8,28,9,28,8,8,8,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,


B. I tried again using sertxd to trigger on the start of each pulse with a high baud rate where the recieved bits sat over the pulse to distinguish between 0the short (1) or long (0)


sertest:
setfreq M32
bptr = 150
high B.1
pause 100
pulsout B.1, 4000 ; output read start pulse approx 5 msec low
serin [10], B.1,N38400_32,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc


setfreq m4
bptr = 150
for wb2 = 1 to 41
sertxd (#@bptrinc,COM)
next wb2
sertxd (RET,LFEED)
pause 10000
goto results
;
;
;Here are the results at N38400 baud with top speed clock of 32 Mhz data = idling low waiting for start bit

;219,219,140,27,140,219,219,27,99,99,140,27,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;219,219,140,155,108,219,155,27,99,99,140,219,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;219,219,140,219,219,219,108,108,219,140,140,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;219,219,140,219,108,219,99,227,108,99,108,108,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

;
;here are the results at N38400 baud with reduced clock speed at 16Mhz

;182,153,205,54,76,75,166,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;182,153,213,55,77,74,107,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;182,153,148,183,171,75,170,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;183,153,149,182,170,41,205,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,



;
;Here are the results at T38400baud = Idle high
;
;146,146,78,206,114,146,146,146,114,146,206,78,206,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;146,146,78,206,146,146,114,114,206,114,78,114,254,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;146,146,78,206,206,146,146,146,114,146,206,78,206,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;146,146,78,206,206,146,146,146,114,206,206,78,242,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

;
;Here is T38400baud with the reduced clock speed down to 16MHz
;
;145,213,85,81,213,212,101,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;144,213,37,82,213,212,81,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;146,213,86,82,213,212,101,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;145,209,37,82,85,169,165,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

C. I did start trying Hserin but since it only has a 2 byte buffer it requiered to many instructions to process each on arrival


D. Andrew Hornblow tried to do this on the faster 20X2 but still with no luck.


E. If someone was really interested then probably the 20x2 witch can read a whole string of bytes in the background could possibly do it.

F. I have gone back to Thomas Li to see if their engineers could re-program the chip in it say to use the spare pin to optionally set a much longer interpulse gap ( say 500 usecs) or use a smarter protocol that makes it more universally usable, Oherwise their product is technically fantastic but practically unusable for Pcaxe users and the like.


G. If anyone has some smart solutions to overcome this that would be appreciated please.

Humidity is about the last thing we need to resolve for the sensor stations for our school rainforest ecosystems services value research program at http://www.whirinakirainforest.info/weather/explanation.htm
 
Last edited:

manie

Senior Member
Hi Shadowgrass
I use the Honeywell HIH4000 supplied by Tech-Supplies. A little pricey but it works a treat and is easy to incorporate. It hooks up to your ADC pin and is therefore EASY to read and calculate. Like Dippy always says, You get what you pay for. Hope you come right........
 

Pauldesign

Senior Member
Hey Peter Goldsbury

Welcome to the PICAXE forum

In future please paste your source code in the hash (#) sign found on the reply window toolbar. It save space and helps a lot when scrolling. ;)

e.g
Code:
A. here is the code and results:

thumscan01: setfreq m32 ; bump speed up - get short timeout 80msec if no responce.
high B.1
pause 100

bptr = 150
pulsout B.1, 4000 ; output read start pulse approx 5 msec low

pulsin B.1,1,@bptrinc ; sensor ack pulse

pulsin B.1,1,@bptrinc ; tries to collect 40 bits of data pulses (1.25 usecs)
pulsin B.1,1,@bptrinc ; pulse time count approx 17 for 1 and and 70 for 0
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc

pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc

pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc

pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc

pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc
pulsin B.1,1,@bptrinc

setfreq m4
bptr = 150
for wb2 = 1 to 41
sertxd (#@bptrinc,COM)
next wb2
sertxd (RET,LFEED)
pause 10000
goto results
;
; here are the results of the above at Picaxe clock 32Mhz - about the half rate needed

;63,17,17,17,17,57,17,17,57,17,17,17,17,57,17,57,1 7,57,57,17,57,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,
;63,17,17,17,17,57,17,17,57,17,17,17,17,57,17,57,5 7,57,57,18,17,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,
;62,17,18,17,17,57,17,17,17,17,18,17,17,57,17,57,5 7,57,57,57,57,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,
;63,17,17,17,17,57,18,17,18,18,17,17,17,57,18,57,5 7,57,57,57,57,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,


; here are the results of the above at Pcaxe clock 16 Mhz - even less get sampled (times half that above );

;8,8,28,29,8,8,8,9,9,28,8,28,9,8,9,8,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;9,9,28,28,9,8,8,9,9,28,9,28,8,8,8,28,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;8,8,28,29,8,28,8,8,8,29,8,29,9,8,9,9,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;8,8,28,28,8,29,9,8,8,28,9,28,8,8,8,8,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,


B. I tried again using sertxd to trigger on the start of each pulse with a high baud rate where the recieved bits sat over the pulse to distinguish between 0the short (1) or long (0)


sertest:
setfreq M32
bptr = 150
high B.1
pause 100
pulsout B.1, 4000 ; output read start pulse approx 5 msec low
serin [10], B.1,N38400_32,@bptrinc,@bptrinc,@bptrinc,@bptrinc, @bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptr inc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@ bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptri nc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@b ptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrin c,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bptrinc,@bp trinc,@bptrinc,@bptrinc


setfreq m4
bptr = 150
for wb2 = 1 to 41
sertxd (#@bptrinc,COM)
next wb2
sertxd (RET,LFEED)
pause 10000
goto results
;
;
;Here are the results at N38400 baud with top speed clock of 32 Mhz data = idling low waiting for start bit

;219,219,140,27,140,219,219,27,99,99,140,27,3,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,
;219,219,140,155,108,219,155,27,99,99,140,219,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,
;219,219,140,219,219,219,108,108,219,140,140,3,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,
;219,219,140,219,108,219,99,227,108,99,108,108,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,

;
;here are the results at N38400 baud with reduced clock speed at 16Mhz

;182,153,205,54,76,75,166,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;182,153,213,55,77,74,107,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;182,153,148,183,171,75,170,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;183,153,149,182,170,41,205,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,



;
;Here are the results at T38400baud = Idle high
;
;146,146,78,206,114,146,146,146,114,146,206,78,206 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,
;146,146,78,206,146,146,114,114,206,114,78,114,254 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,
;146,146,78,206,206,146,146,146,114,146,206,78,206 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,
;146,146,78,206,206,146,146,146,114,206,206,78,242 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,

;
;Here is T38400baud with the reduced clock speed down to 16MHz
;
;145,213,85,81,213,212,101,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;144,213,37,82,213,212,81,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;146,213,86,82,213,212,101,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
;145,209,37,82,85,169,165,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
Remember, PICAXE forumites needs that as chopstick to wrap the spaghetti codes otherwise they might take long to eat it, hence take long to assist u. :D
 
Top