RTC reseting when plugging in

I'm using a DS1302 with an Arduino Mega 2560. Connected to both I have a 5V power supply from 220V AC (image of diagram attached).

I tried running the code from the example in the library, but without the delay:

#include <DS1302.h>

// Init the DS1302
DS1302 rtc(53, 51, 49);

void setup()
{
  // Set the clock to run-mode, and disable the write protection
  rtc.halt(false);
  rtc.writeProtect(false);
  
  // Setup Serial connection
  Serial.begin(9600);

  // The following lines can be commented out to use the values already stored in the DS1302
  //rtc.setDOW(FRIDAY);        // Set Day-of-Week to FRIDAY
  //rtc.setTime(12, 0, 0);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(6, 8, 2010);   // Set the date to August 6th, 2010
}

void loop()
{
  // Send Day-of-Week
  Serial.print(rtc.getDOWStr());
  Serial.print(" ");
  
  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");

  // Send time
  Serial.println(rtc.getTimeStr());

  // delay(1000);
  // Wait one second before repeating :)
}

I can confirm that pins are all set correctly because the clock works perfectly.

I'm running the sketch plugged to my pc, the problem is, sometimes when I plug the power supply to 220V the rtc resets to 01/01/2000 00:00:00, and keeps working perfectly from there. Im plugging it in after running the sketch because I discovered this issue with a project of mine, where I would get this reset whenever I connect or disconnect a relay close (not connected) to the arduino, so I'm assuming it might be a problem with peaks of voltage caused by any interference or such, don't really know.

Thanks for any help!

Not quite enough info but I'll give it a try.

It sounds like you have the mega connected to your PC via some USB-Serial Adapter. In this case the Mega and RTC is powered from your PC.

Then you plug in a mains -> 5V supply and then you connect it to ..... what?

You have (+5) and (-5). This suggests that you are powering the Mega with 10 volts. You show no ground, so I believe your "-5" is actually a ground. But you should label it as "common", "ground", "GND" or something like that...

Is there a backup battery available for your DS1302?

wvmarle:
Is there a backup battery available for your DS1302?

...and is it inserted the correct way?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.