I have noticed that, if you pull the Vcc connector out of the arduino's 5V pin, then the module keeps on 'ticking' such that when you re-connect its Vcc pin it returns the correct time.
But if instead you disconnect the arduinos usb cable then it seems to obliterate the time in DS1307 such that it subsequently displays rubbish when you re-connect the arduino.
But if instead you disconnect the arduinos usb cable then it seems to obliterate the time in DS1307 such that it subsequently displays rubbish when you re-connect the arduino.
Does anyone know why this occurs?
Note sure what you mean with displays rubbish but I will assume you mean loose the time.
Did you have the battery connected to the DS1307 to keep the time?
If you don't have the battery when the DS1307 loses power it stops 'ticking' even if you power it again until you set the time again on it
But if instead you disconnect the arduinos usb cable then it seems to obliterate the time in DS1307 such that it subsequently displays rubbish when you re-connect the arduino.
Does anyone know why this occurs?
Note sure what you mean with displays rubbish but I will assume you mean loose the time.
Did you have the battery connected to the DS1307 to keep the time?
If you don't have the battery when the DS1307 loses power it stops 'ticking' even if you power it again until you set the time again on it
The battery is in place in the tiny RTC module when I disconnect the arduino USB cabkle from the PC.
I have done the tests a number of times and it seems to be consistent.
Disconnecting the arduino USB cable messes up the time in the RTC every time. When you reconnect the arduino to the USB cable, the DS1307 has lost time that was previously set.
But disconnecting the Tiny RTC Vcc connector, with the Arduno still powered via the USB, does not mess up the time in the DS1307. When you re-connect the Vcc, the correct time is displayed.
HugoPT:
Do you have the pull-up resistors in the I2C line?
Post the code to see if there is some software problem
No external pull up resistors. But I would have thought that they would have been provided on the module itself.
But perhaps not given Fig35 in here: http://www.nxp.com/documents/other/UM10204_v5.pdf
I just googled it and found that 4.7k is generally used on both SDA and SLK lines.
What are the pull up resistor about exactly?
#include <Wire.h>
#include <RealTimeClockDS1307.h>
// Board I2C TWI pins
// ---------------------------------------------------
// Uno, Ethernet A4 (SDA), A5 (SCL)
// Mega2560 20 (SDA), 21 (SCL)
// Leonardo 2 (SDA), 3 (SCL)
// Due 20 (SDA), 21 (SCL), SDA1, SCL1
#define Display_Clock_Every_N_Seconds 1
#define Display_ShortHelp_Every_N_Seconds 25
//#define TEST_Squarewave
//#define TEST_StopStart
//#define TEST_1224Switch
int count=0;
char formatted[] = "00-00-00 00:00:00x";
void setup()
{
// Wire.begin();
Serial.begin(9600);
//RTC.switchTo24h();
/*
RTC.setHours(17);
RTC.setMinutes(40);
RTC.setSeconds(0);
RTC.setYear(14);
RTC.setMonth(1);
RTC.setDate(5);
RTC.setDayOfWeek(0);
RTC.writeData(0x08, 1);
RTC.setClock();
*/
}
void loop()
{
delay(1000);
RTC.readClock();
doCheckDayLightSavings(RTC.getDayOfWeek(), RTC.getMonth(), RTC.getHours(), RTC.getMinutes());
switch (RTC.getDayOfWeek())
{
case 0: Serial.print("Sunday");break;
case 1: Serial.print("Monday");break;
case 2: Serial.print("Tuesday");break;
case 3: Serial.print("Wednesday");break;
case 4: Serial.print("Thursday");break;
case 5: Serial.print("Friday");break;
case 6: Serial.print("Saturday");break;
};
Serial.print(" ");
Serial.print(RTC.getDay());
Serial.print("/");
Serial.print(RTC.getMonth());
Serial.print("/");
Serial.print(2000 + RTC.getYear());
Serial.print(" ");
Serial.print(RTC.getHours());
Serial.print(":");
Serial.print(RTC.getMinutes());
Serial.print(":");
Serial.println(RTC.getSeconds());
int nValue = analogRead(A0);
float nVoltage = nValue * (3.6 / 1023.0) * 1.65;
Serial.print("Backup battery: ");
Serial.print(nValue);
Serial.print(", ");
Serial.println(nVoltage);
Serial.println(" ");
}
void doCheckDayLightSavings(const int nDayOfWeek, const int nMonth, const int nHour, const int nMinute)
{
int nDayLightSavingFlag = 0;
const int nDS1307StartRAMAddr = 0x08;
Serial.println(" ");
Serial.println("*****************************************************");
Serial.println("Checking for daylight savings...");
nDayLightSavingFlag = RTC.readData(nDS1307StartRAMAddr);
if ((nDayLightSavingFlag == 0) && (nDayOfWeek == 0) && (nMonth == 10) && (nHour == 2) && (nMinute == 0))
{
RTC.writeData(nDS1307StartRAMAddr, 1);
RTC.setHours(3);
RTC.setClock();
Serial.println("First Sunday of October - clock forward by 1 hour...");
}
else if ((nDayLightSavingFlag == 1) && (nDayOfWeek == 0) && (nMonth == 4) && (nHour == 3) && (nMinute == 0))
{
RTC.writeData(nDS1307StartRAMAddr, 0);
RTC.setHours(2);
RTC.setClock();
Serial.println("First Sunday of April - clock back by 1 hour...");
}
else
{
if (nDayLightSavingFlag == 1)
Serial.print("In daylight savings but ");
else
Serial.print("Not in daylight savings but ");
Serial.println("clock not changed...");
}
Serial.println("*****************************************************");
Serial.println(" ");
}
I had the same issue with the DS1307 module. My problem was that I was sending the time to an LCD display. The sketch used the Serial class to read the PC's current date and time and then my code updated the 1307's registers accordingly. I then removed the calls to the Serial class (I use a #define DEBUG and then an #ifdef DEBUG to toggle the scaffolding debug code)...well, almost. I forgot to remove the Serial.begin() in the setup() function. Because the recompiled code did not try to update the 1307 in the "non-debug" version, it pushed the register data to the LCD display. My problem was that the register data was exactly the time I pulled the plug on the Arduino from the USB port and let the clock run on its battery.
I'm still not sure what is happening, but it appears that any call to the Serial class causes an internal reset of the Arduino, which caused the 1307 to push its registers. When the Arduino is powered up again, the 1307 pops those values back, not the current date and time.
Try removing all calls to the Serial class (if you can) and see what happens. If someone knows exactly what my problem was, I'd appreciate knowing what it was.
econjack:
I had the same issue with the DS1307 module. My problem was that I was sending the time to an LCD display. The sketch used the Serial class to read the PC's current date and time and then my code updated the 1307's registers accordingly. I then removed the calls to the Serial class (I use a #define DEBUG and then an #ifdef DEBUG to toggle the scaffolding debug code)...well, almost. I forgot to remove the Serial.begin() in the setup() function. Because the recompiled code did not try to update the 1307 in the "non-debug" version, it pushed the register data to the LCD display. My problem was that the register data was exactly the time I pulled the plug on the Arduino from the USB port and let the clock run on its battery.
I'm still not sure what is happening, but it appears that any call to the Serial class causes an internal reset of the Arduino, which caused the 1307 to push its registers. When the Arduino is powered up again, the 1307 pops those values back, not the current date and time.
Try removing all calls to the Serial class (if you can) and see what happens. If someone knows exactly what my problem was, I'd appreciate knowing what it was.
Let me get this straight.
It is your belief that the DS1307 is still maintaining the correct time but that it has been put in a state such that it not loading the register with the correct time and it is just returning the random contents of its registers?
Your theory should be easy enough to test with my code.
All I would have to do is comment out all the calls to "Serial." but leave in place all the calls to "RTC."
Re-load the sketch.
Then pull the USB cord and replace it.
Restore all the calls to "Serial.".
Re-load the sketch.
And then see what time and date is output in the serial monitor.
If you are correct it should display the correct date and time.
...and it is just returning the random contents of its registers?
That's not what I said. I said that, upon reapplying power to the board, the time displayed was the exact moment that power was removed from the board. So, when I removed power from the Arduino via the USB cable at 10AM, and came back and reapplied power via the USB cable at, say, 11:30AM, the display showed 10AM.
I guess one thing to try is to power the Tiny RTC from a power supply independent of the arduino (with the GNDs connected), and then try disconnecting the Vcc and GND lines from the RTC.
That will at least eliminate the arduino hardware as the source of the problem.
I have set the arduino and the RTC module up with independent power supplies, which is how I would use it anyway.
There are 4.7k pullup resistor on the SDA SCK lines.
The GND of the arduino and the RTC are connected.
If I disconnect Vcc from the RTC, then it keeps ticking and reports the correct time when you re-connect Vcc.
If I disconnect Vcc from the RTC, and then disconnect GND from the RTC, then it also keeps ticking and reports the correct time when Vcc and GND are reconnected in reverse order.
However if I leave Vcc alone and disconnect GND only, then the RTC loses its data and it reports rubbish data for time and date.
There is something in the DS1307 datasheet about reverse voltages of -0.3V or more causing loss of data.
I have the RTC powered from my breadboard, which is in turn powered by a 12V gel cell going through a 5V linear regulator that supplies all the rails of my bread board.
If I pull the alligator clip off the positive or negative terminal of the gel cell then the RTC keeps ticking and reports the correct time when they are reconnected to the gel cell.
If I disconnect the arduino usb cable then the Tiny RTC module also keeps ticking and reports back the correct time when I re-connect the arduino usb cable.
So it must be the case that, some how, the arduino is sending out a small but sufficient negative voltage pulse into the Tiny RTC and DS1307 when you pull out the usb cable or if you disconnect the arduino GND from the RTC module. Which, according to the DS1307 datasheet, results in the DS1307 being 'wiped'.
Glad you fixed it! I also run this code, at least once on any new (1307 compatible) RTC:
// clear /EOSC bit to ensure that the clock runs on battery power
Wire.beginTransmission(0x68); // address DS3231
Wire.write(0x0E); // select register
Wire.write(0b00011100); // write register bitmap, bit 7 is /EOSC
Wire.endTransmission();
Don't know why, I just do, and have never had a problem. I use tiger repellant too, and ... no tigers!
Hi I think I must comment on that topic. I bought RTC tiny from ebay. I use it to controll lights inside therarium via relay.
Every single time I disconected the power to arduino RTC resetted to 0:00:00! Then it hit me, while reading this post; negative voltage on pins! Relays use to "kick back"(induce a negative voltage on imput pins) when they are disconnected, because of the inductive nature of device... so that could explain negative voltage on ground pin or Vcc pin!!
So I disconnected the relay and the RTC worked. The solution would be I guess to solder a diode the other way oround to relay, so the negative voltage would discharge trough diode. Hope that helps anyone.
YES!!! I soldered a diode acros input pins of relay and no problems with RTC anymore. A little bit more testing is necesarry for long term reliability but right now the thing WORKS !!
In my case it was silly - connector of the coin battery has a soldering defect, so I had to re-solder them.
My DS1307 just did not get 3V to keep their memory.