DHT and LDR separately working, but together not, what's wrong?

  • Contest Winner

    @DannyM You still have some failures. I noticed that they're present in the Humidity only sketch as well.

    In my experience it's better to power nothing from the Arduino. The radio only works always, but using a phone charger will give you 5v and using a buck boost converter (80 euro cents on aliexpress) can provide 3.3V. As an alternative for the buck converter a low drop LE33 works perfectly for me.

    But please attache capacitor to all radio's (gateway and nodes). And try to power as less as you can from the arduino. The arduino just wasn't designed to deliver a lot of current.

  • Maybe This has something to do with it:


    The Lightsensor isn't belonging to the DHT sensor.. I think 😕

  • Contest Winner

    @DannyM Could be. But I doubt it. Look here.

    TempHum only:
    send: 1-1-0-0 s=255,c=0,t=17,pt=0,l=3,sg=0,st=fail:1.5
    send: 1-1-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=fail:0

    st=fails means that the radio can't deliver the message to the gateway. If a message could be delivered st will be ok

    Failed reading temperature from DHT
    Failed reading humidity from DHT
    send: 1-1-0-0 s=2,c=1,t=23,pt=7,l=5,sg=0,st=ok:6.9

    The failed reading is certainly caused by initializing the DHT with the wrong pin.

    Anyway I hope that you're able to get this to work with the help of others. I wouldn't be much online this weekend. But I'm hoping to see a positive post from you in this thread (meaning you got it working ;-)).

    Might be a good idea to start with the humidity temp sensor only. And see if you can get to work without any st=fail's in your log. Then you can go to the next step.

  • Yes, that did it,

    Thanks @sundberg84 and @TheoL for your time..

  • @TheoL I use a telephone charger most of the time, I'm a beginner with all.
    Hope to make this a battery oparated sensor, will search for the components you mentioned.


  • Hi all,

    Now I have bought a stepdown regulator to regulate the power for the radio.
    The readings of the sensor are the same as not using the stepdown regulator. So what can be wrong?
    Or is it not possible to use a LDR and a DHT11 together?

  • @DannyM

    Yes you can use LDR and a DHT11 together.

     * 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.
     * Version 1.0 - Henrik EKblad
     * Example sketch showing how to measue light level using a LM393 photo-resistor 
     * http://www.mysensors.org/build/light
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    // NodId
    #define MY_NODE_ID 99
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    //Including nescessary libraries
    #include <SPI.h>
    #include <MySensors.h> 
    #include <DHT.h>  
    // Set this offset if the sensor has a permanent small offset to the real temperatures
    #define SENSOR_TEMP_OFFSET 0
    // Sleep time between sensor updates (in milliseconds)
    // Must be >1000ms for DHT22 and >2000ms for DHT11
    static const uint64_t UPDATE_INTERVAL = 60000;
    // Force sending an update of the temperature after n sensor reads, so a controller showing the
    // timestamp of the last update doesn't show something like 3 hours in the unlikely case, that
    // the value didn't change since;
    // i.e. the sensor would force sending an update every UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms]
    static const uint8_t FORCE_UPDATE_N_READS = 10;
    //Defining child ID
    #define CHILD_ID_LIGHT 0
    #define CHILD_ID_HUM 1
    #define CHILD_ID_TEMP 2
    //Defining Pin
    #define DHT_DATA_PIN 3
    #define LDR_PIN A0
    //Defining values to store sensor information
    float lastTemp;
    float lastHum;
    uint8_t nNoUpdatesTemp;
    uint8_t nNoUpdatesHum;
    bool metric = true;
    int lastLightLevel;
    //Defining sleep and wait times
    unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)(30 seconds)
    #define MESSAGEWAIT 500
    MyMessage LightMsg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    DHT dht;
    void presentation()  {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Temp Fukt Ljus", "1.0"); // 2017 02 05
    //  wait(MESSAGEWAIT);
      // Register all sensors to gateway (they will be created as child devices)
    //  wait(MESSAGEWAIT);
      present(CHILD_ID_HUM, S_HUM);
    //  wait(MESSAGEWAIT);
      present(CHILD_ID_TEMP, S_TEMP);
        metric = getControllerConfig().isMetric;
    void setup() 
      dht.setup(DHT_DATA_PIN); // set data pin of DHT sensor
      if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) {
        Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
      // Sleep for the time of the minimum sampling period to give the sensor time to power up
      // (otherwise, timeout errors might occure for the first reading)
    void loop() {
      // Force reading sensor, so it works also after sleep()
      // Get temperature from DHT library
      float temperature = dht.getTemperature();
      if (isnan(temperature)) {
        Serial.println("Failed reading temperature from DHT!");
      } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) {
        // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
        lastTemp = temperature;
        if (!metric) {
          temperature = dht.toFahrenheit(temperature);
        // Reset no updates counter
        nNoUpdatesTemp = 0;
        temperature += SENSOR_TEMP_OFFSET;
        send(msgTemp.set(temperature, 1));
        #ifdef MY_DEBUG
        Serial.print("T: ");
      } else {
        // Increase no update counter if the temperature stayed the same
      // Get humidity from DHT library
      float humidity = dht.getHumidity();
      if (isnan(humidity)) {
        Serial.println("Failed reading humidity from DHT");
      } else if (humidity != lastHum || nNoUpdatesHum == FORCE_UPDATE_N_READS) {
        // Only send humidity if it changed since the last measurement or if we didn't send an update for n times
        lastHum = humidity;
        // Reset no updates counter
        nNoUpdatesHum = 0;
        send(msgHum.set(humidity, 1));
        #ifdef MY_DEBUG
        Serial.print("H: ");
      } else {
        // Increase no update counter if the humidity stayed the same
      int lightLevel = (1023-analogRead(LDR_PIN))/10.23; 
      if (lightLevel != lastLightLevel) {
          lastLightLevel = lightLevel;

    LDR pin A0

    DHT pin 3

    And i use pro mini.


  • @MLs

    Hi, thanks for your input, whit setup of the pins you used I have no more failed to read from DHT sensors in my serial monitor.

    Now I need to revise my sketch, so it will send correct values.

    Thanks again!

  • @DannyM


    I use a 10k resistor to LDR.

  • Hi all,

    All works fine now! After the reshuffle of the connection pins it does what I want it to do.
    Only thing I need to change is the sleeptime, it's now set to 6000 and when no change send a value to the GW after ten readings. But it is to often now..