DHT22 sensor get stuck with Spark Core on pin 5 and pin 6 [SOLVED]

I am working on the PietteTech_DHT library with DHT22 sensors, and example is working well in pin 2 and pin 3, however when I am connecting the DHT22 sensors with pin 5 or pin 6, the output will get stuck for no reason.

any one knows why ? (I am using the spark core)

Okay, apologies in advance, but I have to ask: have you changed the pin declarations in the code accordingly?

I am using the PietteTech_DHT example, and only change the pin declarations in this

#define DHTTYPEA  DHT22       // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPINA   6           // Digital pin for comunications
#define DHTTYPEB  DHT22       // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPINB  5           // Digital pin for comunications 

normally it should be ok, but the serial output will get stuck in

0:00s : retrieving information from sensor : Read sensor A.

and seems the sensor reading keep loops in while (DHTA.acquiring()) ;

I too, would expect that to work. Hmm, we could always try asking @mtnscott who wrote the library. @peekay123 and/or @bko might also be able to chime in.

All I have tested so far seems that D0, D1, D2, D3, D4 are working well while D5,D6 are not

I have had the exact same problem on pin 5. I never thought to try other pins. I have no solution for you, I just wanted to back up your observation.

@helxsz, are you using a Photon or a Core? Also, if I can suggest using the full pin designation in your code (D5, D6 instead of 5,6). I can’t remember my own tests but D5, D6 and D7 are pins designated for JTAG debugging so I’m not sure what, if any, impact that may have. @BDub, any ideas?

HI, @peekay123, i am currently using the Core. And I use full pin like D5 and D6, still get stuck in the program

@helxsz, @BDub or @mohit will need to get involved at this point.

It is good to find out this issue, so I am involved. will look into the library to see the problem

If you can give me some library code and an example, I’ll give it a try and see what the difference is on my oscilloscope. Once you take over a pin’s mode with pinMode() the state of the pins from boot will not be the default JTAG pull up/ pull down inputs. In fact, they should be defaulted as input/no pull up once user code runs (i.e., once you leave the bootloader).

hi,@BDub, this is the library on the spark community for DHT22 sensor, https://build.particle.io/libs/544807ebafdb1a2d7f000179/tab/DHT_2sensor.ino

the DHT22 is pretty quirky… I stopped using it after trying a few different libraries and it still gets stuck and it always get’s stuck. I think the issue may be with the sensor… I should try it with an arduino and see if it hangs… It is good for starters, but I would recommend finding a different sensor from production. Honeywell and silicon labs both make an i2c sensor that is rock solid. They work with the wire library

There is a lot going on with this pin in this library... I wonder if there is a subtle difference that's preventing interrupts from working the same way on D6 as it does on the rest of the pins:

        /*
         * Toggle the digital output to trigger the DHT device
         * to send us temperature and humidity data
         */
        pinMode(_sigPin, OUTPUT);
        digitalWrite(_sigPin, LOW);
        if (_type == DHT11)
            delay(18);                  // DHT11 Spec: 18ms min
        else
            delayMicroseconds(1500);    // DHT22 Spec: 0.8-20ms, 1ms typ
        pinMode(_sigPin, INPUT);        // Note Hi-Z mode with pullup resistor
                                        // will keep this high until the DHT responds.
        /*
         * Attach the interrupt handler to receive the data once the DHT
         * starts to send us data
         */
        _us = micros();
        attachInterrupt(_sigPin, isrCallback_wrapper, FALLING);

AHA... attachInterrupt() is not supported on the Core for pins D5~D7 or A2

I would say the library needs protection against those pins being used :smile:
http://docs.particle.io/core/firmware/#interrupts-attachinterrupt

This library does say this as well:

I'm not sure why A4 is not included.

Should work with a Photon as all pins are supported.


I'm going to make this as solved.

4 Likes

Hi,
I followed this topic and I tried to setup this scenario with a Photon + DHT22 sensore but I can’t get it working.

In particular I always get back the status DHTLIB_ERROR_NOTSTARTED.
I initialize the library with:

#include "PietteTech_DHT.h"

#define DHTTYPE  DHT22       // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN   D3          // Digital pin for communications

// Declaration
void dht_wrapper(); // must be declared before the lib initialization

// Lib instantiate
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);

Should I’ve to do something else?
What am I missing?

Thanks in advance.

@xxdede, how are you powering the DHT22 and do you have a pull-up resistor on the output?

Power 3V3 on DHT22 first pin and 1k resistor like this:

After struggling a bit I decided to switch from D3 to D2 and now it works!

Maybe D3 (on Photon) doesn’t support:

attachInterrupt(_sigPin, isrCallback_wrapper, FALLING);

because in a debug session I checked that it was always returning: -7 and -2

DHTLIB_ERROR_NOTSTARTED    -7
DHTLIB_ERROR_ISR_TIMEOUT   -2

Now on D2 everything works as expected.

1 Like