Check Sum Error DHT22

BlairM
Posts: 4
Joined: Sat Aug 26, 2017 10:48 pm

Check Sum Error DHT22

Postby BlairM » Sat Aug 26, 2017 11:06 pm

Hello,

I am having limited success working with a DHT22 Temperature and Humidity Sensor. At ambient temperature and humidity if reads fine using one of the many example sketches for this sensor, but when I pick it up or breath on it the sensor fails. I have activated debug in the DHT.h file and the check sum fails. I have 3 sensors, 2 running on a ATmega and 1 on the ESP32. No issues on the Mega and I have interchanged many times so are fairly sure the sensors are fine. I have some evidence that any sensor attached to the ESP32 reads about 0.5 degree C higher than on the ATMega, but as I write this for the first time in a couple of days they all match so this may not be an issue and more related to environment.

Below is the code I have used (from a ESP32 example, but have tried other versions). I have tried multiple libraries sourced from different places but with no success and always the same behavior. I did read that the sensor is better run on 5V so have used a level shifter to allow it to connect to the ESP32. The behavior is just the same as when it is run from the ESP32 3.3V supply.

Code: Select all

//here we use pin IO14 of ESP32 to read data
#define DHTPIN 14
//our sensor is DHT22 type
#define DHTTYPE DHT22
//create an instance of DHT sensor
DHT dht(DHTPIN, DHTTYPE,100);
void setup() {
  Serial.begin(115200);
  Serial.println("DHT22 sensor!");
  //call begin to start sensor
  dht.begin();
}

void loop() {
 //we delay a little bit for next read
  delay(2500);
  //use the functions which are supplied by library.
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // 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;
  }
  // print the result to Terminal
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" *C ");
}

Example of output change when breathing on the sensor connected to the ESP32. The other 2 (sitting next to this one) continue to read fine. Often going to 100% humidity or near.

Received:
2, 3A, 0, B7, F3 =? F3
Humidity: 57.00 % Temperature: 18.30 *C
Received:
3, 67, 0, B7, A1 =? 21
Checksum failure!
Failed to read from DHT sensor!
Received:
3, 67, 0, BA, A4 =? 24
Checksum failure!
Failed to read from DHT sensor!

The readings normally stop failing within 2-10 minutes. I did experience another type of fault with the debugging turned on once:

Timeout waiting for start signal low pulse.
Failed to read from DHT sensor!

I am fairly green at coding so would be very grateful for any advice.

Regards Blair

Dr_JFM
Posts: 2
Joined: Wed Jun 28, 2017 8:05 pm

Re: Check Sum Error DHT22

Postby Dr_JFM » Tue Aug 29, 2017 5:16 pm

Hi Blair,

I am having no problems with my ESP32 and DHT22 system, reading continually and reliably and posting to a cloud based system (Blynk -- updates the app on my phone every 10 seconds with two devices on one screen).
However, I have typed a reply twice previously only to loose it for not having logged in or for log in timing out -- so we all have plenty to improve upon!

It would be easier to troubleshoot your issues if you listed a link to a specific library you want to use. Since you didn't -- I still believe I can help you and I feel free to point you towards the library I use for more specific suggestions. I believe that your issues are rooted in timing issues and communication with the DHT22. The DHT22 is a slow sensor. The ESP32 is much faster than many early Arduino boards. The libraries that support calls like your code is executing in the loop are typically executing a read of the sensor every time they fetch a temp or RH value. Your loop pauses before reading humidity but then tries to have the library read the sensor again in a few microseconds after returning from the first read. First, just go ahead and slow down ie lengthen your delay to 3 seconds until you have it working reliably and add a second delay before the second read attempt i.e.

Code: Select all

void loop() {
 //we delay a little bit for next read
  delay(3000);
  //use the functions which are supplied by library.
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  
  delay(3000);
  // since both of these calls trigger sensor communication, wait in between them!
  
  float t = dht.readTemperature();
  // 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;
  }
The other issue of speed in adapting older libraries to the ESP32 is that timing loops and various communication routines may run too fast on the ESP32. To illustrate, I will make reference to the Rob Tillaart based ESP_DHT22 library I use. For both the ESP8266 and the ESP32, certain constants used in talking to the DHT22 need to be adjusted. This may be the case in the library you are using as well -- you will need to slug through the communication code to find any key timing dependent constants or values. If the Library has a bunch of conditional statements based on the board being used, these may no only change the pin assignments or timer assignments but may also adjust certain variables or re-define constants to adjust timing. If you want to utilize the Tillaart based library, it can be found herehttp://playground.arduino.cc/Main/DHTLib and I will show below the sections of the .h file that need adjustment for the ESP32. What is pasted below is a comment section from my sketch that was added to capture this issue and the needed library changes. You will need to edit the values in the .h file. If you have difficulty, I can send/post my library. I mainly changed the values show below and renamed it to avoid confusion with non-ESP32 versions of the library. If you go this way, note, the error routines return negative numbers to index them and do not use NAN returns.)

Code: Select all

/ END OF FILE  see library timing note below if adjusting for different CPU speed
//

       // Made OAK ESP8266 and ESP32  timing adjustments listed below directly in ESP_DHT22.h
       // following comments are quote from ESP_DHT22.h, not from this sketch  Only these 3 lines changed 
       // in the .h file (Tillart dht.h or my ESP_DHT22.h)
       // DON'T UNCOMMENT.  changes to  .h for reference here only
       /*********************Changes for ESP32 in ESP_DHT22.h ****************************************
        *  #define DHTLIB_DHT11_WAKEUP    28     // from 18 for Uno 
        *  #define DHTLIB_DHT_WAKEUP       28      //18 for UNO, 22 works w Oak, 28 for ESP32
        *  #define DHTLIB_TIMEOUT (2500)            // 600 for Uno, 1200 for Oak,  2500 for ESP32
        *  *******************Above timing for reference only ********/
 
       // max timeout is 100usec.
       // For a 16Mhz proc that is max 1600 clock cycles
       // loops using TIMEOUT use at least 4 clock cycli
       // so 100 us takes max 400 loops
       // so by dividing F_CPU by 40000 we "fail" as fast as possible
       // #define DHTLIB_TIMEOUT (F_CPU/40000)                                // did not work
       //  for UNO timeout is  600
       //1200 for Oak, 2500 for ESP32
       
This library dispenses with the various derived values that can be calculated from the Temp and RH the sensor provides. Do these in your sketch if desired. The sensor only reads RH and Temp and that is all this library does -- reads the values. It does it with a simple call to a read routine, one that "fetches" both RH and Temp and stores then in public variables. So with a single read, you can than access the current RH and Temp values in a non-time dependent way, just by using the variables. Since I only update my phone app every 10 seconds or so, I do not know how often you can read without errors. Before I set the timeout etc in the library, I got nothing from the library with the ESP32 or the ESP8266, but found out about the timing adjustments on line. If the library you are using has commented out board choices, you may want to use the fastest choice, not the "all other boards" choice and hope for timing compensation adequate to work.

Cheers
James

BlairM
Posts: 4
Joined: Sat Aug 26, 2017 10:48 pm

Re: Check Sum Error DHT22

Postby BlairM » Wed Aug 30, 2017 10:06 am

Thanks James for your significant efforts. I had concluded it was probably board speed and timing that was the issue. I had been hunting for ways to reduce the clock speed within the Arduino environment, but failed. I figured as RH went up the time to punch out the 16 bit word went up and that is when things fail. I work through your suggestions and see how I go. Thanks and regards Blair

BlairM
Posts: 4
Joined: Sat Aug 26, 2017 10:48 pm

Re: Check Sum Error DHT22

Postby BlairM » Thu Aug 31, 2017 12:43 am

The guidance James provides has been invaluable. The Rob Tillaart ( https://github.com/RobTillaart/Arduino) DHTStable library ran without alteration with my DHT22 and ESP32 using his DHT22 example in that library. If anyone else is trying to use the DHT sensors, I would highly recommend looking at this resource. Thanks again James

Dr_JFM
Posts: 2
Joined: Wed Jun 28, 2017 8:05 pm

Re: Check Sum Error DHT22

Postby Dr_JFM » Wed Sep 06, 2017 8:10 pm

Glad it worked out. I, too, would give much credit to Rob Tillaart for his straightforward DHT library!
Now I am fighting with a UART application on the ESP32 -- a port from ESP8266. Will have to start a thread soon to see if someone can help me figure out my problem (not quite ready to give up myself yet).

Cheers
James

Who is online

Users browsing this forum: No registered users and 80 guests