Arduino Freezing at Random

Hello Arduino community! I return to the forums hoping to get some input on a problem I am having with my project. I have an Arduino UNO with an RCT module that reads a humidity sensor and switches on/off relays depending on environmental inputs. This year I switched to a new humidity sensor type and am encountering an issue I haven't had happen before. The microcontroller will simply freeze at random. I am an amateur at best when it comes to programming and have to wonder if there isn't something I am overlooking in my sketch. I would really appreciate if somebody could take a look and see if there is something I am missing here.

Just some things that I have noticed. When the controller freezes the LCD freezes to the last read data. The LCD does not update any information, as if it is stuck on the last piece of data read. When I reset the controller everything runs as normal.

Question 1: is it possible that the battery on the RTC is going bad and causing the system to freeze up? That is the best starting point I have.

#include <LiquidCrystal_I2C.h>
#include <Adafruit_HTU21DF.h>
#include <DS3232RTC.h>
#include <Time.h>
#include <Wire.h>

LiquidCrystal_I2C lcd(0x27,16,2);
Adafruit_HTU21DF htu = Adafruit_HTU21DF();


void setup() {
  
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  
  
 Serial.begin(9600);
   
 setTime(11,1,0,5,3,18);
 
 lcd.init();
 lcd.backlight();
 
 
 lcd.println("HTU21D-F test");
 
 if (!htu.begin()) {
   lcd.println("Couldn't find sensor!");
   while (1);
 }  
}

void loop() 
{
 digitalClockDisplay();
 fanTimer(); 
 lightTimer();
 humiditySensor();
  delay(1000);
}

void digitalClockDisplay(){
    
 // digital clock display of the time
 lcd.setCursor(0, 0);
 lcd.print("Time ");
 lcd.print(hour());
 printDigits(minute());
 printDigits(second());
 lcd.println();
}
 
void printDigits(int digits){
 // utility function for digital clock display: prints preceding colon and leading 0
 lcd.print(":");
 if(digits < 10)
 lcd.print('0');
 lcd.print(digits);
} 



void fanTimer(){
if (
    ( minute() >= 0 && minute() < 4)
    //|| ( minute() >= 12 && minute() < 14)
    || ( minute() >= 20 && minute() < 24)
    //|| ( minute() >= 32 && minute() < 34)
    || ( minute() >= 40 && minute() < 44))
    //|| ( minute() >= 52 && minute() < 54))
{
  digitalWrite ( 4, LOW );
}
else
{
  digitalWrite ( 4, HIGH );
}    
}

void lightTimer(){
if ( hour() >= 6 && hour() < 18)
{
digitalWrite ( 5, LOW );
}
else
{
  digitalWrite ( 5, HIGH);
}
}   

void humiditySensor() {
  delay (1000);
 float h = htu.readHumidity();
  
  lcd.setCursor(0,1);
  lcd.print( "Hum: "); 
  lcd.println(htu.readHumidity());
   delay(1000);
  
  
  if (h <= 89.99)
  {
    digitalWrite (6, LOW);
  }
  else 
  {
    digitalWrite (6, HIGH);
  }
    
}

How do you power it ? Can you measure the voltage of the Arduino 5V pin ? How long are the I2C wires ? Is the ground current from the relais near the Arduino board ? What do you switch with the relais ? The mains voltage ?
The electrical pulse from a relay coil could cause a drop in the 5V. The LCD requires 5V and stops with less. The I2C could halt a sketch if for example an electrical pulse from switching the mains causes noise in the I2C bus.

The LCD displays are known to be very sensitive for electrical noise. Even when the display is in a metal case.
The Wire library for the I2C bus is known for halting the sketch when there is problem with the I2C bus.

The Arduino is powered by a normal arduino power adapter. The arduino is supplying power via the 5v pin to the project board with all of the accessories. These would be the LCD screen, HTU21DF sensor, RTC module and the Relay board. I am not sure the length of the wires leading from each of the accessories attached to the main I2C wires but I would guess they are between 1.5-3 inches. The wires that lead from the project board to the arduino are about 8 inches. The ground wire leading to the relays is nowhere near the arduino and is running from the rails of the project board to the relay board. All wires are shielded and relatively neat. I have attached an image of the setup.

Powering relay board and associated relays from Arduino 5V is very bad idea.

.

Here is the image.

cmwilliams:
The Arduino is powered by a normal arduino power adapter. The arduino is supplying power via the 5v pin to the project board with all of the accessories. These would be the LCD screen, HTU21DF sensor, RTC module and the Relay board. I am not sure the length of the wires leading from each of the accessories attached to the main I2C wires but I would guess they are between 1.5-3 inches. The wires that lead from the project board to the arduino are about 8 inches. The ground wire leading to the relays is nowhere near the arduino and is running from the rails of the project board to the relay board. All wires are shielded and relatively neat. I have attached an image of the setup.

ieee488:
Powering relay board and associated relays from Arduino 5V is very bad idea.

.

I have never had a problem with this in the past. Is there an alternative here?

Koepel:
How do you power it ? Can you measure the voltage of the Arduino 5V pin ? How long are the I2C wires ? Is the ground current from the relais near the Arduino board ? What do you switch with the relais ? The mains voltage ?
The electrical pulse from a relay coil could cause a drop in the 5V. The LCD requires 5V and stops with less. The I2C could halt a sketch if for example an electrical pulse from switching the mains causes noise in the I2C bus.

The LCD displays are known to be very sensitive for electrical noise. Even when the display is in a metal case.
The Wire library for the I2C bus is known for halting the sketch when there is problem with the I2C bus.

So I just tested the voltage on the 5v rail on the breadboard. All three pins (4,5,6) are low.
When pins 4, 5, and 6 are "low" voltage on the 5v rail measured to 5.8v
When pins 5, and 6 are "low" voltage on the 5v rail measured 5.2

When pins 4 and 6 are low while 5 is high voltage measured 5v. Would these fluctuations cause the board to freeze up when pin 4 switches and all three are high?

A change of 0.8 V is a lot. It could be the power supply or the wiring. If the power supply is the problem, perhaps it has voltage peaks as well.

Is your multimeter good quality ? What if the 5.8 V is actually 5.0 V and the 5.0 V is actually 4.2 V. A LCD display requires more than 4.2 V.

What is the voltage of the power supply that you connected to the power barrel jack ? I prefer 7.5 V. If that is 9 V or 12 V, then perhaps the voltage regulator gets hot and can not suppy enough current for the relays.

The relays should not be powered via de 5V pin of the Arduino board.
I even prefer that the relays are connected on a seperate breadboard. When the returning ground current of a relay is on the same breadboard as the sensors, that could stop the I2C bus.

If something has been working for a long time and suddenly does not work, that happens to others as well. It does not mean something. Perhaps it became summer and the room temperature has raised. Perhaps the breadboard connectors do not make contact as good as before. Anything is possible.

You could do a test. Disconnect the relays or replace the relays with leds (use resistors with the leds) and see if it runs.

Koepel:
A change of 0.8 V is a lot. It could be the power supply or the wiring. If the power supply is the problem, perhaps it has voltage peaks as well.

Is your multimeter good quality ? What if the 5.8 V is actually 5.0 V and the 5.0 V is actually 4.2 V. A LCD display requires more than 4.2 V.

What is the voltage of the power supply that you connected to the power barrel jack ? I prefer 7.5 V. If that is 9 V or 12 V, then perhaps the voltage regulator gets hot and can not suppy enough current for the relays.

The relays should not be powered via de 5V pin of the Arduino board.
I even prefer that the relays are connected on a seperate breadboard. When the returning ground current of a relay is on the same breadboard as the sensors, that could stop the I2C bus.

If something has been working for a long time and suddenly does not work, that happens to others as well. It does not mean something. Perhaps it became summer and the room temperature has raised. Perhaps the breadboard connectors do not make contact as good as before. Anything is possible.

You could do a test. Disconnect the relays or replace the relays with leds (use resistors with the leds) and see if it runs.

I'm not certain but I'm pretty sure that the main power supply for the arduino is a standard 9v power supply. I am going to rewire the breadboard to have it's own 5v power supply specifically for the relay board. The wiring and solder contacts are fresh this year and were tested individually with my multimeter so I'm doubting it's a bad solder joint.
So in your opinion the relay shield should be running on it's own 5v power supply. Do you think that the lcd, htu21df, and rtc will still be fine on the arduino's 5v supply? I also noticed that the ground and neutral wires running to the outlets runs right next to the arduino. Could this be the source of the voltage drop?
I will post a photo of this as well.

Photo of ground and neutral

The mains wires running next to the Arduino board should not be a problem in my opinion. When you make the SDA and SCL wires go to the right and make a 90° angle with the mains, then that should avoid any problem.

The only thing that uses some power is the backlight of the display. With a 9V power supply, I think it should work.
If it is on for some time (10 minutes) you could put your finger on the voltage regulator to test its temperature. That is the black component next to the barrel jack.

It is possible to use a single 5V power supply for everything. Then I prefer to power the Arduino board via the USB connector, because then the polyfuse is used.

Do you know someone with a good multimeter to calibrate your multimeter ? Because measuring 5.8 V at the Arduino 5V pin is not normal. There is probably a potentiometer inside your multimeter to adjust it.
If you measured 5.8 V for real, then perhaps the voltage regulator is broken and you need to replace the Arduino board.