Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Troubleshooting
  3. Humidity Sketch w/ DHT22: readings not reliable

Humidity Sketch w/ DHT22: readings not reliable

Scheduled Pinned Locked Moved Troubleshooting
10 Posts 5 Posters 4.9k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • HenryWhiteH Offline
    HenryWhiteH Offline
    HenryWhite
    wrote on last edited by HenryWhite
    #1

    Hi!

    When using the official temperature/humidity sketch with a 10k resistor between data and 3.3V VCC, the readings are not stable; I have lots of
    "Failed reading temperature from DHT"
    "Failed reading humidity from DHT"

    It seems that the temperature reading is successfull only every 4-5th time, while the humidity reading works every 1-2nd time.

    I'm using an 8mhz arduino pro mini 3.3V.
    Any ideas why this happens? Could a capacitor solve this problem?

    1 Reply Last reply
    0
    • scalzS Offline
      scalzS Offline
      scalz
      Hardware Contributor
      wrote on last edited by
      #2

      maybe add gw.wait before your reading so it has enough time to sense...

      1 Reply Last reply
      0
      • barduinoB Offline
        barduinoB Offline
        barduino
        wrote on last edited by
        #3

        Something like this

            Serial.print("Initializing DHT... ");
            DHT _dht;
            _dht.setup(DHT_PIN);
            delay(_dht.getMinimumSamplingPeriod());
            Serial.println("Done!");
        

        Or check the example here

        Cheers

        1 Reply Last reply
        0
        • HenryWhiteH Offline
          HenryWhiteH Offline
          HenryWhite
          wrote on last edited by HenryWhite
          #4

          guys, as written above, i'm already using the official temperature/humidity sketch.. :smiley:
          So there is a

          gw.wait(dht.getMinimumSamplingPeriod());
          

          before the reading.
          I tried to increase this period to static 2,5 seconds (default interval should be 2s according to datasheet), but sadly that didn't help.

          Here's what the serial monitor shows (strange, isn't it?):
          0_1454332301952_serial.png

          1 Reply Last reply
          0
          • barduinoB Offline
            barduinoB Offline
            barduino
            wrote on last edited by
            #5

            Ok, lets see if I can replicate your situation

            Would you post the sketch and the log form serial?

            HenryWhiteH 1 Reply Last reply
            0
            • barduinoB barduino

              Ok, lets see if I can replicate your situation

              Would you post the sketch and the log form serial?

              HenryWhiteH Offline
              HenryWhiteH Offline
              HenryWhite
              wrote on last edited by
              #6

              @barduino said:

              Ok, lets see if I can replicate your situation

              Would you post the sketch and the log form serial?

              for the serial output, see above :)

              default sketch:

              /**
               * The MySensors Arduino library handles the wireless radio link and protocol
               * between your home built sensors/actuators and HA controller of choice.
               * The sensors forms a self healing radio network with optional repeaters. Each
               * repeater and gateway builds a routing tables in EEPROM which keeps track of the
               * network topology allowing messages to be routed to nodes.
               *
               * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
               * Copyright (C) 2013-2015 Sensnology AB
               * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
               *
               * Documentation: http://www.mysensors.org
               * Support Forum: http://forum.mysensors.org
               *
               * This program is free software; you can redistribute it and/or
               * modify it under the terms of the GNU General Public License
               * version 2 as published by the Free Software Foundation.
               *
               *******************************
               *
               * REVISION HISTORY
               * Version 1.0 - Henrik EKblad
               * 
               * DESCRIPTION
               * This sketch provides an example how to implement a humidity/temperature
               * sensor using DHT11/DHT-22 
               * http://www.mysensors.org/build/humidity
               */
               
              #include <SPI.h>
              #include <MySensor.h>  
              #include <DHT.h>  
              
              #define CHILD_ID_HUM 0
              #define CHILD_ID_TEMP 1
              #define HUMIDITY_SENSOR_DIGITAL_PIN 3
              unsigned long SLEEP_TIME = 60000; // Sleep time between reads (in milliseconds)
              
              MySensor gw;
              DHT dht;
              float lastTemp;
              float lastHum;
              boolean metric = true; 
              MyMessage msgHum(CHILD_ID_HUM, V_HUM);
              MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
              int node_id = 22;
              
              
              void setup()  
              { 
                gw.begin();
                dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
              
                // Send the Sketch Version Information to the Gateway
                gw.sendSketchInfo("Humidity", "1.0");
              
                // Register all sensors to gw (they will be created as child devices)
                gw.present(CHILD_ID_HUM, S_HUM);
                gw.present(CHILD_ID_TEMP, S_TEMP);
                
                metric = gw.getConfig().isMetric;
              }
              
              void loop()      
              {  
                delay(dht.getMinimumSamplingPeriod());
              
                float temperature = dht.getTemperature();
                if (isnan(temperature)) {
                    Serial.println("Failed reading temperature from DHT");
                } else if (temperature != lastTemp) {
                  lastTemp = temperature;
                  if (!metric) {
                    temperature = dht.toFahrenheit(temperature);
                  }
                  gw.send(msgTemp.set(temperature, 1));
                  Serial.print("T: ");
                  Serial.println(temperature);
                }
                
                float humidity = dht.getHumidity();
                if (isnan(humidity)) {
                    Serial.println("Failed reading humidity from DHT");
                } else if (humidity != lastHum) {
                    lastHum = humidity;
                    gw.send(msgHum.set(humidity, 1));
                    Serial.print("H: ");
                    Serial.println(humidity);
                }
              
                gw.sleep(SLEEP_TIME); //sleep a bit
              }
              
              
              1 Reply Last reply
              0
              • L Offline
                L Offline
                lxz
                wrote on last edited by lxz
                #7

                I have the same. For the temperature I always thought that there's less variation in values.
                Did you try removing the if (temperature != lastTemp) from the sketch to see if more values are registered?

                HenryWhiteH 1 Reply Last reply
                0
                • AWIA Offline
                  AWIA Offline
                  AWI
                  Hero Member
                  wrote on last edited by
                  #8

                  I can confirm that 3.3v in combination with some DHT22 sensors is not a stable solution. I switched to si7021/ sht21 completely (more reliable/ lower consumption/ etc.). other experiences

                  1 Reply Last reply
                  2
                  • L lxz

                    I have the same. For the temperature I always thought that there's less variation in values.
                    Did you try removing the if (temperature != lastTemp) from the sketch to see if more values are registered?

                    HenryWhiteH Offline
                    HenryWhiteH Offline
                    HenryWhite
                    wrote on last edited by HenryWhite
                    #9

                    @lxz said:

                    I have the same. For the temperature I always thought that there's less variation in values.

                    well, i just realised that, goddammit :smile:

                    Since only every 10th reading or so is a failure, i can live with that I think.

                    @AWI said:

                    I can confirm that 3.3v in combination with some DHT22 sensors is not a stable solution. I switched to si7021/ sht21 completely (more reliable/ lower consumption/ etc.). other experiences

                    thanks for the hint. I think I will switch to the si7021 in my pcb design, it seems to be almost the same price as the dht22.
                    Another solution would be to power the DHT22 directly with 3.7V from my Lithium Battery instead the 3.3V from arduino's VCC.

                    1 Reply Last reply
                    0
                    • scalzS Offline
                      scalzS Offline
                      scalz
                      Hardware Contributor
                      wrote on last edited by
                      #10

                      yes, and with si7021 there is no delay, faster readings, i2c better, low power. I like it too :)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      26

                      Online

                      11.7k

                      Users

                      11.2k

                      Topics

                      113.1k

                      Posts


                      Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • MySensors
                      • OpenHardware.io
                      • Categories
                      • Recent
                      • Tags
                      • Popular