Problem statement: the module is RTC DS1307, it does not keep time setting. More specifically, if Arduino Uno is always powered by a computer from time to time, the time setting is maintained. However, whenever it is powered by an external power source, e.g., a battery, the time setting is reset.
Code used: Before running my own code, the example code of ds1307 in the library is used with the line
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)))
uncommented to synchronize the module time with my computer time. The code is is run only once.
Then my code is run. It goes like this
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
rtc.begin();
}
The other codes not shown here are all related to a different module which is used for saving data and to settings for serial printing. In the main loop of my code, only one sentence is related to the module DS1307, which is
DateTime now = rtc.now();
Others: I have tested the onboard battery of the RTC module, the voltage is normal, slightly above 3v.
Any suggestion on why DS1307 does not hold onto the setting is appreciated.
Usually modules like that come with the battery installed, but also with al small plastic tab beneath the battery.
That way the battery will not discharge needlessly by the module keeping time while it is on its way from production, via some distributor/storage and multiple transports to you.
Did you perhaps miss such a tab or sticker on the battery ?
Thanks for the suggestion. I did remove the battery and checked its condition several times. I did not find any shield on any side of the battery.
On the other hand, I set the time from one computer and then powered the module on other computers at various times. As long as Arduino is always powered by a computer the time setting is kept. I suppose the battery is working somehow?
The time setting reversed to the default only occurs when Arduino is connected to a battery.
I bought the module from our local Jaycar shop in Australia. The external power is a power bank for mobile phones. I used the same power bank for Arduino with different modules in various projects previously but never had any problem.