DS1307 Real Time Clock CH bit problem

Hello guys, I recently bought DALLAS 1307 9610A3 chip and 32.768Mhz crystal oscillator and a 3V battery with its holder. I am trying the circuit on the breadboard it. I set time and date to the RTC it just works fine while the arduino plugged to the computer via USB and i can read the time values sec by sec. The problem is starts when i plugged out the USB from the PC. RTC's data is not resetting but it keeps same as i left when plugged out USB. When i plug in the USB it continues to count. I spent my hours to find the solution but it turn out to be about CH(Clock Halt) bit. I could not set the CH value nor read it. My DS1307's datasheet is this one. I found a code that reads the CH value but when i check it from the Serial window it prints the seconds only. It should not has to be the CH value which should be set to 0 in order the oscillator to vibrate? I would be very thankful if you guys could help me about this problem. Thank you in advance. :)

Yes, you are correct in the understanding of the CH bit. Bit 7 of Register 0 is the clock halt
(CH) bit. When this bit is set to 1, the oscillator is disabled. When cleared to 0, the oscillator is enabled.

I set time and date to the RTC it just works fine while the arduino plugged to the computer via USB and i can read the time values sec by sec

If the clock is running then the CH bit must be in the 0 state.

The problem is starts when i plugged out the USB from the PC. RTC’s data is not resetting but it keeps same as i left when plugged out USB. When i plug in the USB it continues to count. I spent my hours to find the solution but it turn out to be about CH(Clock Halt) bit

I would suggest that the problem is with running the rtc on battery power, rather than something with the CH bit. There is nothing about disconnecting the usb power which will set the CH bit to 1 or reconnecting it that will set it to 0. Can you confirm correct battery power to the chip and oscillator with the USB disconnected?

Here is some code to set and clear the CH bit, and to read the values in the timekeeper registers.

// DS1307 I2C Read and Write Timekeeper Registers 0x00-0X07

#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68
#define startRegister 0x00
#define endRegister 0x07

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  Serial.print("Register");
  Serial.print("\t");
  Serial.println("Bit Values");
  Serial.println();

  writeNVRAM(0x00, B00000000);//enable oscillator and set seconds to 0
  //writeNVRAM(0x00, B10000000);//disable oscillator set CH bit

  for (int a = startRegister; a <= endRegister; a++)
  {
    byte b = readNVRAM(a);
    Serial.print("0X");
    if (a < 16)
      Serial.print("0");
    Serial.print(a, HEX);
    Serial.print("\t");
    Serial.print("\t");


    for (int i = 7; i >= 0; i-- )
    {
      Serial.print((b >> i) & 0X01);//shift and select first bit
    }
    Serial.println();
  }
}

void writeNVRAM(byte location, byte data)
// writes data to DS1307 NVRAM location
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(location);
  Wire.write(data);
  Wire.endTransmission();
}

byte readNVRAM(byte location)//// reads data from DS1307 NVRAM location
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(location);
  Wire.endTransmission();
  Wire.requestFrom(DS1307_I2C_ADDRESS, 1);
  Wire.read();
}

void loop() {
}

cattledog:
Yes, you are correct in the understanding of the CH bit. Bit 7 of Register 0 is the clock halt
(CH) bit. When this bit is set to 1, the oscillator is disabled. When cleared to 0, the oscillator is enabled. If the clock is running then the CH bit must be in the 0 state.
I would suggest that the problem is with running the rtc on battery power, rather than something with the CH bit. There is nothing about disconnecting the usb power which will set the CH bit to 1 or reconnecting it that will set it to 0. Can you confirm correct battery power to the chip and oscillator with the USB disconnected?

Here is some code to set and clear the CH bit, and to read the values in the timekeeper registers.

// DS1307 I2C Read and Write Timekeeper Registers 0x00-0X07

#include “Wire.h”
#define DS1307_I2C_ADDRESS 0x68
#define startRegister 0x00
#define endRegister 0x07

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  Serial.print(“Register”);
  Serial.print("\t");
  Serial.println(“Bit Values”);
  Serial.println();

writeNVRAM(0x00, B00000000);//enable oscillator and set seconds to 0
  //writeNVRAM(0x00, B10000000);//disable oscillator set CH bit

for (int a = startRegister; a <= endRegister; a++)
  {
    byte b = readNVRAM(a);
    Serial.print(“0X”);
    if (a < 16)
      Serial.print(“0”);
    Serial.print(a, HEX);
    Serial.print("\t");
    Serial.print("\t");

for (int i = 7; i >= 0; i-- )
    {
      Serial.print((b >> i) & 0X01);//shift and select first bit
    }
    Serial.println();
  }
}

void writeNVRAM(byte location, byte data)
// writes data to DS1307 NVRAM location
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(location);
  Wire.write(data);
  Wire.endTransmission();
}

byte readNVRAM(byte location)//// reads data from DS1307 NVRAM location
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(location);
  Wire.endTransmission();
  Wire.requestFrom(DS1307_I2C_ADDRESS, 1);
  Wire.read();
}

void loop() {
}

Firstly, thank you so much for your reply. I used your code and it gave me this:

Register Bit Values

0X00 01000101
0X01 01000001
0X02 00100000
0X03 00000000
0X04 00100101
0X05 00000010
0X06 00010101
0X07 00000000

I think which is a good think about CH bit because i tracked it by copying the print part to the loop and it was never 1. When I set it to 1 and check the time with other code it gives me error that the time was not set on the chip. So, it is not about the CH pin anymore i think. I checked the battery it gives me 3.28V so its OK. when i plug out the oscillator the seconds stop and not working. As you said something sets the CH pin to 1 when i plugged out the USB from the PC. I tried many things but i don’t know what else i can do :frowning:

I have found that somebody had a problem like me he used 4.7uF capacitor parallel with the DS1307 VCC pin. And he solved his problem. I do not have any capacitor on my board. Could it be the problem that not having a capacitor? Here is the link.

I do not believe that the RTC chip is getting battery power when the usb power is removed. There is no setting of the CH bit on usb power removal.

From your linked posting, it is possible that power spikes on disconnect may be an issue, so the cap across the Vcc and ground is a good idea. I'm not sure of the mechanism of how the disconnect can corrupt the register structure of the chip.

Edit -- Take a look at http://www.maximintegrated.com/en/app-notes/index.mvp/id/504 which has information on how power glitches can affect the RTC. It is quite possible that power spikes , particularly negative voltages during disconnect can corrupt the RTC.

Please provide the wiring diagram for your circuit.

I checked the battery it gives me 3.28V so its OK.

Have you confirmed that you actually see this voltage at pin3 of the rtc chip? Do you have a scope and can check if the oscillator is running under battery power?

If you have another arduino at hand, you can try running the VBAT from the 3.3v supply. Connect all grounds together if you do this?

I am gonna check these when i get home. Thank you again so much.

Here is my fritzing; and this is real photo;

By the way, I confirmed the voltage between 3-4 pins without touching battery pins(I touched the probes to the legs of the DS1307)

I'm afraid I dont have a Oscilloscope :( (I wish i have :/ )

But when the chip connected to PC with USB, the seconds are working just fine so oscillator is working with the VCC power.

I have tried with second Arduino to supply 3.3V to the MEGA and it didnt work as well. (I grounded two Arduinos together)

I am really pissed off with this little chip. What is wrong with this?

I confirmed the voltage between 3-4 pins without touching battery pins(I touched the probes to the legs of the DS1307)

That's good. It confirms that battery power is indeed getting to the chip. The Fritzing does not match the bread board and it does look like the battery ground and chip ground are connected.

Every DS1307 module I have looked at has a cap across the Vcc/Ground. I would suggest you do that. You have already mentioned others with power spikes on disconnect issues.

On possible trouble shooting idea is to turn the computer power off, rather than pulling the usb connector. It may be smoother and if the problem of not updating time without power goes away you have definitely found the problem.

Okay i added a cap and not plugged out the USB. I shut down the computer and waited 5min then started the computer it was 4 minutes back again. the 1 min is closing+opening stage of the computer :(

I'm running out of ideas. I think that the cap and computer shut down eliminate the power turn off spike hypothesis.

It's pretty clear that either the battery power is not correctly applied to the chip, or the chip and/or oscillator are defective and don't run under battery power for some reason. However, it is unusual that if you have a real Dallas chip it would be defective.

One possibility is that there is a defective jumper from the battery ground going over to the chip pin. You have confirmed the battery voltage at pins 3-4, but given your wiring I think you would see that even if the black wire from battery ground going over to the pin were defective. Possibly, the bread board may be defective. I have seen bad wires and bad holes in these inexpensive things. I'd try some different jumper wires and different breadboard connection positions.

I think you misunderstood an earlier suggestion bout the 3.3v from another arduino.

I have tried with second Arduino to supply 3.3V to the MEGA and it didnt work as well. (I grounded two Arduinos together)

I wanted you to use the second arduino 3.3v source as a replacement for VBAT. not as a power source for the MEGA. I was trying to get at possibilities that the battery voltage was not getting to the chip. It's still worth a try to replace the battery with the other arduino.

You should also focus on the oscillator. Do you have another one to try? I've seen postings about issues with contamination(high impedance shorts) affecting the oscillator, so you may want to move the chip to different position on the board you have or try a new breadboard entirely.

I'd also like to take a look at the code you are using to read the RTC.

I was just working with someone in Germany who had DS1307 module from SainSmart. Module would hang up after running briefly. He had several, and they all did the same. My sparkfun module ran just fine in a test bed. He changed to DS1307 from another source and his problems went away. Where did your DS1307 come from?

I think you misunderstood an earlier suggestion bout the 3.3v from another arduino.

I wanted you to use the second arduino 3.3v source as a replacement for VBAT. not as a power source for the MEGA. I was trying to get at possibilities that the battery voltage was not getting to the chip. It’s still worth a try to replace the battery with the other arduino.

I connected second Arduino’s 3.3 pin to 1307’s VBAT pin and connected grounds together. I’m sorry, i just could not explained it well. I just did what you have told me. Sorry for my bad English. I have 10 oscillators and 5 DS1307 and tried couple of these but still same. I am now gonna change breadboard and wires as well.

CrossRoads:
I was just working with someone in Germany who had DS1307 module from SainSmart. Module would hang up after running briefly. He had several, and they all did the same.
My sparkfun module ran just fine in a test bed. He changed to DS1307 from another source and his problems went away.
Where did your DS1307 come from?

I get it from a local seller in Turkey. I don’t know where did it come from. He said it is real DALLAS and it is first class product. I don’t know if he said that to sell the chip or not. If changing wires and breadboard will not change any result, I am going to buy a new DS1307 from a different seller tomorrow. By the way, here is a photo of my chip;

And i don’t know how to thank you guys for your all afford again. Thank you so much :slight_smile:

Likely fake. Real ones say DS1307 -->> Device type and tolerance 0706A4 -->> yywwrv year, week, die rev +102B N -->> +###xx lot code

yww or yyww Date Code. Identifies the year and work week the device was assembled. y, the last digit of the year. yy, the last two digits of the year. ww, the work week (values 01 through 52). rv Die Revision A letter followed by a number (eg. B1)

xx Lot Code Three numbers followed by 2 letters

If marked with +, indicates lead free If marked with N, indicates industrial temp range

I have not seen any indication of the chips actually saying Dallas on them.

"Dallas Semiconductor, acquired by Maxim Integrated Products in 2001, designed and manufactured analog, digital, and mixed-signal semiconductors (integrated circuits, or ICs). Its specialties included communications products (including T/E and Ethernet products), microcontrollers, battery management, thermal sensing and thermal management, non-volatile RAM, microprocessor supervisors, delay lines, silicon oscillators, digital potentiometers, real-time clocks, temperature-compensated crystal oscillators (TCXOs), iButton, and 1-Wire products. The Dallas, Texas-based company was founded in 1984 and purchased by Maxim Integrated Products in 2001. Both the Maxim and Dallas Semiconductor brands were actively used until 2007. Since then the Maxim name has been used for all new products, though the Dallas Semiconductor brand has been retained for some older products."

Thank you so much, so it explains everything. I could not buy a new one because the shop was closed. So, I am planning to buy it on Tuesday. Thank you so much for your help. I will post a feedback here as soon as i complete the circuit and make it work :)

Bciceksoy: Thank you so much, so it explains everything. I could not buy a new one because the shop was closed. So, I am planning to buy it on Tuesday.

Please also check whether the crystal meets more than just the frequency specification listed in the datasheet:

Crystal Specifications: Nominal Frequency 32.768 kHz Load Capacitance 12.5 pF

Some Arduino workers just care about the frequency, but not about the load capacitance of the crystal.

If the load capacitance is not matching, the crystal might not work as intended. Such crystals are available with many different load capacitance specifications like 6pF, 12.5 pF or 22 pF. But only 12.5 pF is really correct for the DS1307.

Even if a DS1307 should be working with a crystal that provides the wrong load capacitance (which happens in most cases luckily), you will find one problem: The accuracy of the clock will not be within the datasheet specifications and the clock will always be fast or late more than the datasheet tells. This is also caused by wrong load capacitance of the crystal.

BTW: Crystal oscillators are sensitive to shock. So if you think that your crystal might have been exposed to shock like fallen down to the floor before or after soldering or usage in breadboard, perhaps try another one.

Okay, I will consider them as well. But the DS1307 is working with USB power so oscillator is working? Even if oscillator is working with USB power, could it be not working with battery power?

Bciceksoy: Okay, I will consider them as well. But the DS1307 is working with USB power so oscillator is working? Even if oscillator is working with USB power, could it be not working with battery power?

See application note.

Scroll down to the chapter "Slow Clocks".

Guys, something happened and I am not gonna sure are you guys believe or not :D I just went mad and tried all capacitors, resistors, chips, crystal oscillators randomly and I put another oscillator parallel to the oscillator which was connected to first and second pin of the DS1307 and guess what happened? Two oscillators are combined their power and valla! :D Chip is now counting while the USB is plugged off?!?! It was a just chance. I don't know why and how this happened. I guess it is about the oscillator's capacitance? Datasheets say 12.5pF so I was thinking if one oscillator's capacitance is like 6.25pF so I connected them serial than C1+C2 = 12.5pF? Is it the problem? Wrong oscillator? or wrong oscillator capacitance? We are about the solve it...

EDIT: But there is happening some difference between the RTC time and PC time like 4-5 sec/hr I guess it is about the 2 oscillators?

Thank you in advance.

You're getting closer, and I hope your frustration level is going down. You've suffered enough.

Do you know how accurate the RTC is when running on the usb power? A mismatch between the crystal and the internal capacitors will make the rtc run fast or slow.

Take a look at this crystal oscillator trouble shooting guide http://www.freescale.com/files/microcontrollers/doc/app_note/AN3208.pdf

Check out section 7 on insufficient loop gain with Pierce oscillators. There are clearly circuit design issues, and resistor values which will make the oscillator not run at lower voltages.

It's not clear to me if the root cause of your problem is a counterfeit ds1307 chip with something wrong in the oscillator circuit or the wrong crystal. I'm leaning towards the chip being the problem, but you should be able to find out when you get back to the electronics store.

Hello again guys, today I went to the electronics shop(where I bought the crystal oscillators) and said them that when i put two oscillators serial it works but time not stable. I was brought my oscillators and gave him these and he changed these oscillators with a better quality ones. But I bought a one DS1307 just in case.(This DS1307 is x3 price of the one I have) And I told the seller guy that I bought it 1/3 price of this and showed my chip. He said It is likely fake and this might causing the problem. He said he knew the guy that I bought my fake DS1307s and he was selling fake stuffs. So, I came home and tried new oscillators with fake DS1307, it kinda worked but every 5-6 sec second is froze for 1 sec. It was a problem again. Then, I remade the circuit with new oscillator and new DS1307(it is likely original) and it is now working perfectly. I tried with USB powered for 3 hours and even one seconds is not delayed. Also unplugged and waited about 1 hrs no time change. Exactly the same seconds, minutes, hours... So, my problem solved with changing DS1307. If anyone having problem and followed these each steps as I did, I recommend them to change their DS1307 if possible change your crystal oscillators with better quality ones. Thank you all of you guys that helped me, I am very thankful for your all help :) See you, have a wonderful day.