DS1307 RTC with Arduino

Article thumbnail

Published

arduino rtc

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 NumberPin NameFunction
1X1Connection to 32.768 kHz quartz crystal
2X2Connection to 32.768 kHz quartz crystal
3VBATConnection to 3V battery
4GNDGround
5SDAI2C Data connection
6SCLI2C Clock connection
7SQW/OUTSquare wave output (open drain)
8VCC5V power supply

Schematic (Typical Operating Circuit)

DS1302 circuit 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.

Library

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. The LED circuit is included in the schematic above.

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 R4, 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.

/*
   SquareWave.ino
   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
   above.
*/

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

DS1307 rtc;

void setup() {
  Wire.begin();
  Serial.begin(9600);
  rtc.setHalt(false); // Enable internal oscillator (needed for working output)
}

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

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

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

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

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

  // Output off
  Serial.println("SQW Off");
  rtc.writeRaw(SQW_OUT, SQW_LOW);
  delay(5000);
}

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:

note:

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