Problems with RTC

So I'm working on a project that uses a servo at a specific time.

I have 2 problems:

1: My RTC doesn't keep track of time when it's turned off, when turning on again it will use the time that it got the last time I synced it with my PC.

2: The RTC doesn't want to work when the serial monitor isn't open

I am using:

  • Arduino Uno Rev 3
  • RTC PCF8523 with coin battery
  • high torque servo

my code used:

#include <RTClib.h>
#include <Wire.h>
#include <Servo.h>

RTC_PCF8523 rtc;

Servo servo;
int pos = 0;

char buf1[20];


void setup() {
  servo.attach(9);

  servo.write(0);

  Serial.begin(9600);
  Wire.begin();

  rtc.begin();
  rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));
}

void loop() {
  DateTime now = rtc.now();

  sprintf(buf1, "%02d:%02d:%02d %02d/%02d/%02d",  now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());  
  
  /*
   * now.hour() == 19 &&  && now.second() == 00 
   */
  if (now.minute() == 00 && now.second() == 00 || now.minute() == 10 && now.second() == 00 || now.minute() == 20 && now.second() == 00 || now.minute() == 30 && now.second() == 00 || now.minute() == 40 && now.second() == 00 || now.minute() == 50 && now.second() == 00){
    servo.write(90);
    delay (5000);
    servo.write(0);
    delay (300);
  }
  else {

  }
  
  Serial.print(F("Date/Time: "));
  Serial.println(buf1);

  delay(1000);

}

the code is used as a test to use the servo every 10 minutes, ignore the hour part since I won't use that atm.

as attachement, I will show a scetch of my current setup.

Thanks in advance!

bvernimmen:
1: My RTC doesn't keep track of time when it's turned off, when turning on again it will use the time that it got the last time I synced it with my PC.

Just like you told it to do, if that's a problem, why are you doing it?

  rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));

Whandall:
Just like you told it to do, if that's a problem, why are you doing it?

  rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));

it uses the time of the last time I synced it, I used this code once just to get my time on the rtc so I could work with it. Turning on the arduino with rtc, I don't run the code again so the time won't get synced again.

So you are posting code that is not running? Why?

Have you got a sketch that does nothing but read the rtc (which would have been set in a different sketch) and displays the rtc's (idea of) time in the monitor?

Whandall:
So you are posting code that is not running? Why?

I think I misundedrstood, are you saying that this after I used this line of code once, I should remove it since it is preventing the time to change in real time?

It will set the time to the last compilation time each time it resets.

Whandall:
It will set the time to the last compilation time each time it resets.

that makes a lot more sense, thanks.
The only problem i have right now is that the time is 8 seconds off, when I tried this yesterday it was always different. Is there any possible solution to fix this?

There's probably a way of hard-coding your time of choice into rtc.adjust() instead of the compile time. Then have a separate set time sketch with a value a bit ahead of the actual time, and hit reset when that actual time arrives on your watch.

All the registers of the chip are read-write, some registers have additional bits besides the time/date values.

To adjust a value

  • read the current value
  • save the special bits (if any)
  • manipulate the number
  • recombine the special bits (if any)
  • write the register
    Have a look at the datasheet. The chip seems to be quite similar to the DS3231.

Other then hard-coding, is there a way to sync the time using a WiFi shield?

NTP can be used to get the current time.
You will still have to transfer the values to the RTC.

I'll see what I can do using NTP.

Thanks for all the answers! :slight_smile: