The Netduino forums have been replaced by new forums at community.wildernesslabs.co.
This site has been preserved for archival purposes only
and the ability to make new accounts or posts has been turned off.
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.
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.
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.
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?
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 (?)
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.
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
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.
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
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
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.
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.
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.
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.
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:
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)
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.