1
votes

I have an Arduino Pro Mini 5v, 16 mhz and it is connected to a digital switch on pin 2. This switch is used to wake the Arduino from sleep using a external digital interrupt. I also have a DHT11 temperature sensor connected to pin 9. What I want to achieve is the when the Arduino is awake for 5 seconds and also when the switch on pin 2 is HIGH, I want to read the temperature sensor and return the temperature. I am using the DHT11 library by Tillart and when I do this, it returns a TIME_OUT error. The only possible explanation I have for this is that somehow the voltage is changed when both the DHT11 and the switch on pin 2 is being read together? Any pointers to a solution will be greatly appreciated. Thank you.

Edit 1: Added code

        #include <LowPower.h>
        #include <dht.h>
        int pin2 = 2;
        dht DHT;
        #define DHT11_PIN 9

        void pin2interrupt(void)
        {
           // Function called when awoken from sleep
           // Detach interrupt to stop it from continuosly firing when in normal mode
        }

        void enterSleep(void)
        {
           attachInterrupt(0, pin2interrupt, HIGH);
           Serial.println("Sleeping");
           delay(100);
           LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
           Serial.println("Awake!");
        }


        void setup()
        {
           Serial.begin(115200);
           pinMode(pin2, INPUT);
           pinMode(DHT11_PIN, INPUT);
        }

        int seconds = 0;

        void loop()
        {
           delay(1000);
           seconds++;
           Serial.println("Awake in the loop!");
           Serial.println(seconds);

           if (digitalRead(pin2) == LOW && seconds == 5)
           {
               seconds = 0;
               Serial.println("No child detected, so going to sleep!");
               delay(200);
               enterSleep();
           }
           else if (seconds == 5)
           {
               Serial.print("DHT11, \t");
               int chk = DHT.read11(DHT11_PIN);
               switch (chk)
               {
                   case DHTLIB_OK:
                   Serial.print("OK,\t");
                   break;
                   case DHTLIB_ERROR_CHECKSUM:
                   Serial.print("Checksum error,\t");
                   break;
                   case DHTLIB_ERROR_TIMEOUT:
                   Serial.print("Time out error,\t");
                   break;
                   default:
                   Serial.print("Unknown error,\t");
                   break;
               }
            // DISPLAY DATA
            Serial.println(DHT.temperature, 1);

            delay(2000);
            seconds = 0;
            }
         }

Edit 2: I also forgot to to mention that I am using the LowPower library by RocketScream to put the Arduino to sleep. The library can be found here: https://github.com/rocketscream/Low-Power

1
@PatrickTrentin Sorry about that. Added the code. Hope its formatted correctly. - Jigten
would you try change attachInterrupt(0, pin2interrupt, HIGH); to attachInterrupt(0, pin2interrupt, RISING); and see what happens? And please rename seconds into loop_iterations. And replace seconds == 5 with seconds >= 5. - Patrick Trentin
@PatrickTrentin Thank you for taking time and giving me your suggestions. I will try them and report back the results. - Jigten
@PatrickTrentin I still get a TIME_OUT error. When i read the DHT11 alone, the sensor works fine. I also noticed that after I run this program and then run a program that just reads the DHT11 alone, the DHT11 responds with TIME_OUT error for a little while then functions back to normal. - Jigten
I am not sure what attachInterrupt(0, ...) is doing, according to the docs it should be attachInterrupt(digitalPinToInterrupt(pin2)). Since that didn't work, try add detachInterrupt(digitalPinToInterrupt(pin2)); to the code of pin2interrupt(). Perhaps it's better to take a look at the source code of dht and see what is the reason why it might take so long to read the value. - Patrick Trentin

1 Answers

0
votes

As discussed in the issues section on the official Github page of the DHT11 library by Rob Tillart, the problem is caused because some DHT11 sensors take longer to transfer data back to the board then the 50ms or so specified on the datasheet. Therefore, if you are encountering this problem try increasing the DHTLIB_TIMEOUT on the dht header file by reducing the the value dividing the F_CPU value to around 400 and try again. This allows the board to wait longer than 50ms for the board to receive data back from the sensor. If this fix doesn't work, you might want to try measuring the response time using an oscilloscope as it seems some DHT11 are built differently.