Conflict between DHT library and Adafruit LED library?

I am trying to use the PietteTech_DHT library together with the adafruit-led-backpack library and it is not working.

My goal is to read the temperature on my DHT11 and display it on my adafruit quad alphanumeric LED display. I am able to either read the DHT11 or change the display but I am finding that any attempt to change the display after reading the DHT11 does not work.

In the below code I would expect the display to change between 0 and 1 and the temperature/humidity to be published as an event to the dashboard. What actually happens is that the display gets properly set to 0 on the first time through the loop and never changes after that. The temperature/humidity is being properly reported on the dashboard. If I comment out calls to the DHT class inside of loop the display properly changes between 0 and 1 as expected.

Other people seem to have gotten these libraries to cooperate so I hope that there is something simple going wrong for me. Thank you for your help in advance.

// This #include statement was automatically added by the Particle IDE.
#include "adafruit-led-backpack/adafruit-led-backpack.h"

// This #include statement was automatically added by the Particle IDE.
#include "PietteTech_DHT/PietteTech_DHT.h"

// DHT parametershttps://build.particle.io/build/566c7cfdbc7aa6cd4a0000cc#flash
#define DHTPIN 5
#define DHTTYPE DHT11

#define MINUTE 60000

void dht_wrapper();

PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);

// Variables
int temperature = 0;
int humidity = 0;

int num = 1;

Adafruit_AlphaNum4 alpha4 = Adafruit_AlphaNum4();

void setup() {
    Particle.variable("temperature", &temperature, INT);
    Particle.variable("humidity", &humidity, INT);
    
    alpha4.begin(0x70);
    
    alpha4.clear();
    alpha4.writeDisplay();
    delay(300);
}

// This wrapper is in charge of calling
// must be defined like this for the lib work
void dht_wrapper() {
    DHT.isrCallback();
}

void loop() {
    if(num == 1)
    {
        num = 0;
    }
    else
    {
        num = 1;
    }
    
    alpha4.writeDigitAscii(1, '0' + num);
    alpha4.writeDisplay();
    delay(300);
    
    int result = DHT.acquireAndWait();
    
    if(result == DHTLIB_OK)
    {
        temperature = DHT.getFahrenheit();
        humidity = DHT.getHumidity();
    
        Particle.publish("temperature", String(temperature) + "F");
        delay(10);
        Particle.publish("humidity", String(humidity) + "%");
    }
    else
    {
        Particle.publish("ERROR: " + result);
    }
    
    delay(2000);
}

Could you add some Serial.print() lines to see which of your code line causes the blocking?
I'd suspect int result = DHT.acquireAndWait();.

You might also want to do the instantiating of your display object this way instead

Adafruit_AlphaNum4 alpha4; // usually you'd append () but don't here - don't ask ;-) 

Despite the fact that the sample does it differently.

Also the comment in the PIETTECH_DHT library states this

But you do this

#define DHTPIN 5
#define DHTTYPE DHT11

where 5 stands for pin D5 which is not in the list of supported pins.

BTW: Try making a habit of using Dx and Ax as pin denominators rather than e.g. 12 for A2 or 18 for TX (or is it RX ... you get the point ;-))

1 Like

I am attaching my new code after moving the sensor pin to D2 and adding Serial.println as well as the output. As you can see from the output the loop executes the entire way through. The symptom I see is that even though the lines to write to the display are executed, the display does not respond to them after the first call to the DHT sensor.

// This #include statement was automatically added by the Particle IDE.
#include "adafruit-led-backpack/adafruit-led-backpack.h"

// This #include statement was automatically added by the Particle IDE.
#include "PietteTech_DHT/PietteTech_DHT.h"

// DHT parametershttps://build.particle.io/build/566c7cfdbc7aa6cd4a0000cc#flash
#define DHTPIN D2
#define DHTTYPE DHT11

#define MINUTE 60000

void dht_wrapper();

PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);

// Variables
int temperature = 0;
int humidity = 0;

int num = 1;

Adafruit_AlphaNum4 alpha4 = Adafruit_AlphaNum4();

void setup() {
    Serial.begin(9660);
    delay(20000);
    Serial.println("Starting init.");
    
    Particle.variable("temperature", &temperature, INT);
    Particle.variable("humidity", &humidity, INT);
    
    alpha4.begin(0x70);
    
    alpha4.clear();
    alpha4.writeDisplay();
    delay(300);
}

// This wrapper is in charge of calling
// must be defined like this for the lib work
void dht_wrapper() {
    DHT.isrCallback();
}

void loop() {
    if(num == 1)
    {
        num = 0;
    }
    else
    {
        num = 1;
    }
    
    Serial.println(num);
    
    alpha4.writeDigitAscii(1, '0' + num);
    alpha4.writeDisplay();
    delay(300);
    
    Serial.println("Should have flipped display. Now aquire.");
    
    int result = DHT.acquireAndWait();
    
    if(result == DHTLIB_OK)
    {
        temperature = DHT.getFahrenheit();
        Serial.println(temperature);
        
        humidity = DHT.getHumidity();
        Serial.println(humidity);
        
    
        Particle.publish("temperature", String(temperature) + "F");
        delay(10);
        Particle.publish("humidity", String(humidity) + "%");
    }
    else
    {
        Particle.publish("ERROR: " + result);
    }
    Serial.println("end of loop.");
    delay(2000);
}

Output:

Starting init.
0
Should have flipped display. Now aquire.
78
43
end of loop.
1
Should have flipped display. Now aquire.
75
47
end of loop.
0
Should have flipped display. Now aquire.
78
46
end of loop.
1
Should have flipped display. Now aquire.
75
58
end of loop.
0
Should have flipped display. Now aquire.
80
64
end of loop.
1
Should have flipped display. Now aquire.
78
68
end of loop.
0
Should have flipped display. Now aquire.
78
71
end of loop.
1
Should have flipped display. Now aquire.
78
71
end of loop.