Very simple relay and timer

Hello. It is my very first project and I am using Arduino nano + ds1307 + 8 channel relay module for that. The code turns on the lights but it does not turn them off. and if I turn the Arduino on at any time between "Alarms" relay does not work properly. please help me to fix the code. Thank you.
PS: Why there is 6min difference between my system time and 1307??

#include <TimeLib.h>
#include <TimeAlarms.h>
#include <Wire.h>
#include <DS1307RTC.h>


const int Relay1 = 2; 
const int Relay2 = 3;
const int Relay3 = 4;
const int Relay4 = 5;
const int Relay5 = 6;


void setup() {
  // prepare pin as output
  pinMode(Relay1, OUTPUT);
  digitalWrite(Relay1, HIGH);

  pinMode(Relay2, OUTPUT);
  digitalWrite(Relay2, HIGH);

  pinMode(Relay3, OUTPUT);
  digitalWrite(Relay3, HIGH);

  pinMode(Relay4, OUTPUT);
  digitalWrite(Relay4, HIGH);

  pinMode(Relay5, OUTPUT);
  digitalWrite(Relay5, HIGH);


  
  Serial.begin(9600);
  // wait for Arduino Serial Monitor
  while (!Serial) ; 
  
  // get and set the time from the RTC
  setSyncProvider(RTC.get);   
  if (timeStatus() != timeSet) 
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");  
        
  // to test your project, you can set the time manually 
  //setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011

  // create the alarms, to trigger functions at specific times
  Alarm.alarmRepeat(23,1,0,Light1On);  // xx:xxam every day
  Alarm.alarmRepeat(23,3,0,Light2On);
  Alarm.alarmRepeat(23,5,0,Light3On);
  Alarm.alarmRepeat(23,7,0,Light4On);
  Alarm.alarmRepeat(0,34,0,FanOn);
  Alarm.alarmRepeat(7,0,0,Light1Off);  // xx:xx -> x:xx every day
  Alarm.alarmRepeat(7,0,0,Light2Off);
  Alarm.alarmRepeat(7,0,0,Light3Off);
  Alarm.alarmRepeat(9,0,0,Light4Off);
  Alarm.alarmRepeat(0,30,0,FanOff);
}



void loop() {
  digitalClockDisplay();
  // wait one second between each clock display in serial monitor
  Alarm.delay(3000); 
}


// functions to be calRelay1 when an alarm triggers
void Light1On() {
  // write here the task to perform every morning
  Serial.println("Turn light1 on");
  digitalWrite(Relay1, LOW);
}
void Light2On() {
  Serial.println("Turn light2 on");
  digitalWrite(Relay2, LOW);
}
void Light3On() {
  Serial.println("Turn light3 on");
  digitalWrite(Relay3, LOW);
}
void Light4On() {
  Serial.println("Turn light4 on");
  digitalWrite(Relay4, LOW);
}void FanOn() {
  Serial.println("Turn light Veg. on");
  digitalWrite(Relay5, LOW);
}


void Light1Off() {
  // write here the task to perform every evening
  Serial.println("Turn light1 off");
  digitalWrite(Relay1, HIGH);
}
void Light2Off() {
  Serial.println("Turn light2 off");
  digitalWrite(Relay2, HIGH);
}
void Light3Off() {
  Serial.println("Turn light3 off");
  digitalWrite(Relay3, HIGH);
}

void Light4Off() {
  Serial.println("Turn light4 off");
  digitalWrite(Relay4, HIGH);
}

void FanOff() {
  Serial.println("Turn Veg. off");
  digitalWrite(Relay5, HIGH);
}




void digitalClockDisplay() {
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.println();
}
void printDigits(int digits) {
  Serial.print(":");
  if (digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

Can you post a schematic, not a frizzy thing showing your circuit. Be sure to include all power and ground connections. Your description is weak, and has no real information. Best to start with one part of your design and make it work before adding another section. A rule to follow is if you do not understand the software do not use it or study it until you do. Does not work properly tells me nothing, you have not defined properly, what is expected and what you get.

Hello there!
There it is, except I have been used 8ch relay module and ds1307 for clock.
Well, for example I want to lights stay on from 23:00 to 7:00. The relay turns lights on at 23 but it can not turn them off. And when I turn on the system between 23 and 7, the lights (relay) does not work.

Your relay board should be wired as thus:

The Arduino has its own 5 volt power supply.

larryd:
Your relay board should be wired as thus:

The Arduino has its own 5 volt power supply.

I changed the wiring according to your diagram. Problems is still ongoing. Plus rtc is malfunctioned. I can not set the time by system. At the beginning it was 6min difference. But now I just can't.

Screenshot 2021-03-08 123708.jpg

Screenshot 2021-03-08 123708.jpg

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