ESP32 Wroom 32 + DHT22 Sensor Acting up with Relay On Off

Hi Everyone,

I am building a Project to control a pool pump. (ESP32 Wroom32)

There are specific conditions when the pool pump must engage and disengage.
Everything works 100% but I have a DHT22 Sensor that is Acting up Randomly when The Relay Switches off, then the DHT22 Sensor "freezes". (Startup is fine).

I have other Temp sensors on as well to Monitor the Pool Temp and Roof Temp DS18B20 Waterproof Sensors, they continue to read normal when the relay switches off.
I have tried 2x Libraries for the DHT22 Sensor, the one Displays NaN the Other-Current One Freezes.

The DHT22 Sensor Values does nothing to the Operation, but I would like to display the Indoor temp and humid on the screen. (without having to restart the esp32 everyday)
I have tried to install a different DHT22 Sensor as well (Same issues)

I am powering Everything on the ESP32 with 3.3V and the Relay with 5V.
The Power supply Module takes in 9V and then Splits it into 3.3 and 5V Rails that can be used.
Connected to ESP32 is:
3x 5mmLED's (3.3V)
1x Pushbutton (3.3V)
1x 3.5" TFT Display (3.3V)
1x DHT22 Sensor (3.3V)
2x DS18B20 Sensors (3.3V)
1x DS3231 RTC Module (3.3V)
1x 30amp TongLing Relay (This Handles 220V To Switch On the PoolPump) (5V)

Code is as Follows:

#include <Arduino.h>
#include <TFT_eSPI.h>
#include <SPI.h>
#include <RTClib.h>
#include <OneWire.h>          
#include <DallasTemperature.h> 
#include <DHT22.h>
#include "font.h"
#include "powerIcon.h"
#include "pumpIcon.h"
#include "bypassIcon.h"


#define ONE_WIRE_BUS    25  // DS18B20 Temp Sensors
#define POWER_LED       32
#define POOL_PUMP_LED   33 
#define BYPASS_LED      27  
#define BYPASS_BTN      5
#define RELAY_PIN       14



// Setup a oneWire instance to communicate with any OneWire devices &
// Pass oneWire reference to Dallas Temperature.
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature tempSensors(&oneWire);
// DS18B20 Temp Sensor Addresses.
uint8_t roofSensor[8] = { 0x28, 0xD3, 0x02, 0x48, 0xF6, 0xD2, 0x3C, 0x74 };
uint8_t poolSensor[8]   = { 0x28, 0xFF, 0x64, 0x1E, 0x0E, 0x78, 0x25, 0x43 };



// ############################ TFT Display Constructor and Sprite Constructor ####################
//                              480 x 320 TFT Display
TFT_eSPI tft = TFT_eSPI();
TFT_eSprite sprite = TFT_eSprite(&tft);

// ############################ RTC Instance for DS3231 RTC #######################################
//
RTC_DS3231 rtc;

// ############################ Humidity / Temp Sensor (DHT22) ####################################
//
DHT22 dht (19);  


//
// ############################ Project Variables #################################################
//
bool    byPassON           = false;            // Bypass On/Off Value
float   roofTemp           = 0.00;             // DS18B20 Roof Temp
float   lapaTemp           = 0.00;             // Store Lapa Temp DHT22 Sensor
float   lapaHumid          = 0.00;             // Store Lapa Humid DHT22 Sensor
const float activationTemp = 25.00;            // Adjust this Temp to activate the pool pump (mimimum temp)
const int timeIntervals[4] = {0, 15, 30, 45};  // 15min Intervals to activate pool pump

int bypassLedState = LOW;                      // Store the Bypass LED State (As it changes)
unsigned long blinkLastMillis = 0;             // Store last Millis to blink LED every 1 Sec




void setup() {
  // Call this function to setup the date and time for the fist time.
  // January 21, 2014 at 3am you would call:
  //rtc.begin();
  //rtc.adjust(DateTime(2024, 4, 27, 17, 25, 0));
  Serial.begin(9600);
  rtc.begin();
  tempSensors.begin();
  pinMode(POWER_LED, OUTPUT);
  pinMode(POOL_PUMP_LED, OUTPUT);
  pinMode(RELAY_PIN, OUTPUT);
  pinMode(BYPASS_LED, OUTPUT);
  pinMode(BYPASS_BTN, INPUT_PULLUP);
  digitalWrite(POWER_LED, HIGH);

  tft.init();
  tft.setRotation(3);
  tft.fillScreen(TFT_BLACK);
  tft.setTextColor(TFT_WHITE, TFT_BLACK);
  tft.drawString("POOL PUMP CONTROLLER", 5, 5, 4);
  tft.drawString("Version | V1.0", 385, 300, 2);
  tft.drawLine(0, 30, 480, 30, TFT_WHITE);

  tft.setTextColor(TFT_WHITE, TFT_BLACK);
  tft.setFreeFont(&Open_Sans_Bold_30);
  tft.drawString("POOL TEMP               C",60 ,130);
  tft.drawString("LAPA TEMP                C",60 ,170);
  tft.drawString("ROOF TEMP               C",60 ,210);
  tft.drawString("HUMIDITY               %",60 ,270);
  tft.drawCircle(375, 130, 3, TFT_WHITE);
  tft.drawCircle(375, 173, 3, TFT_WHITE);
  tft.drawCircle(375, 210, 3, TFT_WHITE);

  

  sprite.createSprite(24, 24);
  sprite.setSwapBytes(true);
  sprite.pushImage(0, 0, 24, 24, power);
  sprite.pushSprite(450, 3, TFT_BLACK);
}

void loop() {
  displayMain();
  togglePoolPump();
  toggleBypass();
  delay(500);
}

//
// ################################################################################################
//                                                                                           
//                            Display All Main Temp Data                          
//                                                                                           
// ################################################################################################
//
void displayMain(){
  tempSensors.requestTemperatures();     // Read Temp Data from DS18B20 Sensors
  lapaTemp = dht.getTemperature();       // Read Temp and Humidity Data from DHT22 Sensor
  lapaHumid = dht.getHumidity();         //

  // Display time clock on the display.
  DateTime t = rtc.now();
  tft.setTextColor(TFT_GREENYELLOW, TFT_BLACK);
  tft.drawString(String(t.timestamp(DateTime::TIMESTAMP_TIME)),120, 50, 7);

  // display Temp Data on the Display
  float realPoolTemp = tempSensors.getTempC(poolSensor) - 1.00;  // deduct 1 degree from pool tmp.
  float realRoofTemp = tempSensors.getTempC(roofSensor) - 1.00; // deduct 1 degree from roof tmp.
  tft.setTextColor(TFT_WHITE, TFT_BLACK);
  tft.setFreeFont(&Open_Sans_Bold_30);
  tft.drawString(String(realPoolTemp),260 ,130);
  tft.drawString(String(lapaTemp),260 ,170);
  tft.drawString(String(realRoofTemp),260 ,210);
  tft.drawString(String(lapaHumid),260 ,270);
}

// ################################################################################################
//                                                                                           
//                            Activate / Deactivate Pool Pump Relay                          
//                                                                                           
// ################################################################################################
//
void togglePoolPump(){
  DateTime t = rtc.now();
  
  for(int i = 0; i < 4; i++){
    if(timeIntervals[i] == t.minute()){
      roofTemp = tempSensors.getTempC(roofSensor) - 1.00;
    }
  }

  if((t.hour() > 10) && (t.hour() < 15) && (roofTemp > activationTemp) ){
    digitalWrite(POOL_PUMP_LED, HIGH);
    digitalWrite(RELAY_PIN, HIGH);

    sprite.createSprite(24, 24);          // Display Pool Pump Icon
    sprite.setSwapBytes(true);
    sprite.pushImage(0, 0, 24, 24, pump);
    sprite.pushSprite(420, 3, TFT_BLACK);
  }
  else{
    digitalWrite(POOL_PUMP_LED, LOW);
    digitalWrite(RELAY_PIN, LOW);
    tft.fillRect(380, 3, 70, 24, TFT_BLACK);
  }
}

//
// ################################################################################################
//                                                                                           
//                            Activate / Deactivate Bypass                         
//                                                                                           
// ################################################################################################
//

void toggleBypass(){
  if (!digitalRead(BYPASS_BTN)) {        // Bypass ACTIVATE (Button Press) (HOLD)
    byPassON = true;

    delay(1000);
  }
  while (byPassON) {
    displayMain();                       // Display TEMP
    blinkBypassLED();                    // Display Bypass Indicator Blink LED
    digitalWrite(POOL_PUMP_LED, HIGH);   // Pool Pump ON !
    digitalWrite(RELAY_PIN, HIGH);       // Relay ON !

    sprite.createSprite(24, 24);         // Display Pool Pump Icon
    sprite.setSwapBytes(true);
    sprite.pushImage(0, 0, 24, 24, pump);
    sprite.pushSprite(420, 3, TFT_BLACK);

    sprite.createSprite(24, 24);        // Display Bypass Icon
    sprite.setSwapBytes(true);
    sprite.pushImage(0, 0, 24, 24, bypass);
    sprite.pushSprite(390, 3, TFT_BLACK);
    delay(500);
    if (!digitalRead(BYPASS_BTN)) {      // Bypass DEACTIVATE (Button Press) (HOLD)
      byPassON = false;
      digitalWrite(POOL_PUMP_LED, LOW);
      digitalWrite(RELAY_PIN, LOW);
      digitalWrite(BYPASS_LED, LOW);
      bypassLedState = LOW;
      tft.fillRect(380, 3, 70, 24, TFT_BLACK);
    }
  }
}



//
// ################################################################################################
//                                                                                           
//                            Bypass Indicator LED (Blink - LED)                          
//                                                                                           
// ################################################################################################
//
void blinkBypassLED(){
  unsigned long bypassBlinkMillis = millis();
  if(bypassBlinkMillis - blinkLastMillis >= 1000){
    blinkLastMillis = bypassBlinkMillis;
    if(bypassLedState == LOW){
      bypassLedState = HIGH;
    }else {
      bypassLedState = LOW;
    }
    digitalWrite(BYPASS_LED, bypassLedState);
  }
}

Does the relay trigger a pump?
If so, you are probably having electrical noise problems.
This can be resolved with noise reducers, such as snubbers.

I suggest you post a wiring schematic of your project.

I moved your topic to a more appropriate forum category @Fanie.

The Nano ESP32 category you chose is only used for discussions directly related to the Arduino Nano ESP32 board.

In the future, please take the time to pick the forum category that best suits the subject of your question. There is an "About the _____ category" topic at the top of each category that explains its purpose.

Thanks in advance for your cooperation.

Always show us a good schematic of your proposed circuit.
Show us good images of your ‘actual’ wiring.
Give links to components.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.