trigger relay using RTC DS1307

I have little experience in Arduino programming and trying to pilot an old electric clock with it.
I succeeded sending a signal to the relay each minute using internal Arduino clock and a delay loop but I would like to use an RTC to improve accuracy.
I tried the following sketch first, it sucessfully blinks the LED each second but I feel that the 1s delay is done by the arduino rather than the RTC (I may be wrong) . Could someone help me understanding how to use the clock to send a signal to the LED each minute reading the RTC (or any outPin) ?
Thank you in advance for your help !!

#include <Wire.h>
#include <DS1307.h>
/*
about set time:
format: year,month,day,week,hour,min,sec
example: 14,03,25,02,13,55,10 2014.03.25 tuesday 13:55:10
*/
String comdata = "";
int mark=0;
//store the current time data
int rtc[7];
//store the set time data
byte rr[7];
//light pin
int ledPin = 13;
//initial light
void setup()
{
DDRC |= _BV(2) | _BV(3); // POWER:Vcc Gnd
PORTC |= _BV(3); // VCC PINC3
pinMode(ledPin, OUTPUT);
//initial baudrate
Serial.begin(9600);
//get current time
RTC.get(rtc, true);
//if time is wrong reset to default time
if (rtc[6] < 12) {
//stop rtc time
RTC.stop();
RTC.set(DS1307_SEC, 1);
RTC.set(DS1307_MIN, 27);
RTC.set(DS1307_HR, 01);
RTC.set(DS1307_DOW, 7);
RTC.set(DS1307_DATE, 12);
RTC.set(DS1307_MTH, 2);
RTC.set(DS1307_YR, 12);
//start rtc time
RTC.start();
}
//RTC.SetOutput(LOW);
//RTC.SetOutput(HIGH);
//RTC.SetOutput(DS1307_SQW1HZ);
//RTC.SetOutput(DS1307_SQW4KHZ);
//RTC.SetOutput(DS1307_SQW8KHZ);
RTC.SetOutput(DS1307_SQW32KHZ);
}

void loop()
{
int i;
//get current time
RTC.get(rtc, true);
//print current time format : year month day week hour min sec
for (i = 0; i < 7; i++)
{
Serial.print(rtc*);*

  • Serial.print(" ");*
  • }*
  • //blink the light*
  • Serial.println();*
  • digitalWrite(ledPin, HIGH);*
  • delay(500);*
  • digitalWrite(ledPin, LOW);*
  • delay(500);*
  • //*
  • int j = 0;*
  • //read all the data*
  • while (Serial.available() > 0)*
  • {*
  • comdata += char(Serial.read());*
  • delay(2);*
  • mark = 1;*
  • }*
  • //if data is all collected,then parse it*
  • if (mark == 1)*
  • {*
  • Serial.println(comdata);*
  • Serial.println(comdata.length());*
  • //parse data*
  • for (int i = 0; i < comdata.length() ; i++)*
  • {*
  • //if the byte is ',' jump it,and parse next value*
    _ if (comdata == ',')_
    * {*
    * j++;*
    * }*
    * else*
    * {*
    _ rr[j] = rr[j] * 10 + (comdata - '0');
    * }
    }
    comdata = String("");
    RTC.stop();_
    RTC.set(DS1307_SEC, rr[6]);
    RTC.set(DS1307_MIN, rr[5]);
    RTC.set(DS1307_HR, rr[4]);
    RTC.set(DS1307_DOW, rr[3]);
    RTC.set(DS1307_DATE, rr[2]);
    RTC.set(DS1307_MTH, rr[1]);
    RTC.set(DS1307_YR, rr[0]);
    _ RTC.start();
    mark = 0;
    }*_

}

Welcome to the Forum. Please read the two posts at the top of this Forum for guidelines on posting here, especially when posting code. Also, use Ctrl-T in the IDE to reformat the code into a standard format before copying and posting here. It will help us help you.

sorry for that I tried but did not succeeded I will read it more carrefully

jxdurand:
Could someone help me understanding how to use the clock to send a signal to the LED each minute reading the RTC (or any outPin) ?

The slowest signal the RTC can "send" by itself is 1 Hz = 1 pulse per second.
You would have to enable the RTC 1 Hz square wave output, connect RTC "SQ" pin to an Arduino input pin, and count the pulses to 60 if you want to have accurate 1 minute timing.

The other possibility would be "polling" the time from the RTC, but the time you read from an RTC has only a one second resolution.

How accurate do you need your one minute timing, so that the Arduino 16 MHz frequency is not accurate enough for you, using the "millis()" function for timing and you want RTC timing instead?

The most straighforward and easiest way, is to set the RTC for 1Hz output, and count pulses. When the count reaches 60, output a control pulse to the relay and reset the count to zero.

Easy Peasy.

BTW, I regard the DS1307 as inaccurate. It's no more accurate than the cheap digital clocks and watches that are around, that always run slow or fast. Look towards the DS3232 series for much better time keeping.