I'm using a DS3231 RTC for my anniversary project (Anniversary Gift - Programming Questions - Arduino Forum) and for some reason my RTC will not retain what time it is. The RTC reads everything just fine when it is plugged in to the computer, and it runs each test program smoothly but it won't retain time when either the power is turned off or when I unplug it from the computer.
#include <Wire.h>
#include "RTClib.h"
#include <Servo.h>
#include <Adafruit_GFX.h>
#include <gfxfont.h>
#include "Adafruit_LEDBackpack.h"
#define DS3231_I2CIADDRESS 0x68
Servo giftgiver;
RTC_DS3231 rtc;
Adafruit_7segment matrix = Adafruit_7segment();
DateTime firstDate(2017, 1, 21, 0, 0, 0);
void setup() {
#ifndef __AVR_ATtiny85__
Serial.begin(9600);
Serial.println("Display test");
#endif
matrix.begin(0x70);
#ifndef ESP8266
while (!Serial);
#endif
delay(3000);
matrix.setBrightness(8);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
giftgiver.attach(9); //Attach it in the loop if it's the 21 so you can save power
giftgiver.write(0);
if (rtc.begin()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
Serial.println(F("RTC TIME SET"));
digitalWrite(LED_BUILTIN, HIGH);
}
if (rtc.lostPower()) {
Serial.println("RTC LOST POWER");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
}
void loop() {
DateTime current = rtc.now();
Serial.print("Days: ");
int daysSince = floor((current.unixtime() / 86400L) - (firstDate.unixtime() / 86400L));
Serial.print(daysSince);
matrix.print(daysSince, DEC);
matrix.writeDisplay();
if(current.day() == 9 && current.minute() == 27) {
giftgiver.write(150);
delay(1000);
}
delay(3600000);
giftgiver.detach();
}
but it won't retain time when either the power is turned off or when I unplug it from the computer.
These lines in your code are resetting the RTC time to the compile time every time you cycle the power. Set the time in the rtc , comment out the rtc.adjust() line, download the modified sketch.
if (rtc.begin()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); //comment out this line once rtc is set.
Serial.println(F("RTC TIME SET"));
digitalWrite(LED_BUILTIN, HIGH);
}
cattledog:
These lines in your code are resetting the RTC time to the compile time every time you cycle the power. Set the time in the rtc , comment out the ret.adjust() line, download the modified sketch.
OOPS, I saw the lostPower but not that one. must stop using a cell phone for this site.
Andromeda_:
I'm using a DS3231 RTC for my anniversary project (Anniversary Gift - Programming Questions - Arduino Forum) and for some reason my RTC will not retain what time it is. The RTC reads everything just fine when it is plugged in to the computer, and it runs each test program smoothly but it won't retain time when either the power is turned off or when I unplug it from the computer.
Is there a backup battery inserted in the RTC module?