RTC(DS3231)+Relay

HEY everyone

I am working on my project with automatic controlled aquarium(lights and airpump).I read many topics on this forum about this but I can´t find the right code for me.

The whole thing is that the relay will start in the morning and turn them off in the evening.
I want to controlled two relays(first for lights and second for airpump).
I find some code and edit it in this:

#include <Time.h>
#include <TimeAlarms.h>

// Pins
#define RELAY1  2                       
#define RELAY2  3

void setup() {
 
 // Start Serial & set pin to output 
 Serial.begin(9600);

//set relays
 pinMode(RELAY1, OUTPUT);       
 pinMode(RELAY2, OUTPUT);

// start clock
 setTime(18,00,00,7,1,17); // set time to 18:00 July 1 2017 

// create the alarms 
 Alarm.alarmRepeat(22,00,00, RelayOff);  // 22:00:00 every day
 
 Alarm.alarmRepeat(05,30,00, LightsON);  // 05:30:00 every day
 Alarm.alarmRepeat(22,00,00, LightsOFF);  // 22:00:30 every day  
 
 Alarm.alarmRepeat(06,00,00, AirPumpON);  // 06:00:00 every day 
 Alarm.alarmRepeat(22,00,00, AirPumpOFF);  // 22:00:00 every day


}

void loop() {

 digitalClockDisplay();
 Alarm.delay(1000); 

 
}

//Relays OFF
void RelayOff(){
 Serial.println("All Off"); 
digitalWrite(RELAY1,HIGH);           
digitalWrite(RELAY2,HIGH);             
 }
 
void LightsON(){
 Serial.println("Lights are ON"); 
 digitalWrite(RELAY1,LOW);           

  }

                                   
 void LightsOFF(){
 Serial.println("Lights are OFF");
 digitalWrite(RELAY1,HIGH);          

 }
  void AirPumpON(){
 Serial.println("AirPump is ON");
 digitalWrite(RELAY2,LOW);           

 }


 void AirPumpOFF(){
 Serial.println("AirPump is OFF");
 digitalWrite(RELAY2,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);
}

It´s work but i have some problems.

1.When I upload code and open Serial monitor the code started running from the beginning.
For example-the time from arduino are display on the serial monitor
18:00:01
18:00:02
18:00:03
18:00:04
18:00:05

I close and open serial monitor and the code started running from the beginning.
18:00:01
18:00:02
18:00:03
(but that is “ok”.I will not open Serial monitor.Second problem is bigger)

2.I want to use my rtc(DS3231) with this(because i live on village and the electricial system are often “falls”(for a several minutes)).And the code started running from the beginning when I pull out usb cable(oder electrical system “falls”)

I will be very happy when someone can help me :slight_smile:

–Sorry for my bad english–

Thanks :slight_smile:

Have you got your RTC working?

https://www.pjrc.com/teensy/td_libs_DS1307RTC.html

Yes I use code from this page http://www.rinkydinkelectronics.com/library.php?id=73 And all it works even if I pull out the cable from arduino(I try use my another RTC modul and all works too)

setTime(18,00,00,7,1,17); Having the above line in your sketch, resets the the time each time there is a restart and each time the serial monitor is opened.

You set the RTC time once only to get it set up, there after, you read time from the RTC as needed. Review some RTC examples so you fully understand how things work.

Examples https://learn.adafruit.com/adafruit-ds3231-precision-rtc-breakout?view=all

http://tronixstuff.com/2014/12/01/tutorial-using-ds1307-and-ds3231-real-time-clock-modules-with-arduino/

.

I upload the code second time with //setTime(18,00,00,7,1,17) and nothing.The problem is still here...But serial monitor is now display 0:00:01 0:00:02 0:00:03 0:00:04

your code (and results) suggest that you have no RTC connected to your arduino.

how is your RTC wired to your Arduino? which RTC are you using?

I use this RTC(ds3231) https://www.google.sk/search?q=ds3231&source=lnms&tbm=isch&sa=X&ved=0ahUKEwjZm8DS9OjUAhUBVxQKHYCtAb4Q_AUICigB&biw=1366&bih=638#imgrc=GkgnpISwipcyyM:

And connection like this https://www.google.sk/search?q=ds3231&source=lnms&tbm=isch&sa=X&ved=0ahUKEwjZm8DS9OjUAhUBVxQKHYCtAb4Q_AUICigB&biw=1366&bih=638#tbm=isch&q=rtc+relay&imgrc=PQUo8U2VM0ZqJM:

I try this on arduino Nano and Arduino-Flip-n-Click(https://shop.mikroe.com/flip-n-click-sam3x)

Did you connect RTC ground to Nano Vin as in that stupid diagram?

Showing images not of your work, does not help us help you.

.

No.I'm not that stupid.I connect GND to GND an VIN to VCC.RTC is work-i try other program and the time are normally displaying.

MatusM: No.I'm not that stupid.I connect GND to GND an VIN to VCC.RTC is work-i try other program and the time are normally displaying.

your code doesn't ever sync to the clock, it seems...

does your other program use any library to communicate to the RTC?

You connect Vin to VCC? That doesn’t sound right. Also Bulldog is right, there is no access to any RTC in your program.

I download RTClib and try to set time through this(and work(on this link Overview | Adafruit DS3231 Precision RTC Breakout | Adafruit Learning System).
This program use RTClib.h and Wire.h

MatusM: I download RTClib and try to set time through this(and work(on this link https://learn.adafruit.com/adafruit-ds3231-precision-rtc-breakout?view=all). This program use RTClib.h and Wire.h

Yes, but the program you posted doesn't. Please clarify what code you actually are having problems with.

Ok I have problem with code in 1.message in this topic.

MatusM: Ok I have problem with code in 1.message in this topic.

As pointed out in the last two replies, there is no RTC code in it. It only uses the Time Library, which defaults to millis() timing.

And how can i give in this code the function which will be use RTC library?

MatusM: And how can i give in this code the function which will be use RTC library?

Include the RTC library and set it as a sync provider for the Time library. Research this before you come back with more questions so everyone will be on the same page.

Here is how I do it in my code:

  setSyncProvider(RTC.get);

Where to place this command? to void loop or void setup

setSyncProvider(RTC.get);

Put it in setup()

You can find examples by Googling: setSyncProvider

.