DHT-22 humidity temp sensor stops updating after about 30 minutes.
My display shows alternating temp in Celsius and humidity every 10 seconds and works OK
for about 30 minutes.
After that the temperature shows 0 C on the display and a humidity of 44.6%.
Only a restart makes it OK again for some time.
Tried adding a decoupling capacitor on the V+ and GND and various pull-up resistors (4.7 to 10 KOhm).
Wire connection to DHT-22 to board is about 20 cm.
Pump motor, push-button and Led still work as expected, so no infinite loops.
Can it be conflicting libraries or is my sensor broken?
Or is it something stupid in my code?
/*
Name: HumiditySwitch.ino
Created: 23/6/2021 12:56:53
Author: ajkes
Based on:
How to use the DHT-22 sensor with Arduino uno
Temperature and humidity sensor
https://www.makerguides.com/tm1637-arduino-tutorial/
<JC_Button.h> // https://github.com/JChristensen/JC_Button
Using ATmega328P(old bootloader, Nano)
*/
#include <arduino.h>
#include <DHT.h>
#include <TM1637Display.h>
#include <JC_Button.h>
#include <arduino-timer.h>
#include <Adafruit_Sensor.h>
//Constants
#define DHTPIN 4 // DHT sensor connects to this pin.
#define CLK 2 // CLK pin for display.
#define DIO 3 // DIO pin for display.
#define DHTTYPE DHT22 // DHT 22 (AM2302).
#define MOTOROUTPIN 11 // Humidity motor relay.
#define TEMPOUTPIN 12 // Heater relay.
#define BTNPIN 8 // Motor on/off button.
#define LEDPIN 9 // Motor on/off Led.
const uint8_t BUTTON_PIN(BTNPIN);
Button button(BUTTON_PIN);
const uint8_t celsius[] = { SEG_A | SEG_F | SEG_G };// Create lower case "C".
const uint8_t hum[] = {
SEG_F | SEG_E | SEG_C | SEG_E | SEG_G };// Create lower case "h".
int seconds;
int Ontime = 60;
int Offtime = 30;
bool ReadingHum = false;
float temperature;
float humidity;
int ledState = HIGH;
bool heater = LOW;
bool ReadOK = false;
TM1637Display display = TM1637Display(CLK, DIO); // Create display object of type TM1637Display:
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor
auto timer = timer_create_default(); // create a timer with default settings
/// <summary>
/// Callback from timer.every(1000UL * 5, UpdateReading);
/// Alternate between reading Hum and temp
/// </summary>
/// <param name=""></param>
/// <returns>bool</returns>
bool UpdateReading(void*)
{
if (ReadingHum)
{
ReadHum();
}
else
{
ReadTemp();
}
if (ReadOK)
{
//Switch heater ON or OFF
if (humidity <= 10)
{
//Serial.println("Hum < 10");
digitalWrite(TEMPOUTPIN, LOW);
heater = LOW;
}
else
{
digitalWrite(TEMPOUTPIN, HIGH);
heater = HIGH;
//Serial.println("Hum > 10");
}
//Prevent heater ON if temperature too high
if (temperature >= 40)
{
digitalWrite(TEMPOUTPIN, LOW);
heater = LOW;
//Serial.println("Temp 40+");
}
}
ReadingHum = !ReadingHum;
return true;
}
/// <summary>
/// Callback from timer.every(1000UL, UpdateSeconds);
/// </summary>
/// <returns>bool</returns>
bool UpdateSeconds(void*)
{
seconds++;
if (seconds < Ontime)
{
if (humidity > 15)
{
//Serial.println("Hum > 15");
digitalWrite(MOTOROUTPIN, ledState);// only run if pumpmotor is switched on(ledstate high
}
else
{
//Serial.println("Hum ! > 15");
digitalWrite(MOTOROUTPIN, LOW);
}
}
if (seconds >= Ontime)// only run pump-motor for fixed amount of time to prevent overheating
{
digitalWrite(MOTOROUTPIN, LOW);
if (seconds >= Ontime + Offtime)
{
seconds = 0;
}
}
//Serial.print("Updating Pump ");
//Serial.println(digitalRead(MOTOROUTPIN));
return true;
}
void ReadTemp()
{
temperature = dht.readTemperature();
if (temperature > -10 && !isnan(temperature))//check for false reading
{
ReadOK = true;
}
else
{
ReadOK = false;
//impossible number to show false temperature reading
temperature = 88.8;
}
//show on display
display.showNumberDecEx(temperature * 10, 0b11100000, false, 3, 0);
display.setSegments(celsius, 1, 3);
}
void ReadHum()
{
humidity = dht.readHumidity();
if (humidity > 0 && !isnan(humidity))
{
ReadOK = true;
}
else
{
humidity = 99.9;//impossible number to show false temperature reading
ReadOK = false;
}
display.showNumberDecEx(humidity * 10, 0b11100000, false, 3, 0);
display.setSegments(hum, 1, 3);
}
void setup()
{
Serial.begin(9600);
display.setBrightness(7);
pinMode(DHTPIN, INPUT_PULLUP);//Makes DHT reading more reliable
dht.begin();
pinMode(MOTOROUTPIN, OUTPUT);
pinMode(TEMPOUTPIN, OUTPUT);
button.begin();
pinMode(LEDPIN, OUTPUT);
digitalWrite(LEDPIN, ledState);
digitalWrite(MOTOROUTPIN, HIGH);
digitalWrite(TEMPOUTPIN, HIGH);
timer.every(1000UL * 5, UpdateReading);
timer.every(1000UL, UpdateSeconds);
}
void loop()
{
timer.tick();
delay(1);
button.read();
if (!ledState)
{
seconds = 0;
digitalWrite(MOTOROUTPIN, LOW);
}
// Returns true if the button state was released at the last call to read().
// Does not cause the button to be read.
if (button.isReleased()) // set led state to indicate pump-motor is ON or OFF
{
ledState = !ledState; // SET ledState TO THE OPPOSITE OF ledState
digitalWrite(LEDPIN, ledState); // WRITE THE NEW ledState
}
}
Compiler info:
Compiling 'HumidityControler' for 'ATmega328P (Old Bootloader) (Arduino Nano) (nano_atmega328old)'
Program size: 6.280 bytes (used 20% of a 30.720 byte maximum) (1,54 secs)
Minimum Memory Usage: 516 bytes (25% of a 2048 byte maximum)