RTC ds1302 seemingly random reset

Me and a friend are working on building a fully automatic greenhouse with arduino. We use relais for highly powered stuff like fans and the pump. We have 3 relais, 2 servo's and a RTC module. But whenever we boot the arduino up, the RTC seemingly randomly resets. We think it has something to do with the pump because when we don't plug the pump in, it works just fine. The RTC we use is the ds1302. The arduino used is the arduino uno (non-chinese ripoff, the original). The power for the pump is from a usb-splitter, not from the arduino. This is the code. Don't think that's the problem, but it may be:

#include <virtuabotixRTC.h>
#include <Servo.h>
bool lightVar = false;
bool extravar = false;
virtuabotixRTC myRTC(A4, 8, 9);
Servo ServoOut;
Servo ServoIn;
void setup() {
  ServoOut.attach(10);
  ServoIn.attach(10);
  Serial.begin(9600);
 // seconds, minutes, hours, day of the week, day of the month, month, year
  myRTC.setDS1302Time(0, 0, 0, 7, 23, 5, 2021);
  for (int i = 2; i < 6; i++) {
    pinMode(i, OUTPUT);
  }
}

void loop() {
  digitalWrite(3, LOW);
  myRTC.updateTime();
  Serial.print("Seconds:");
  Serial.println(myRTC.seconds);
  Serial.print("Minutes:");
  Serial.println(myRTC.minutes);
  Serial.print("Hours:");
  Serial.println(myRTC.hours);
  delay(200);
  Pump();
//  Light();
//  Air();
}

//pin 4
void Pump() {
  if (myRTC.seconds < 25 && myRTC.minutes == 0) {
    digitalWrite(4, !lightVar);
  } else {
    digitalWrite(4, LOW);
  }
}

//pin 3
void Light() {
  if (myRTC.hours >= 22 && myRTC.minutes >= 30) {
    lightVar = true;
  } else if (myRTC.hours >= 23 || myRTC.hours <= 8) {
    lightVar = true;
  } else {
    lightVar = false;
  }
  digitalWrite(3, lightVar);
}

//pin 5
void Air() {
  if (myRTC.minutes < 5) {
    ServoOut.write(170);
    ServoIn.write(170);
    digitalWrite(5, !lightVar);
  } else {
    ServoOut.write(10);
    ServoIn.write(10);
    digitalWrite(5, LOW);
  }
}

Already thanks in regard,

MokerDikkeKloteKoter

Schematic, wiring diagram, image of the project and the wiring.

Ground connected together? Spike suppression on the relay coils? Powering the motors/pumps through the breadboard?

Well done, posting the code using code tags! But as @ Idahowalker says, draw a wiring diagram and post it. Don't use the useless Fritzings!

Just checked, no ground connected together, i am not powering the motors or pumps through the breadboard. I just made a rough scheme in tinkercad. They don't have pumps, fans, RTC modules or 12Volt connections, so i had to improvise and be a little creative:). The 9v block is the 12v supply. My friend noticed that the RTC module always went to 79 seconds after 20-22 seconds, and gets stuck there. Already thanks a lot!
The link: Login | Tinkercad

Have you read Common ground and why you need one](https://forum.arduino.cc/t/common-ground-and-why-you-need-one/626215)?

You are using a 9V battery? One of those square goes into a smoke detector kind of thing? You'll find that will be an error.

The "Smoke detectors" are relais, and the 9volt battery is a 12V power supply. The fans are configured to if the pin on the relais is high, the fans are on, same counts for the pump. Leds are switched opposite, so if the pin on the relais is true, the light will go off. I'm sorry if i wasn't clear or if my sketch isn't clear. I don't know whether i explained my problem right or not. I'm sorry if i did. I plugged everything in the right way, my RTC is giving wrong indicators. So the pump and fans go on when they shouldn't. Nothing to do with the several relais or the amount of power consumed(i think).

Update: I slept a night above it, and it magically improved. I don't have any idea what could have fixed it, but i am happy it just works! I think that's just how electronics work sometimes. @Idahowalker thanks a lot for your help and time!

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