Can't read DHT22 with Electron - *SOLVED*

Trying to get a reading from a DHT22/AM2302 with my E-Series electron dev board with no luck. I’m using the PietteTech_DHT library and an 10k resistor between the +5v vcc (pin 1) and data (pin 2), and have pin 3 to GND. (I also tried 4.77 resistor and 3.3v as suggested somewhere). I have multiple DHT22 units that I’ve swapped in to rule out a faulty sensor. (These sensors worked well on RPi with no issues)

SERIAL CONSOLE:

Serial monitor opened successfully:

0: Retrieving information from sensor: Read sensor: Error
        Response time out error
Humidity (%): -5.00
Temperature (oC): -5.00

1: Retrieving information from sensor: Read sensor: Error
        Response time out error
Humidity (%): -5.00
Temperature (oC): -5.00

MY .INO FILE:

// Include library for the temp sensors
#include "../lib/PietteTech_DHT/src/PietteTech_DHT.h"

#define DHTPIN D1
#define DHTTYPE DHT22

// Lib instantiate
PietteTech_DHT DHT(DHTPIN, DHTTYPE);
int n;      // counter

// setup() runs once, when the device is first turned on.
void setup() {
  // Put initialization like pinMode and begin functions here.

 	Serial.begin(9600); 
	Serial.println("DHT Simple program using DHT.acquireAndWait");
  Serial.print("LIB version: ");
  Serial.println(DHTLIB_VERSION);

	DHT.begin();
 
}

// loop() runs over and over again, as quickly as it can execute.
void loop() {
  // The core of your code will likely live here.
  
  // Temp sensors require 2 sec delay
  delay(2500);
  
  Serial.print("\n");
  Serial.print(n);
  Serial.print(": Retrieving information from sensor: ");
  Serial.print("Read sensor: ");

  int result = DHT.acquireAndWait(2500); // wait up to 1 sec (default indefinitely)

  switch (result) {
  case DHTLIB_OK:
    Serial.println("OK");
    break;
  case DHTLIB_ERROR_CHECKSUM:
    Serial.println("Error\n\r\tChecksum error");
    break;
  case DHTLIB_ERROR_ISR_TIMEOUT:
    Serial.println("Error\n\r\tISR time out error");
    break;
  case DHTLIB_ERROR_RESPONSE_TIMEOUT:
    Serial.println("Error\n\r\tResponse time out error");
    break;
  case DHTLIB_ERROR_DATA_TIMEOUT:
    Serial.println("Error\n\r\tData time out error");
    break;
  case DHTLIB_ERROR_ACQUIRING:
    Serial.println("Error\n\r\tAcquiring");
    break;
  case DHTLIB_ERROR_DELTA:
    Serial.println("Error\n\r\tDelta time to small");
    break;
  case DHTLIB_ERROR_NOTSTARTED:
    Serial.println("Error\n\r\tNot started");
    break;
  default:
    Serial.println("Unknown error");
    break;
  }

  Serial.print("Humidity (%): ");
  Serial.println(DHT.getHumidity(), 2);

  Serial.print("Temperature (oC): ");
  Serial.println(DHT.getCelsius(), 2);

 // Serial.print("Temperature (oF): ");
 // Serial.println(DHT.getFahrenheit(), 2);

 // Serial.print("Temperature (K): ");
 // Serial.println(DHT.getKelvin(), 2);

 // Serial.print("Dew Point (oC): ");
 // Serial.println(DHT.getDewPoint());

 // Serial.print("Dew Point Slow (oC): ");
 // Serial.println(DHT.getDewPointSlow());

  n++;
}

I’ve read several threads on this issue but most are very old now. I did switch from the Adafruit_DHT lib which wasn’t working either and many suggested PietteTech_DHT works better. My leads to the sensor are about 15cm long. I was planning to run this sensor on 5vdc at about 20 meters which the device specs claim is supported.

I appreciate any feedback. I switched my project from RPi to Particle Electron and am considering switching back due to this issue unless I can solve it soon.

@kquainta, give the Adafruit_DHT library a try since it has been updated recently. When did you try it? Also, do make sure the VIN pin (pin 1) is actually at 5V.

I am currently reading a Grove DHT11 on an E0 series dev board and it works fine. I can share the code if you wish.

@peekay123 . Thanks for the quick reply. Yes, I measure 5.05vdc on pin 1.

I’ll switch back to the Adafruit_DHT library now but would love to see your code as well. Maybe I’m just missing something here.

@kquainta, here is the code I use:

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


// This example assumes the sensor to be plugged into CONN2
#define DHTPIN D1     // what pin we're connected to

// Here we define the type of sensor used
#define DHTTYPE DHT11        // DHT 11 

DHT dht(DHTPIN, DHTTYPE);

char msg[80];

void setup() {

    // We open up a serial port to monitor the sensor values
    Serial.begin(9600); 
    Serial.println("DHT11 test!");

    dht.begin();
}

void loop() {
    // Wait a few seconds between measurements.
    delay(60000);

    // Reading temperature or humidity takes about 250 milliseconds!
    // Sensor readings may also be up to 2 seconds 
    float h = dht.getHumidity();
    // Read temperature as Celsius
    float t = dht.getTempCelcius();
    // Read temperature as Farenheit
    float f = dht.getTempFarenheit();

    // Check if any reads failed and exit early (to try again).
    if (isnan(h) || isnan(t) || isnan(f)) {
        Serial.println("Failed to read from DHT sensor!");
        return;
    }

/*
    // Print the data over serial
    Serial.print("Humid: "); 
    Serial.print(h);
    Serial.print("% - ");
    Serial.print("Temp: "); 
    Serial.print(t);
    Serial.print("*C ");
    Serial.println(Time.timeStr());
*/

    // Publish data to the Particle cloud. 
    // Remember that you'll consume data every time you publish to the cloud.
    snprintf(msg, sizeof(msg), "%4.2fC   %4.2f%%", t, h);
    Particle.publish("temp_humi", msg, 60, PRIVATE);
}

The E0 dev board is sitting on my bench publishing every 60 seconds. I’ll be doing some sleep tests at some point.

@peekay123 Thanks for this. I switched over to ADAfruit_DHT but still cannot read anything. I confirmed +5v and tried two different DHT22’s.

SERIAL CONSOLE:

Serial monitor opened successfully:
Failed to read from DHT sensor!
Failed to read from DHT sensor!

MY CODE:

// Include library for the temp sensors
#include "../lib/Adafruit_DHT/src/Adafruit_DHT.h"

#define DHTPIN D1
#define DHTTYPE DHT22

// Lib instantiate
DHT dht(DHTPIN, DHTTYPE);

// setup() runs once, when the device is first turned on.
void setup() {
  // Put initialization like pinMode and begin functions here.

 	Serial.begin(9600); 
	Serial.println("DHT ADAfruit test");
	dht.begin();
}

// loop() runs over and over again, as quickly as it can execute.
void loop() {
 // Wait a few seconds between measurements.
	delay(10000);

// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a 
// very slow sensor)
	float h = dht.getHumidity();
// Read temperature as Celsius
	float t = dht.getTempCelcius();
// Read temperature as Farenheit
//	float f = dht.getTempFarenheit();
  
// Check if any reads failed and exit early (to try again).
	if (isnan(h) || isnan(t)) {
		Serial.println("Failed to read from DHT sensor!");
		return;
	}

	Serial.print("Humid: "); 
	Serial.print(h);
	Serial.print("% - ");
	Serial.print("Temp: "); 
	Serial.print(t);
	Serial.println("*C");
	Serial.println(Time.timeStr());
}

Questions:

  1. I noticed you are using the CLI. I am using vbCode/Workbench. Any known issues here?
  2. Attempting to flash via Workbench in DFU Mode is very flaky. I need to power down/up the board, remove/replace my USB cable, re-enter DFU mode many times before DFU-Util can successfully flash. I replaced my USB cable with no luck. I am using a Macbook Pro with an USB-C to USB-A adapter. Just curious if there are known issues.

UPDATE: So switched over to the grove DHT11 but unfortunately had the same issues but I stumbled on another library named ADAfruit_DHT_Particle and when I changed to this library from ADAfruit_DHT, it now WORKS!

I’m now reverting to my preferred DHT22 sensor and will report if it is also successful.

SOLVED!

After replacing my DHT22 sensor, I’m pleased to report that it works. Here’s a recap:

Library: ADAfruit_DHT_Particle driver (NOT ADAfruit_DHT driver)
Sensor: DHT22/AM2302 (Labeled “ASAIR AM2302”)
Sensor Pinouts (Read from left to right while looking at the front):
PIN 1: +5vdc
PIN 2: To GPIO
PIN 3: Not Used
PIN 4: GND
100uf capacitor between pins 1 & 2 (for wave smoothing)
(Note: No 10k pullup resistor is needed between pins 1 & 2)

Particle E0 series dev board (Electron module with breakouts, connectors, etc):
* We can use any GPIO pin (I am using D0 - D3 & C5 since i need to run 5 temp sensors)
* +9v via barrel connector
* LiPoly battery connected

** program.ino file**

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


// This example assumes the sensor to be plugged into CONN2
#define DHTPIN D1     // what pin we're connected to

// Here we define the type of sensor used
#define DHTTYPE DHT22       // DHT11 or DHT22 

DHT dht(DHTPIN, DHTTYPE);

void setup() {

    // We open up a serial port to monitor the sensor values
    Serial.begin(9600); 
    Serial.println("DHT22 test!");

    dht.begin();
}

void loop() {
    // Wait a few seconds between measurements.
    delay(5000);

    // Reading temperature or humidity takes about 250 milliseconds!
    // Sensor readings may also be up to 2 seconds 
    float h = dht.getHumidity();
    // Read temperature as Celsius
    float t = dht.getTempCelcius();
    // Read temperature as Farenheit
    float f = dht.getTempFarenheit();

    // Check if any reads failed and exit early (to try again).
    if (isnan(h) || isnan(t) || isnan(f)) {
        Serial.println("Failed to read from Grove DHT sensor!");
        return;
    }

    // Print the data over serial
    Serial.print("Humid: "); 
    Serial.print(h);
    Serial.print("% - ");
    Serial.print("Temp: "); 
    Serial.print(f);
    Serial.print("*F ");
    Serial.println(Time.timeStr());

    // Publish data to the Particle cloud. 
    // Remember that you'll consume data every time you publish to the cloud.
    // Particle.publish("temp", String (f));
    // Particle.publish("humi", String (h));
}

CONSOLE OUTPUT

Humid: 45.40% - Temp: 77.00*F Wed Jun 19 00:41:48 2019
Humid: 45.30% - Temp: 77.00*F Wed Jun 19 00:41:53 2019
Humid: 45.20% - Temp: 77.00*F Wed Jun 19 00:41:58 2019
Humid: 45.20% - Temp: 77.00*F Wed Jun 19 00:42:04 2019
Humid: 45.20% - Temp: 77.00*F Wed Jun 19 00:42:09 2019
Humid: 45.20% - Temp: 77.00*F Wed Jun 19 00:42:14 2019
Humid: 45.20% - Temp: 77.00*F Wed Jun 19 00:42:19 2019
Humid: 45.20% - Temp: 77.00*F Wed Jun 19 00:42:25 2019

NOTE: I also switched from the vbCode/Workbench IDE to the WebIDE. Not sure if that also helped.

1 Like