Arduino freezes after 30-90min

Hello everyone!

I've made a climate control for my greenhouse, but after a while the arduino doesn't update no more.
I had to switch out the heater because it was too weak and then the problems started. Before i switched heater, i didn't have this problem.
First i thought it was hardware or because of the heat and humidity, but same problems show up when i place the arduino out of the tent.

Here's my code:

// Temp, Humd = 998 betekend "sensor not detected"
#include <Wire.h>
#include <DS3231.h>
#include "SparkFunHTU21D.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); 
HTU21D myHumidity;
DS3231  rtc(A4, A5);
Time t;

unsigned long tod; 
bool dag;
bool lucht;
bool warm;
bool vocht;
bool licht;


// Eigen karakter maken op site:   https://arduinogetstarted.com/tutorials/arduino-lcd-i2c
byte customChar[8] = {
	0b01100,
	0b10010,
	0b10010,
	0b01100,
	0b00000,
	0b00000,
	0b00000,
	0b00000
};

void setup()
{
  Serial.begin(9600);
  Serial.println("Booting");

  myHumidity.begin();
  rtc.begin();
  lcd.begin();
  lcd.backlight();

  // TIJD RTC INSTELLEN
  //rtc.setDOW(MONDAY);     // Set Day-of-Week to SUNDAY
  //rtc.setTime(12, 44, 10);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(20, 3, 2023);   // Set the date to January 1st, 2014

  lcd.createChar(0, customChar); // create a new custom character

  pinMode(12, OUTPUT);   // Ultrasone mist maker 
  pinMode(11, OUTPUT);   // Buisverwarming
  pinMode(10, OUTPUT);   // Verlichting
  pinMode(9, OUTPUT);    // Luchtcirculatie

}

void loop()
{
  t = rtc.getTime();
  float humd = myHumidity.readHumidity();
  float temp = myHumidity.readTemperature();
  unsigned long tod = t.hour*60 + t.min;       // Tijd omzetten in RTC code in aantal minuten

 
  // LCD scherm opstelling
  lcd.clear();
  lcd.setCursor(0, 0);    // Cursor op positie (0, 0) zetten
  lcd.print("T: ");       // Text op gekozen positie
  lcd.setCursor(3, 0);
  lcd.print(temp, 1);     // Actieve temperatuurs weergave
  lcd.setCursor(7, 0);
  lcd.write((byte)0);
  lcd.setCursor(8, 0);
  // Onderste lijn
  lcd.setCursor(0, 1);    
  lcd.print("RV:");       // Text op gekozen positie
  lcd.setCursor(3, 1);
  lcd.print(humd, 1);     // Active relatieve vochtigheid
  lcd.setCursor(7, 1);
  lcd.print("%");
  lcd.setCursor(11, 1);   // Klok
  lcd.print(t.hour);
  lcd.setCursor(13, 1);
  lcd.print(":");
  lcd.setCursor(14, 1);
  lcd.print(t.min);

  // Seriële monitor
  Serial.print(dag);
  Serial.print(" : ");
  Serial.print(tod);
  Serial.print("   T: ");
  Serial.print(temp, 1);
  Serial.print("C");
  Serial.print("   R.V.: ");
  Serial.print(humd, 1);
  Serial.print("%    ");
  Serial.print("   Lucht: ");
  Serial.print(lucht);
  Serial.print("   Warm: ");
  Serial.print(warm);
  Serial.print("   Vocht: ");
  Serial.print(vocht);
  Serial.print("   Licht ");
  Serial.print(licht);

//Verluchting
if (tod >= 405 && tod <= 1339) {    // Tijd van "aan" en "uit" schakelen luchtCIRCULATIE
   digitalWrite(9, LOW);   
   lucht = true;
   } else if (humd >= 85.0) {          // Ook als RV boven 85% komt, aan gaan
  digitalWrite(9, LOW);
  lucht = true;
}
else {
  digitalWrite(9, HIGH);
  lucht = false;
}


// Temperatuur regeling
if (tod >= 410 && tod <= 1259) {                // DAG REGELING,    verwarming gaat 15min vroeger aan dan zonsopgang
  if (temp <= 21.7) {               // MIN  
    digitalWrite(11, LOW);         // Verwarming gaat AAN
    warm = true;
  } 
  if (temp >= 22.2) {               // MAX
    digitalWrite(11, HIGH);          // Verwarming gaat UIT
    warm = false;
  }
} else {                // NACHT REGELING
  if (temp <= 17.8) {               // MIN
    digitalWrite(11, LOW);         // Verwarming gaat AAN
    warm = true;
  } 
  if (temp >= 18.3) {               // MAX
    digitalWrite(11, HIGH);          // Verwarming gaat UIT
    warm = false;
  }
}

// Vocht regeling
if (tod >= 420 && tod <= 1319) { 
  if (humd <= 79.0) {                // MINIMUM vocht !!
    digitalWrite(12, LOW);           // Verwarming gaat AAN
    vocht = true;
  }
  if (humd >= 82) {                  // MAXIMUM vocht !!
    digitalWrite(12, HIGH);          // Verwarming gaat UIT
    vocht = false;
  }
} else {                             // S'nachts geen vocht meer bijgeven
  digitalWrite(12, HIGH);
  vocht = false;
}
 
 //Verlichtings schema
 if (tod >= 420 && tod <= 1319){     // Overdagse tijd
    digitalWrite(10, LOW);
    licht = true;
    } else{
      digitalWrite(10, HIGH);
      licht = false;
    }

  Serial.println();
  delay(5000);
}

Ur help would be appreciated!

You might want to read this;

"We need to know exactly what hardware you have. We need to know what Arduino board (or other brand, such as ESP32) you are using and exactly what other hardware you are connecting it to. The more details we have, the easier it is to help"

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


BTW
lcd.print((char)223); //this will print a °

Pardon me,

i'm working with an Arduino Uno R3. There's a HTU21d sensor for temperature and humidity, cables might be too long (around 0,75m) for correct readings.
Then there's a RTC ds3231 for setting daytime or nighttime, and a 16x2 I2C display.

The HTU21d is connected to the SCL and SDA pins, while the RTC and display are connected to A4 and A5 in parallel.

All the components like lighting, heating... are connected to a relay, controlled by the arduino.
Everything gets its power from the arduino 5V or 3.3V pins. The Arduino is fed with the USB cable with 5V.

If you run blink or some other test code, does it freeze up still?

Yes sir,
everything freezes at the last set values and keeps doing so until the arduino is reset.

What is the current draw of the relay coils? If directly driven, do you have flyback protection? Probably best to use a BJT to drive them. Again, need schematics and pictures.

The Arduino is not a power supply, well you can use it only for light loads.


Always show us a good schematic of your proposed circuit.

Show us a good image of your ‘actual’ wiring.

Were you responding to me? If so, I suggested using a different program like blink to see if the board or software is the issue.

Sorry for the mess, (planning to put it in a case later on)
(PS. Don't have a schematic)

Should have flyback diodes on the relay module.
And i've tried using an external power supply but the relay won't turn on even though it reads 5V over de GND and VCC.

Even though the modules have ESD protection, using them on the carpet is a bad idea. Also, what is the relay board model number?

Do not power that relay board from the Arduino 5v pin.


Show us how you tried to power that relay board from an external 5v power supply, i.e. in a schematic.


Are all those wires going to the Arduino PCB soldered to the circuit pads on the Arduino ?

That points to a hardware problem so we need a drawing of how the relays and heater are connected to Arduino. What is the difference between the new heater and the "weak" heater?


This is basically the schematic, except for the output pins, was not able to edit the wiring for some reason.

Here how i wired the external power supply to the (OT954-C103) relay.
And yes i soldered the wires to the arduino, had problems bad connections earlier.

The heater i had installed before was 45W. The new one draws 300W.

Update:

I connected the relay module the right way this time, with this wiring diagram i found in another topic.
image

It's still freezing, should i put the sensor, RTC or lcd on external power supply aswell?

Today, i've switched back to the other heater of 45W instead of the 300W one. Still freezing, so the heater wasn't the problem.
Maybe it could be the HTU21d sensor, which cables are too long (around 1m), also have tried using pull-up resistors on SDA & SCL, still freezing.

I think it's strange how before i changed the heater and placed the sensor further from the arduino, it wasn't freezing.

Have you ran any other programs (E.g. Blink) to verify the board still locks up after a period of time?

I would put Serial.print in between all sensor readings. The last print is before your problem.

I may get shredded for this but did you use String variables of any other command that uses dynamic memory allocation/deallocation?

GND must be connected between the components.

How long are the I2C wires? And where are they routed?
Are the wires SOLDERED to the pcb holes you are using?
If not: the wires need perfect contact, just sticking them in the holes will not work.
Use the headers instead!

1 Like