Photon 2 - DHT11 Sensor and Reading Values

I have a DHT11 sensor on a 3-pin breakout board with a 10k resistor on the board between the Vcc and signal lines. This should be a very straight forward sensor with VCC connected to the Photon 2 3.3 pin; ground to ground and the signal wire is connected to D4.

Using the following code

// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT_Particle.h>

#define DHTPIN D4 // Define the pin connected to the DHT sensor
#define DHTTYPE DHT11 // Define the type of DHT sensor

DHT DHT(DHTPIN, DHTTYPE); // Initialize the DHT sensor

void setup() {
Serial.begin(9600); // Start serial communication at a baud rate of 9600
DHT.begin(); // Initialize the DHT sensor
}

void loop() {
// Read humidity and temperature values
float humidity = DHT.getHumidity();
float temperature = DHT.getTempCelcius();

// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
} else {
// Print the results to the serial monitor
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("%, Temp: ");
Serial.print(temperature);
Serial.println(" Celsius");
}

delay(10000); // Wait for 30 seconds before reading again
}

I get the following results.

Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Humidity: 0.00%, Temp: 0.00 Celsius
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Humidity: 0.00%, Temp: 0.00 Celsius
Humidity: 0.00%, Temp: 0.00 Celsius
Humidity: 0.00%, Temp: 0.00 Celsius
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!

Any ideas on why I can't pull simple temperature and humidity readings with the Photon 2 and this sensor? I've had the same results with another sensor, both are known good. I've also had the same results when trying a DHT22 and similar code.

I also loaded the sample code that comes with the library and get these values...

Humid: 160.00% - Temp: 128.00C 262.40F 401.15K - DewP: 143.98C - HeatI: 3629.76C
Thu Jan 11 02:28:39 2024
Failed to read from DHT sensor!
Humid: 176.00% - Temp: 128.00
C 262.40F 401.15K - DewP: 147.39C - HeatI: 4043.13C
Thu Jan 11 02:28:50 2024
Failed to read from DHT sensor!
Humid: 0.00% - Temp: 0.00C 32.00F 273.15K - DewP: nanC - HeatI: -8.78C
Thu Jan 11 02:29:00 2024
Failed to read from DHT sensor!
Humid: 160.00% - Temp: 128.00
C 262.40F 401.15K - DewP: 143.98C - HeatI: 3629.76C
Thu Jan 11 02:29:10 2024
Failed to read from DHT sensor!
Humid: 128.00% - Temp: 0.00C 32.00F 273.15K - DewP: 3.45C - HeatI: 21.45*C
Thu Jan 11 02:29:20 2024

You tried the DHT22 sensor and the similar sensor on the same device prior and after using the DHT11?

Have you tried a different DHT11 to rule out the sensor itself?

Yes sir - and the sensor works on a Pi, I also have another DHT11 and a DHT22 that have the same results

I also put two other sensors on D4 (tilt and loudness) to rule out D4 being the issue; both of those sensors provided data as expected.

May you try using Seeed_DHT11.h instead of Adafruit_DHT_Particle.h please and use A0.

2nd option: Particle Web IDE

I had the same issue, 3-pin DHT11 w/resister working on a Pi, but couldn't get any of the existing libraries to work for my photon.

I wrote my own read function and it's working fine on the photon now.
Setup:

  • Photon
    • to 3.3v pin
    • to GND pin
  • Data PIN to D2
uint8_t PIN = D2;

byte data[5] = {0x00};
float humidity = 0.0f;
float temp = 0.0f;

void setup() {
    Serial.begin(9600);
    pinMode(PIN, INPUT);
	digitalWrite(PIN, HIGH);
}

void loop() {
    delay(2000);
    if (readDht11()) {
        Serial.println("Success");
    } else {
        Serial.println("Failure");
    }
     
        Serial.print("Humidity: ");
        Serial.print(humidity);
        Serial.print("% / Temp: ");
        Serial.print((temp*1.8)+32);
        Serial.println("deg-F");
}


bool readDht11() {
    /*
     * I followed this data sheet for the timings, values, and checksums:  
     *    https://components101.com/sites/default/files/component_datasheet/DHT11-Temperature-Sensor.pdf
     *
     * I also followed the patterns in pi_dht_read.c from (Author: Tony DiCola)
     */
    
    unsigned long lastRead = 0; // Holds time microsecond timestamp for measuring
    int BITS=41; // expecting 1 constant bit to start, and then 40 bits of data
    
    // pull the pin high and wait 250 milliseconds
	digitalWrite(PIN, HIGH);
	delay(250);

    // now pull it low for 20 milliseconds
	pinMode(PIN, OUTPUT);
	digitalWrite(PIN, LOW);
	delay(20);
    
    // now pull the pin high again for 40 microseconds and set the pin state to read
	noInterrupts();
	digitalWrite(PIN, HIGH);
    pinMode(PIN, INPUT);
    
    // In the Pi version pi_dht_read.c, they there was a small for loop to add a slight delay before reading the pin
    // I have now found that I need it on the Photon too
    delayMicroseconds(5);
	
	// wait for DHT to pull down  ~80u
    //  note:  I set to 100u just to be safe
    lastRead = micros();
    while (!digitalRead(PIN)) {
        if ((micros()-lastRead) > 100) {
            interrupts();
            Serial.print("Timeout 1: ");
            Serial.println(micros()-lastRead);
            return false;
        }
    }
    
    // Next, measure the highs a lows
    //  Reading a 0 :  low:50u / high for 26-28u
    //  Reading a 1 :  low:50u / high for 70u
    // In all cases, if we exceed 100u, timeout
    unsigned long times[BITS*2] = {0};
    for (uint8_t i = 0; i < BITS*2; i+=2) {
        // count how long it's low, useful for debug
        lastRead = micros();
        while (!digitalRead(PIN)) {
            if ((micros()-lastRead) > 100) {
                interrupts();
                Serial.println("Timeout waiting for low");
                Serial.print("Bit: ");
                Serial.println(i);
                return false;
            }
        }
        times[i] = micros()-lastRead; // I don't really need this but was useful for debug
        
        // count how long it's high
        lastRead = micros();
        while (digitalRead(PIN)) {
            if ((micros()-lastRead) > 100) {
                interrupts();
                Serial.println("Timeout waiting for high");
                Serial.print("Bit: ");
                Serial.println(i);
                return false;
            }
        }
        times[i+1] = micros()-lastRead; // Stash the time
    }
    interrupts();
    
    // Process the messages to construct the bytes
    // The commented out Serial.print will give you good insights in to the output
    for (uint8_t i = 3; i<BITS*2; i+=2) {
        uint8_t index = (i-3)/16;
        // Serial.print(i);
        data[index] = data[index] << 1;
        if (times[i] >= 50) {
        //    Serial.print(": is high");
            data[index] = data[index] | 1;
        } else {
        //    Serial.print(": is low");
        }
        // Serial.print(" (high: ");
        // Serial.print(times[i]);
        // Serial.print(" / low: ");
        // Serial.print(times[i-1]);
        // Serial.println(")");
    }
    
    // verify checksum
    if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) {
        for (int i = 0; i<5; i++) {
        //    Serial.print(i);
        //    Serial.print(":");
        //    Serial.println(data[i]);
        }
        humidity = (float) data[0];
        temp = (float)data[2];
        return true;
    }
    Serial.println("Failed Checksum");
    for (int i = 0; i<5; i++) {
        Serial.print(i);
        Serial.print(":");
        Serial.println(data[i]);
    }
    // I'll still calculate the values...
    humidity = (float) data[0];
    temp = (float)data[2];
    return false; // checksum failed
}

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.