Netduino home hardware projects downloads community

Jump to content


Photo

DHT11/22 sensor managed driver


  • Please log in to reply
59 replies to this topic

#1 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 16 September 2011 - 08:04 PM

Attached is the source code of managed driver for DHT11 and DHT22 sensors. It uses trick with two pins wired together to workaround timing problems caused by slow execution of managed code and interrupt-based pulse measurement.

Important: Due to the resolution of Netduino system timer which is not enough to precisely measure duration of sensor pulses, it may happen that the sensor readout fails due to invalid checksum, usually for specific values of RH or temperature. You could either ignore that or try tuning BitThreshold value in DhtSensor.cs; or change the firmware to enhance timer precision.

Enjoy Posted Image

Attached Files



#2 bxb

bxb

    New Member

  • Members
  • Pip
  • 6 posts

Posted 08 October 2011 - 07:14 PM

Hi, just wanted to thank you, the driver works quite well! bye, bxb

#3 randalx

randalx

    New Member

  • Members
  • Pip
  • 3 posts

Posted 13 October 2011 - 01:09 AM

Hi, I tried your code but unfortunately it's not working for me. Perhaps it's my wiring? This is how I set it up the wiring between a Netduino Plus and the DHT22 sensor: Pin 1 -> VCC Pin 2 -> D0 and D1 Pin 4 -> Ground And a 10K resistor between Pin 1 and Pin 2. I initialize with var dhtSensor = new Dht22Sensor(Pins.GPIO_PIN_D0, Pins.GPIO_PIN_D1, PullUpResistor.External); But fail when calling Read: DhtSensor.Read() { ... if(dataReceived.WaitOne(50, false)) //FAILS HERE I tried playing with the various timings in the code but I never succeeded in getting the Read method to pass. Any help would be appreciated. Thanks.

#4 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 13 October 2011 - 06:28 AM

This is how I set it up the wiring between a Netduino Plus and the DHT22 sensor:

Well, I have not tried the code on Netduino Plus - your wiring seems to be fine (unless you've flipped the sensor), but it may be possible that for example network communication interrupts disturb the pulse measurement. Could you please try unplugging the network cable? Or do you have any other code running (e.g. in background thread) that may affect the communication with DHT sensor?

if(dataReceived.WaitOne(50, false)) //FAILS HERE

What is the value of bitMask field when the method returns? Have you tried increasing the timeout value (e.g. 200 instead of 50)? Also, try adding some diagnostic output (Debug.Print()) into portIn_OnInterrupt handler to verify it is being called.

#5 randalx

randalx

    New Member

  • Members
  • Pip
  • 3 posts

Posted 14 October 2011 - 01:16 AM

Thanks for taking the time to respond to my questions.

Well, I have not tried the code on Netduino Plus - your wiring seems to be fine (unless you've flipped the sensor), but it may be possible that for example network communication interrupts disturb the pulse measurement. Could you please try unplugging the network cable? Or do you have any other code running (e.g. in background thread) that may affect the communication with DHT sensor?


Unfortunately, unplugging the network cable made no difference. I triple checked the wiring and it seems correct. The CheckPins() call passes successfully.

What is the value of bitMask field when the method returns? Have you tried increasing the timeout value (e.g. 200 instead of 50)? Also, try adding some diagnostic output (Debug.Print()) into portIn_OnInterrupt handler to verify it is being called.


With the default timeout of 50ms the bitmask makes it down to 2199023255552. I had to increase it to 2000ms to be able to get it down to 0, but that just resulted in checksum failures.

The portIn_OnInterrupt method is called repeatedly but I noticed that most of the time the BitThreshold is being exceeded.
if((ticks - lastTicks) > BitThreshold)

I ran several calls in a loop but never had a single successful result.

Should I conclude that my netduino plus is running slower than the norm?

Thanks again for the help.

#6 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 14 October 2011 - 06:50 AM

With the default timeout of 50ms the bitmask makes it down to 2199023255552. I had to increase it to 2000ms to be able to get it down to 0, but that just resulted in checksum failures.

Well, the first value (with default 50 ms timeout) indicates there was no interrupt received during that time, 2000 ms timeout is very suspicious, because the whole DHT communication should not exceed 5 ms (= 40 bits × ~50+70 µs), a little bit bigger value is needed to accommodate the slow speed of managed code execution of queued interrupt handlers, but 400× factor is really big - do you have any other code running, that may be using interrupts, blocking the CPU etc.? Also, could you please try running the DHT sample application alone?

Should I conclude that my netduino plus is running slower than the norm?

That would be very strange. It may also be a defective sensor, but further troubleshooting would probably require seeing what exactly is happening on the communication line (i.e. oscilloscope or logic analyzer). Or maybe [ab]using the SPI to sample the data line (?)

#7 randalx

randalx

    New Member

  • Members
  • Pip
  • 3 posts

Posted 19 October 2011 - 01:31 AM

I tried running the code alone and got the same results. I also tried the sensor with an Arduino and the sensor worked successfully so it's not a defective sensor. I'm new to hardware so I don't think I'll be able to attempt to investigate further on my own. Thanks anyhow for the help.

#8 Darduino

Darduino

    New Member

  • Members
  • Pip
  • 8 posts

Posted 07 January 2012 - 03:57 AM

Hi Stanislav, I tried your code but unfortunately it's not working for me. I know my wiring is OK, cause when i remove the sensor I get "DHT Sensor timeout" however i like to point out that I used 1k pull-up resistor as shown in the datasheet. At first i was getting "DHT sensor data has invalid checksum." then i played with BitThreshold changing from 1050 to 1650 I started getting a mixture of "DHT sensor Read() ok, RH = 0.0%, Temp = 0.0°C" and "DHT sensor data has invalid checksum." the RH and Temp are always 0, and sometimes (very few) both will be 12.8% and 12.8C

#9 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 08 January 2012 - 11:45 AM

then i played with BitThreshold changing from 1050 to 1650
...
the RH and Temp are always 0, and sometimes (very few) both will be 12.8% and 12.8C

Well, this is unfortunate, as there is not much left to be adjusted in the managed code - you'd need to use a logic analyzer (or an oscilloscope) to see what exactly is happening on the communication line and then probably the code would need to be done natively (C/C++), to overcome .NET MF timing limitations.

#10 rex64

rex64

    Member

  • Members
  • PipPip
  • 17 posts

Posted 09 March 2012 - 05:14 PM

Do you know of a good tutorial on how to build custom firmwares? I download the files but was confused on what to do. Also, has anyone built a compiled file they can post? I have a web server if it is to big to post here. Thanks! This is what I am working on: http://forums.netdui...h__1#entry22320

#11 PointDev

PointDev

    New Member

  • Members
  • Pip
  • 3 posts

Posted 27 July 2012 - 07:30 AM

Hi everybody. Could someone who successfully played with the DHTxxApp shows the wiring? I'm mostly a software guy and I still don't figure out the two wires trick. I understand the intention and the netduino's limitation it workarounds, but not the wiring. Thanks guys. PointDev

#12 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 27 July 2012 - 09:23 AM

Attached is the wiring for the DHT sample application with internal pull-up resistor (use PullUpResistor.Internal in the DhtxxSensor constructor). Otherwise, you'd need to connect the resistor between DHT sensor pins 1 (power) and 2 (Data). The 'two wires trick' is to have DHT sensor data signal connected to Netduino pins D0 and D1 simultaneously.

Attached Files



#13 neslekkim

neslekkim

    Advanced Member

  • Members
  • PipPipPip
  • 350 posts
  • LocationOslo, Norway

Posted 27 July 2012 - 09:31 AM

Important: Due to the resolution of Netduino system timer which is not enough to precisely measure duration of sensor pulses, it may happen that the sensor readout fails due to invalid checksum, usually for specific values of RH or temperature. You could either ignore that or try tuning BitThreshold value in DhtSensor.cs; or change the firmware to enhance timer precision.


How is this resolution on the Netduino Go?

--
Asbjrn


#14 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 27 July 2012 - 09:34 AM

How is this resolution on the Netduino Go?

1 µs Posted Image

#15 PointDev

PointDev

    New Member

  • Members
  • Pip
  • 3 posts

Posted 27 July 2012 - 10:20 AM

Attached is the wiring for the DHT sample application with internal pull-up resistor (use PullUpResistor.Internal in the DhtxxSensor constructor). Otherwise, you'd need to connect the resistor between DHT sensor pins 1 (power) and 2 (Data). The 'two wires trick' is to have DHT sensor data signal connected to Netduino pins D0 and D1 simultaneously.


Thank you so much! I received my Netduino yesterday, and tried to use your code, but because of my poor hardware knowledge, i didn't manage to wire it myself.
Can I ask which soft is used to make that kind of wiring picture? I guess it is a convenient tool for exchanges on the forum.

@the community : english isn't my first language. I try not to make too much mistakes and apologize if some of it makes reading me too difficult.

#16 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 27 July 2012 - 10:39 AM

Can I ask which soft is used to make that kind of wiring picture?

It's Fritzing.

#17 PointDev

PointDev

    New Member

  • Members
  • Pip
  • 3 posts

Posted 27 July 2012 - 10:46 AM

It's Fritzing.


Thank you very much again. :)

#18 Giuliano

Giuliano

    Advanced Member

  • Members
  • PipPipPip
  • 361 posts
  • LocationSimi Valley, CA

Posted 13 September 2012 - 05:09 AM

Hi CW2,

First of all, thanks for posting your code, it is realy helpful.

I am running your code in my N+. I got it to work after pulling my hair for hours. I am not using a 10K Ohms resistor between pins 1 and 2, as suggested by randalx in this thread, is it neccesary? I don't see it in your schematics. Anyway, this is how I got it to work:


First I had to put a breakpoint inside the method CheckPins (shown below). That was the only way I was able to get the method to succeed, otherwise without this breakpoint or either using Thread.Sleep(thousand of mileseconds) before initializing the Dht22Sensor class I would get the "DHT sensor pins are not connected together." error all the time.


Posted Image

Well, now that I got it to work, I get the following readings which are very accurate but it fails to read from time to time:

Posted Image

Please advise

#19 neslekkim

neslekkim

    Advanced Member

  • Members
  • PipPipPip
  • 350 posts
  • LocationOslo, Norway

Posted 13 September 2012 - 06:31 AM

Look at the specs here: http://learn.adafruit.com/dht You are probably reading to fast/often Sampling rate: dht11: No more than 1 Hz sampling rate (once every second) dht22: No more than 0.5 Hz sampling rate (once every 2 seconds)

--
Asbjrn


#20 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 13 September 2012 - 06:47 AM

I am not using a 10K Ohms resistor between pins 1 and 2, as suggested by randalx in this thread, is it neccesary? I don't see it in your schematics. Anyway, this is how I got it to work:

The pull-up resistor is mandatory, but you can use Netduino built-in one. There is parameter in the DHT sensor class constructor to specify that (PullUpResistor.Internal, External).

First I had to put a breakpoint inside the method CheckPins (shown below). That was the only way I was able to get the method to succeed, otherwise without this breakpoint or either using Thread.Sleep(thousand of mileseconds) before initializing the Dht22Sensor class I would get the "DHT sensor pins are not connected together." error all the time.


Hm, I am not sure I understand - you get the exception even if you have the two pins connected together?

I get the following readings which are very accurate but it fails to read from time to time:


I have got similar reports from other NetduinoPlus users - I think the reason is that there are more interrupts on NetduinoPlus (e.g. network-related) that disturb the sensor communication.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.