![]() |
Forum Index : Microcontroller and PC projects : DHT11 Humidity Sensor on an F4 (or any MMBasic)
Page 1 of 3 ![]() ![]() |
|||||
Author | Message | ||||
Andrew_G Guru ![]() Joined: 18/10/2016 Location: AustraliaPosts: 871 |
Hi, Do any of you have DHT11 (humidity) sensors running on an F4 (or MMBasic for that matter)? I have my weather station sensors running on an F4. The DHT22 works well but I would like to use the newer DHT11 (Jaycar's XC-4520). I believe/understand that the same code should work for both the DHT22 and DHT11, ie page 97 of the manual: DIM FLOAT temp, humidity HUMID pin, temp, humidity PRINT "The temperature is" temp " and the humidity is" humidity All I get from the DHT11 is the error value of 1000 for both temperature and humidity. The DHT22 is fine. Both are running off 5V, no resistor, and I've tried various F4 pins (eg 15 and 97). There is a subtle difference in the pinouts but I believe I have that right. Any ideas? Andrew |
||||
CaptainBoing![]() Guru ![]() Joined: 07/09/2016 Location: United KingdomPosts: 2170 |
I have tried many times to get these things either A. working B. accurate - even ball-park I have seen others report that they are at best an "estimator" and seem to fade with time. I am done with them (and the dht22[?]) in favour of the I2C based HTU21D - much better device. With projects that require temperature only, I have even "gone back" to using bead thermistors which are very good and surprisingly accurate within a given range. The HTU21D software came from Tassy Jim, I think and the thermistor software I wrote myself after a couple of days research - you can find it here if you are interested in it. It's quite short and only requires the barest minimum of support circuitry and an analogue pin. EDIT: Confirmed Tassy Jim as the source for the HTU21D software... as with so many things https://www.thebackshed.com/forum/ViewTopic.php?TID=11937&PID=143600#143600 Edited 2021-05-25 20:56 by CaptainBoing |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6274 |
As far as I can tell, the only difference between the DHT22 and DTH11 is the start pulse. The DHT11 needs a long 18mS start pulse compared to the 1mS on the DHT22. That extra 17mS is a long time in micromite land... and I would try with the 5k pullup resistor. Jim VK7JH MMedit |
||||
Andrew_G Guru ![]() Joined: 18/10/2016 Location: AustraliaPosts: 871 |
Hi Jim, This is part of a project with BigMik (watch this space . . ). There is a 4K7 pullup {edit: - on Mik's board - I have tried it with and without the resistor}. I'm only using the standard HUMID command - how do I increase the start pulse? Hi to CaptainBoing too, I will look at the HTU21D too but I'm keen to get the DHT11 going too. Cheers, Andrew Edited 2021-05-26 08:24 by Andrew_G |
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1992 |
I'm with the Captain, I never got the DHT11 to work the DHT22 was easy but they all failed after a while, probably not genuine and I could never find a 'definately genuine' one. The HTU21D all work and seem to be very accurate. I have an indoor and outdoor on my weather station and they have been running for about 2 years. "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6274 |
Take the code from the source and bundle it into a cfunction with the start pulse increased as required. I wouldn't like the source changed because that would have huge penalty on the performance for DTH22 users unless there was an extra parameter to indicate the DHT11. I guess, with a bit of imagination you could tie two pins together. The second pin configured as open collector and use it to pull the line low for 17.5 ms 17 ms after you start that pulse, start the HUMID command. This assumes that the firmware isn't worried about the data line being low when the command starts. I haven't checked that. It looks like there is a 'new' DHT11 but I don't think the start pulse time has changed. https://www.seeedstudio.com/blog/2020/04/20/dht11-vs-dht22-am2302-which-temperature-humidity-sensor-should-you-use/ Jim VK7JH MMedit |
||||
Andrew_G Guru ![]() Joined: 18/10/2016 Location: AustraliaPosts: 871 |
Thanks Palcal - I'll get a brace of HTU21D. Jim, thanks too. I'm not into writing cfunctions but I will try the two-pin idea. The good news of sorts is that I appear to have the "new" version (but that doesn't help it work) Thanks, Andrew |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6274 |
You should be able to bit-bash it on the F4 using the ADC commands. I might have a play (with a DHT22) when I get back from an appointment at the hospital. Jim VK7JH MMedit |
||||
palcal![]() Guru ![]() Joined: 12/10/2011 Location: AustraliaPosts: 1992 |
A brace, reminds me of when I was a boy and Mum would buy a brace of rabbits off the Rabbito who came around on his horse and cart. "It is better to be ignorant and ask a stupid question than to be plain Stupid and not ask at all" |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6274 |
Being the son of a builder, my first thought was a 'brace and bit'. Back to the issue, with further thought I think that the F4 can do a better job of reading the rogue DTH using interrupts and TIMER() to record the transitions. We need to be able to find a 40uS difference between 0's and 1's I will play (some time) Jim VK7JH MMedit |
||||
Andrew_G Guru ![]() Joined: 18/10/2016 Location: AustraliaPosts: 871 |
Thanks Jim, "In the fulness of time" will be great but it has annoyed me that it says its the same but its not. Cheers, Andrew (I remember the horse and cart milko, knife sharpener and bread men. The horses knew which houses to stop at which ones to skip. Arrh, the good old days) |
||||
phil99![]() Guru ![]() Joined: 11/02/2018 Location: AustraliaPosts: 2632 |
Thought it would be clever to to try TassyJim's two pin method with one pin, but no go. > for n=15 to 20 step 0.1 : SETPIN PE2, dout, oc, pu : pin(PE2)=0 : pause n : SETPIN PE2,off :HUMID PE2, temp, humidity :? n, temp, humidity :SETPIN PE2,off :next 15 1000 1000 15.1 1000 1000 15.2 1000 1000 15.3 1000 1000 15.4 1000 1000 15.5 1000 1000 15.6 1000 1000 15.7 1000 1000 15.8 1000 1000 15.9 1000 1000 16 1000 1000 16.1 1000 1000 16.2 1000 1000 16.3 1000 1000 16.4 1000 1000 16.5 1000 1000 16.6 1000 1000 16.7 1000 1000 16.8 1000 1000 16.9 1000 1000 17 1000 1000 17.1 1000 1000 17.2 1000 1000 17.3 1000 1000 17.4 1000 1000 17.5 1000 1000 17.6 1000 1000 17.7 1000 1000 17.8 1000 1000 17.9 1000 1000 18 1000 1000 18.1 1000 1000 18.2 1000 1000 18.3 1000 1000 18.4 1000 1000 18.5 1000 1000 18.6 1000 1000 18.7 1000 1000 18.8 1000 1000 18.9 1000 1000 19 1000 1000 19.1 1000 1000 19.2 1000 1000 19.3 1000 1000 19.4 1000 1000 19.5 1000 1000 19.6 1000 1000 19.7 1000 1000 19.8 1000 1000 19.9 1000 1000 > |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6274 |
You have to start the long pulse as a background pulse and start the HUMID just before the long pulse ends. Jim VK7JH MMedit |
||||
phil99![]() Guru ![]() Joined: 11/02/2018 Location: AustraliaPosts: 2632 |
Me thick, not get 2 pins work neither! Must be bed time. SETPIN PE6,dout,oc,pu for n=15 to 20 step 0.1 : pin(PE6)=1 : pause 200 : pulse PE6,n : HUMID PE2,temp,humidity :? n, temp, humidity : next 15 1000 1000 15.1 1000 1000 15.2 1000 1000 15.3 1000 1000 15.4 1000 1000 15.5 1000 1000 15.6 1000 1000 15.7 1000 1000 15.8 1000 1000 15.9 1000 1000 16 1000 1000 16.1 1000 1000 16.2 1000 1000 16.3 1000 1000 16.4 1000 1000 16.5 1000 1000 16.6 1000 1000 16.7 1000 1000 16.8 1000 1000 16.9 1000 1000 17 1000 1000 17.1 1000 1000 17.2 1000 1000 17.3 1000 1000 17.4 1000 1000 17.5 1000 1000 17.6 1000 1000 17.7 1000 1000 17.8 1000 1000 17.9 1000 1000 18 1000 1000 18.1 1000 1000 18.2 1000 1000 18.3 1000 1000 18.4 1000 1000 18.5 1000 1000 18.6 1000 1000 18.7 1000 1000 18.8 1000 1000 18.9 1000 1000 19 1000 1000 19.1 1000 1000 19.2 1000 1000 19.3 1000 1000 19.4 1000 1000 19.5 1000 1000 19.6 1000 1000 19.7 1000 1000 19.8 1000 1000 19.9 1000 1000 > |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6274 |
I don't think there is much point trying any times apart from 18mS This is the sequence required (If it is going to work at all.) The TEMP, HUMID command starts just before the pulse has ended. I am assuming that a 18mS pulse happens in the background - haven't checked that yet. Provided interrupts are serviced fast enough, I think interrupt timing would be the way to go Jim Edited 2021-05-27 08:53 by TassyJim VK7JH MMedit |
||||
Andrew_G Guru ![]() Joined: 18/10/2016 Location: AustraliaPosts: 871 |
Sorry Jim but I can't get that to work (even with changing 'DH22' to 'HUMID'). My DHT22 took a few seconds to "warm up" so I've also tried it in a loop with 5 second delay between readings - still no joy. Andrew |
||||
phil99![]() Guru ![]() Joined: 11/02/2018 Location: AustraliaPosts: 2632 |
"This is the sequence required (If it is going to work at all.)" Yes, I forgot about the PAUSE 17.5 but still no joy with the DHT11. Also tried varying Pause from 17.1 to 17.9. |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6274 |
Try this code. You will need to set the pin number in line 5 and depending on what you are running it on, DHT22 may have to be changed to HUMID DIM FLOAT te, hu DIM FLOAT tr(50,2) DIM INTEGER steps, alldone DIM INTEGER dht_pin = 28 DO DHT22 dht_pin,te,hu PRINT te,hu PAUSE 2000 dht11 dht_pin,te,hu PRINT te,hu PAUSE 5000 LOOP SUB dht11 pn, tem, hum LOCAL INTEGER n, x = 1 LOCAL INTEGER repl(5) LOCAL reply$ steps = 0 FOR n = 0 TO 50 repl(n) = 0 SETTICK 25, endtime,1 PIN(pn) = 1 SETPIN pn, DOUT, OC PAUSE 2 PIN(pn) = 0 PAUSE 18 ' this is the line that determines the start pulse length PIN(pn) = 1 SETPIN pn, INTL, tick DO LOOP UNTIL alldone = 1 SETTICK 0,1,1 SETPIN pn, OFF FOR n = 1 TO 49 IF tr(n+1,1)= 0 THEN EXIT FOR ' print (tr(n+1,1)-tr(n,1))*1000, tr(n,1) IF (tr(n+1,1)-tr(n,1)) > 0.1 THEN reply$ = reply$ +"1" ELSE reply$ = reply$ + "0" ENDIF NEXT n FOR n = 0 TO 4 repl(n) = VAL("&b"+MID$(reply$,n*8+x,8)) NEXT n hum = repl(0)*256+repl(1) IF hum > 32767 THEN hum = hum - 65536 hum = hum/10 tem = repl(2)*256+repl(3) IF tem > 32767 THEN tem = tem - 65536 tem = tem/10 PRINT reply$ PRINT repl(0),repl(1),repl(2),repl(3),repl(4)," ch_sum ";(repl(0)+repl(1)+repl(2)+repl(3)) AND 255 END SUB SUB tick tr(steps,1) = TIMER steps = steps + 1 END SUB SUB endtime alldone = 1 END SUB Un-comment line 38 and post the table of values so I can see how reliable it will be. So far I have only tested on a CMM2 and I have found the builtin command faulty. More work needed before reporting my findings to Peter. Sunny day here today so back to pruning the fruit trees. Jim VK7JH MMedit |
||||
Andrew_G Guru ![]() Joined: 18/10/2016 Location: AustraliaPosts: 871 |
Hi Jim, Thanks for that, much appreciated. In SUB dht11: - should "LOCAL INTEGER repl(5)" be ". . . repl(50)"? - should there be a "NEXT n" after "repl(n) = 0" (MMEdit objects)? Making those changes I get an output as below. Cheers, Andrew run 1000 1000 0 2.945145e+06 0 2.945145e+06 0 2.945145e+06 0 2.945145e+06 0 2.945145e+06 1000 2.945145e+06 0 2.945146e+06 0 2.945146e+06 0 2.945146e+06 0 2.945146e+06 0 2.945146e+06 0 2.945146e+06 1000 2.945146e+06 0 2.945147e+06 0 2.945147e+06 0 2.945147e+06 0 2.945147e+06 0 2.945147e+06 0 2.945147e+06 0 2.945147e+06 1000 2.945147e+06 0 2.945148e+06 0 2.945148e+06 0 2.945148e+06 0 2.945148e+06 0 2.945148e+06 00000100000010000000100000 4 8 8 0 0 ch_sum 20 204.8 103.2 1000 1000 0 2.945145e+06 0 2.945145e+06 0 2.945145e+06 0 2.945145e+06 0 2.945145e+06 1000 2.945145e+06 0 2.945146e+06 0 2.945146e+06 0 2.945146e+06 0 2.945146e+06 0 2.945146e+06 0 2.945146e+06 1000 2.945146e+06 0 2.945147e+06 0 2.945147e+06 0 2.945147e+06 0 2.945147e+06 0 2.945147e+06 0 2.945147e+06 0 2.945147e+06 1000 2.945147e+06 0 2.945148e+06 0 2.945148e+06 0 2.945148e+06 0 2.945148e+06 0 2.945148e+06 00000100000010000000100000 4 8 8 0 0 ch_sum 20 204.8 103.2 Edited 2021-05-28 14:32 by Andrew_G |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6274 |
NO. That array is for the 4 bytes plus checksum after converting the bits. The bit timing is stored in tr() Yes the loop should actually be: FOR n = 0 TO 50 tr(n,1) = 0 tr(n,2) = 0 next n There are a couple of other bugs in the version I posted but it is all pointless for now. I just fired up my F4. The manual suggests that TIMER function returns a float with microsecond resolution. That appears to be wrong. On the F4, TIMER is in milliseconds only, no fraction. That buggers up everything so back to the drawing board unless Peter can give us the higher resolution TIMER The code works nicely on the CMM2 but I don't have any DHT11 to test. I'm running MMBasic Version 5.07.00b9 Jim Edited 2021-05-28 16:29 by TassyJim VK7JH MMedit |
||||
Page 1 of 3 ![]() ![]() |
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |