DS1307 Square Wave Output Help

I've searched through a number of threads looking for an answer and cannot find one.

My project is building a digital clock and originally I was using a 14 Bit counter to divide down a 32.768 kHz crystal to 2 Hz, then feeding into a flip flop to get 1 Hz. The main problem with this is the space it takes up and the external components needed to keep time correctly, so I got a DS1307 and want to use it solely for the 1 Hz output, which will be fed into an interrupt

I have the circuit hooked up properly per the datasheet (identical and using 1k resistors) and have a single LED coming off of the SQW pin and going to ground through a resistor so I can see the pulses.

If using the DS1307RTC.h library and running the SetTime example sketch from that library I can see the LED blinking at 1 Hz and the serial print out confirms that the circuit is working and the IC has been set correctly.

If I then remove power from the chip, and apply power again to reset, then upload my program I cannot get the LED to blink at 1 Hz (or at all)

The datasheet makes this seem very straightforward, so maybe somebody can point out why I'm having trouble? Are there anything other bits that need to be written in order to get the SQW output (Besides the ones for the SQW)?

EDIT TO ADD: I am not using a 3V battery, but connecting both those pins to ground

Here is my code:

#include <Wire.h>

void setup() {

  Serial.begin(9600);
  Wire.begin();
  Wire.beginTransmission(0x68);
  Wire.write(0x07);
  Wire.write(0x10);  // Set Square Wave to 1 Hz
  Wire.endTransmission();  
}

void loop() {
  
  Serial.println(analogRead(A0)*.0049);
  delay(250);
  
}

Have you tried connecting the arduino 3.3V to Vbatt instead. If you have the voltage to make the chip happy why not just using and not waste any time trying to make it work without it. Who cares. If it works with it , your good to go.

If Vbat is not used, the data sheet says to ground it.

The SQW output needs to be set at the proper rate and enabled and it appears that your program does that properly. However, the SQW output is open drain, which means it cannot supply current to ground. It can only sink current from Vcc. So, I don't understand how this ever worked:

I have a single LED coming off of the SQW pin and going to ground through a resistor so I can see the pulses.

I was hoping to not use a backup battery in the finished product. I'm not using the chip to keep time, just for the accurate square wave.I was just wondering why the example code will work and cause the SQW pin to output the square wave, but my code cannot. Does a time need to be set in order for it to work?

Have you tried connecting the arduino 3.3V to Vbatt instead.

Where does the above say anything about using a battery ?

There is also the CH (clock halt) bit, bit 7 in register 0. If that got set to 1 somehow, the oscillator will not run and neither will SQW. Try writing 0 to register 0.
The data sheet says this:

On first application of power to the device the time and date registers are typically reset to 01/01/00 01 00:00:00 (MM/DD/YY DOW HH:MM:SS). The CH bit in the seconds register will be set to a 1.

It isn't clear how the device could know when the "first application of power" could be, so perhaps if you do not have a backup battery, the first time is every time.

jremington:
If Vbat is not used, the data sheet says to ground it.

The SQW output needs to be set at the proper rate and enabled and it appears that your program does that properly. However, the SQW output is open drain, which means it cannot supply current to ground. It can only sink current from Vcc. So, I don't understand how this ever worked:

I have a single LED coming off of the SQW pin and going to ground through a resistor so I can see the pulses.

I must have missed that in the datasheet. I will try changing that part of the circuit and see if that fixes my problem. Regardless, somehow the LED lights up when running the example program

jremington:
There is also the CH (clock halt) bit, bit 7 in register 0. If that got set to 1 somehow, the oscillator will not run and neither will SQW.
Try writing 0 to register 0.

I will give this a try too

EDIT: This solved my problem! Thank you!

good catch rheb1026

rheb1026,

I am trying to do the same thing. Would you share your final code?

Thank you.

:slight_smile:

Sure thing!

void rtcPulse() {
   
  Wire.begin();
  Wire.beginTransmission(0x68);
  Wire.write(0x00);
  Wire.write(0x00);
  Wire.endTransmission();
  Wire.beginTransmission(0x68);
  Wire.write(0x07);
  Wire.write(0x10);  // Set Square Wave to 1 Hz
  Wire.endTransmission();
  
}

And here's one for the DS3231

#include <Wire.h>

void setup() {
  // put your setup code here, to run once:
  Wire.begin();
  Wire.beginTransmission(0x68);
  Wire.write(0x0e);
  Wire.write(0);  // Set Square Wave to 1 Hz
  Wire.endTransmission();
}

void loop() {
}