Practical use of DS1307 SQW output?

Does anyone actually use the DS1307's square wave output? (And if so, what for?)

e.g. suppose you have a RTC connected to the sketch and you want to keep time up to date without reading the RTC over and over, you can get a pulse every second to update the time (seconds/minutes/hours/etc). That has probably less overhead than reading the RTC over I2C.

So now to my supplementary question: how to make it work?

Using the registers as explained in the data sheet is a bit above me. As far as I can see there’s no simple way provided by the library.

edit… I’m thinking a byte write of some kind, to adress 68’s register 7, a value that sets bit 4 and also bits 0 and 1 to the values required for the SQW frequency. So a 16 would do the trick to set bit 4 while leaving bits 0 and 1 at 0, for a 1Hz wave.

But I have no idea of the syntax…

more edit… ok Google led me to this which I didn’t find earlier. I’ll try it in the morning. (Good old Dave at EEV)


I have a DS1307 library that gives full access to all the functionality of the IC. Link from my Code libraries (below).

Thanks Marco- I'll have a look.

Jim-- It is very easy to set the control register 07h (bits 4,1,0) in the DS1307 to enable and set the SQW with out any library except Wire.h. The data sheet

You use a simple function

void writeRTC(byte location, byte data)
// writes data to location

and call for the frequency you want

writeRTC(0X07, B00010000);// 1Hz
writeRTC(0X07, B00010001);//4.096 KHz
writeRTC(0X07, B00010010);//8.192 KHz
writeRTC(0X07, B00010011);//32.768 KHz

Thanks Guys, that was easy.

It's a bit disconcerting that it keeps the wave going when the main power is off: that's a sure way to kill your backup battery.

Take a look at the DS3231

In the control register 0Eh for the square wave, bit 6 is Battery Backed Square Wave Enable/Disable, and the square wave can be turned off when Vcc is taken over by Vbat.

The DS3231 is more accurate than the DS1307, has alarms and other additional functions, and the modules are about the same price as the DS1307. They will run with the DS1307 library or you can use a specific library for the DS3231. I used Jack Christensen's.

I've switched to the DS3231 from DS1307's in several applications.

@cattledog, could you please clarify a bit more as how to set interval for SQW? Is there an example you could share with us?

Thank you,

Write to the appropriate bits of the control register:

Bit 7: Output Control (OUT). This bit controls the output level of the SQW/OUT pin when the square-wave output is disabled. If SQWE = 0, the logic level on the SQW/OUT pin is 1 if OUT = 1 and is 0 if OUT = 0. On initial application of power to the device, this bit is typically set to a 0.

Bit 4: Square-Wave Enable (SQWE). This bit, when set to logic 1, enables the oscillator output. The frequency of the square-wave output depends upon the value of the RS0 and RS1 bits. With the square-wave output set to 1Hz, the clock registers update on the falling edge of the square wave. On initial application of power to the device, this bit is typically set to a 0.

Bits 1 and 0: Rate Select (RS[1:0]). These bits control the frequency of the square-wave output when the square-wave output has been enabled. The following table lists the square-wave frequencies that can be selected with the RS bits. On initial application of power to the device, these bits are typically set to a 1.

@cattledog, could you please clarify a bit more as how to set interval for SQW? Is there an example you could share with us?

You add the register writing function to your code. You call it with the values given.

If you can please explain what you don’t understand about what was given in reply #5, I’ll try to address your explicit questions.

Please review page 9 of the linked data sheet for the DS1307 which covers the control register (0x07) for the square wave in detail

If you have some code where you are trying to add the square wave and are not having success, please post that.

Thank you for responses. I'm not sure how to get pulses from SQW at a specific time interval, e.g. 5 minutes, which then can be used to time data collection from a bunch of sensors (using interrupts).

Thank you for your patience,

You'll have to monitor time passing, and after 5 minutes turn on the square wave output for some time period, then turn it back off again.

Thank you for the responses. So I'm assuming that this RTC can not be programmed to send out pulses via the SQW pin at a specific interval, i.e. it needs the Arduino to actuate it, right?


The SQW is either on or off, and runs at the set rate according to the data sheet for the device (1 Hz from memory). If you want variable timing, you need to either:

  1. Use the BlinkWithoutDelay timer counting technique to wait for 5 minutes (use the RTC time for greater accuracy), turn on the SQW while you are collecting data and then turn the SQW off. Reapeat as often as you want.

  2. Turn the SQW on and count the number of interrupts until you get to the 5 minutes (300 seconds), then start data collection. Once done, reset the counter and start counting all over again.

SQW can output 1 of 4 frequencies: 1Hz / 4.096kHz / 8.192kHz / 32.768kHz

Thanks CrossRoads.

Thank you Marco and CrossRoads, I have a follow up question: Is each one of these frequencies associated with a specific timing intervals? What is the criteria to select one over the other?

Thanks for your patience and help, B.

You can select the frequency according the need. Sometimes the resolution under second is needed. RTC can also acts as main clock for MCU, e.g. 32768Hz. If you want just counting seconds then 1Hz connected to the external interrupt fits. I use in my designs the DS3231 and default 32768Hz output which drives the Timer2 on ATmega1284P. To read RTC date and time at startup or at date/time set is enough. ATmega328 in the UNO has no Timer2 available. The pins are the same for main crystal so the solution in this case is to use 1Hz and external interrupt.

Christmas tree lights