DS1307 RTC

Article thumbnail


Using the DS1307 real-time clock (RTC) with Arduino

Date/time and RAM examples are listed in the DS130X Common Reference. This page covers DS1307-specific features.

DS1307 Features

The DS1307:

Pin Number Pin Name Function
1 X1 Connection to a 32.768 kHz quartz crystal
2 X2
3 VBAT Connection to a 3V battery
4 GND Ground
5 SDA I2C Data connection
6 SCL I2C Clock connection
7 SQW/OUT Square wave output (open drain)
8 VCC 5V power supply

Typical Operating Circuit

DS1307 basic schematic

Note that the backup supply is a 3V coin cell. This supply will allow the DS1307 to keep track of time when it is turned off.

The I2C communication lines require 10k pullup resistors. This is so they are not floating and can always have a known state.

The SQW output also has a 10k pullup. This is because the pin is open drain and can't directly drive other electronics. Without proper external circuitry, it will also float like the I2C pins.

The DS1307 keeps track of time by reading ticks from an oscillator. This is why a 32.768 kHz oscillator (Y1) is attached to pins 1 and 2.


DS1302 circuit schematic


This project uses my DS130X library. Download the library in Zip format

Using the SQW Output

In this example, we will use the SQW output on the DS1307. For example, the schematic to drive an LED is below:

DS1302 circuit schematic with LED

Note the use of the 10k resistor - this is the pullup resistor required for the SQW output.

The SQW pin is used as a current sink - the LED's anode is connected to 5V and the cathode is connected to R1, then the SQW pin. This is because the output is open-drain, so it can only be used to sink current.

The example code is at File > Examples > DS130X > DS1307 > Other > SquareWave.

   Sketch to demonstrate the square wave output of the DS1307.
   Created August 29, 2020

   The DS1307 has a Square Wave output pin (#7). This pin can provide six frequencies:
   Continuous Off : SQW_LOW
   1 Hz           : SQW_1HZ
   4.096 kHz      : SQW_4KHZ
   8.192 kHz      : SQW_8KHZ
   32.768 kHz     : SQW_32KHZ
   Continuous On  : SQW_HIGH

   To use this output, first call setHalt(false) to enable the oscillator.
   Then, use writeRaw [not write()] to write to the SQW_OUT register.
   For a desired frequency, use the constant listed next to the frequency

#include <DS130X.h>
#include <Wire.h>

DS1307 rtc;

void setup() {
  rtc.setHalt(false); // Enable internal oscillator (needed for working output)

void loop() {
  // Output high
  Serial.println("SQW High");
  rtc.writeRaw(SQW_OUT, SQW_HIGH);

  // Output 1 Hz
  Serial.println("SQW 1 Hz");
  rtc.writeRaw(SQW_OUT, SQW_1HZ);

  // Output 4.096 kHz
  Serial.println("SQW 4.096 kHz");
  rtc.writeRaw(SQW_OUT, SQW_4KHZ);

  // Output 8.192 kHz
  Serial.println("SQW 8.192 kHz");
  rtc.writeRaw(SQW_OUT, SQW_8KHZ);

  // Output 32.768 kHz
  Serial.println("SQW 32.768 kHz");
  rtc.writeRaw(SQW_OUT, SQW_32KHZ);

  // Output off
  Serial.println("SQW Off");
  rtc.writeRaw(SQW_OUT, SQW_LOW);
SQW High
SQW 1 Hz
SQW 4.096 kHz
SQW 8.192 kHz
SQW 32.768 kHz

Setting the Frequency

To use the SQW pin, we first need to enable the internal oscillator. This done with setHalt(false);.

To set the square wave frequency, use writeRaw to write to the register SQW_OUT. For the data to write, use one of the constants:

Be sure to use writeRaw [not write] to set the frequency. The constant values are in the raw BCD format.

Output Waveforms

Below are oscilloscope screenshots of the various wave frequencies:

1 Hz

1 Hz wave

4.096 kHz

4.096 kHz wave

8.192 kHz

8.192 kHz wave

32.768 kHz

32.768 kHz wave