Judy@4pcba.com
7:30 AM - 7:30 PM
Monday to Saturday

DS1307 Pinout: An in-depth Guide to the Real-Time Clock IC

Introduction to the DS1307 Real-Time Clock

The DS1307 is a popular real-time clock (RTC) integrated circuit used in many electronic projects and products to keep accurate time. It includes a battery backup that allows it to continue tracking time even when the main power is removed. The DS1307 communicates with a microcontroller or other device using the I2C serial interface.

In this comprehensive DS1307 Guide, we will cover the pinout and functions of this versatile RTC chip. By the end, you will understand how to connect and use the DS1307 in your own projects.

DS1307 Features and Specifications

Before diving into the details of the DS1307 pinout, let’s review some of the key features and specifications of this real-time clock IC:

  • Counts seconds, minutes, hours, day, date, month, and year, with leap-year compensation valid up to 2100
  • 56-byte, battery-backed, nonvolatile (NV) RAM for data storage
  • Two-wire serial interface (I2C)
  • Programmable square-wave output signal
  • Automatic power-fail detect and switch circuitry
  • Consumes less than 500nA in battery backup mode with oscillator running
Parameter Value
Supply Voltage (VCC) 4.5V to 5.5V
Operating Temperature -40°C to +85°C
Timekeeping Accuracy ±2ppm from 0°C to +40°C
I2C Clock Frequency 100kHz max

Now that we have an overview of the DS1307’s capabilities, let’s examine the pinout and functions of each pin.

DS1307 Pin Configuration and Functions

The DS1307 comes in an 8-pin DIP or SO package. Here is a table showing the pinout:

Pin Name Function
1 X1 32.768kHz Crystal Connection
2 X2 32.768kHz Crystal Connection
3 VBAT +3V Battery Input
4 GND Ground
5 SDA Serial Data Input/Output
6 SCL Serial Clock Input
7 SQW/OUT Square Wave/Output Driver
8 VCC +5V Power Supply

X1 and X2 (Pins 1 and 2)

The X1 and X2 pins are used to connect a 32.768kHz quartz crystal, which serves as the time base for the DS1307. The crystal load capacitance is 12.5pF. For best accuracy, use a crystal with a specified load capacitance of 12.5pF.

VBAT (Pin 3)

The VBAT pin is the backup supply input for the DS1307. This pin should be connected to a 3V lithium battery or other backup power source. When VCC falls below the switching threshold, the DS1307 will switch from VCC to the backup supply and maintain timekeeping. Typical current draw is less than 500nA with a 32kHz crystal.

GND (Pin 4)

The GND pin is the ground reference for the DS1307. Connect this pin to the common ground of your circuit.

SDA (Pin 5)

SDA is the serial data input/output for the I2C interface. This pin is bidirectional and open-drain, so it requires a pull-up resistor. When writing to the DS1307, SDA is an input. When reading from the DS1307, SDA is an output.

SCL (Pin 6)

SCL is the serial clock input for the I2C interface. Like SDA, this pin is also bidirectional and open-drain, requiring a pull-up resistor.

SQW/OUT (Pin 7)

The SQW/OUT pin can output a square wave signal with one of four frequencies: 1Hz, 4kHz, 8kHz, or 32kHz. The frequency is software programmable. This pin is an open drain output and requires a pull-up resistor. If the square wave output is not used, this pin can be left unconnected.

VCC (Pin 8)

The VCC pin is the main power supply pin for the DS1307. A 5V supply should be connected to this pin. When VCC drops below the backup switchover threshold, the DS1307 will switch to battery power on VBAT to maintain timekeeping.

Connecting the DS1307 to a Microcontroller

To use the DS1307 in a project, you’ll need to connect it to a microcontroller or other device via the I2C interface. Here’s a typical connection diagram:

        DS1307              Microcontroller

   X1 -- 32kHz Crystal -- X1
   X2 -- 32kHz Crystal -- X2 
 VBAT -------------- +3V Battery
  GND ------------------------ GND
  SDA ------------------------ SDA
  SCL ------------------------ SCL 
  SQW --+
        |
        +--[10K]--+5V
  VCC -------------- +5V

Note the pull-up resistors on the SDA and SCL lines (typically 4.7kΩ), and the pull-up on SQW if that output is used. The I2C address of the DS1307 is fixed at 1101000.

Using the DS1307

Setting and Reading the Time

The DS1307 maintains the time in BCD format. To set the current time, write the 7 time bytes (seconds, minutes, hours, day, date, month, year) to the appropriate register addresses.

For example, to set the time to 1:30:00 PM on Monday, January 14, 2023:
– Write 0x00 to register 0x00 (seconds)
– Write 0x30 to register 0x01 (minutes)
– Write 0x13 to register 0x02 (hours, 24-hr format)
– Write 0x02 to register 0x03 (day of week, Monday=2)
– Write 0x14 to register 0x04 (date)
– Write 0x01 to register 0x05 (month)
– Write 0x23 to register 0x06 (year)

To read the current time, simply read the 7 time bytes from the DS1307 registers.

Using the Square Wave Output

The square wave output frequency on the SQW pin can be set by writing to the control register at address 0x07:

Bits Value SQW Output
RS1:RS0 00 1 Hz
RS1:RS0 01 4.096 kHz
RS1:RS0 10 8.192 kHz
RS1:RS0 11 32.768 kHz

For example, to enable the 1Hz output, write 0x10 to register 0x07.

Using the RAM

The DS1307 includes 56 bytes of battery-backed RAM that can be used to store data. The RAM registers start at address 0x08 and go up to 0x3F. To write data, simply write a byte to the desired address. To read data, read a byte from the desired RAM address.

Example Code

Here is some example Arduino code showing how to set the time on the DS1307 and read it back:

#include <Wire.h>

#define DS1307_ADDRESS 0x68

void setup() {
  Wire.begin();
  Serial.begin(9600);

  // Set the time to 1:30:00 PM, Monday Jan 14, 2023
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(0x00); // Set register pointer to seconds reg
  Wire.write(0x00); // 0 seconds
  Wire.write(0x30); // 30 minutes
  Wire.write(0x13); // 13 hrs (1 PM)  
  Wire.write(0x02); // Monday
  Wire.write(0x14); // 14th
  Wire.write(0x01); // January
  Wire.write(0x23); // 2023
  Wire.endTransmission();
}

void loop() {
  // Read current time
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(0x00); // Set register pointer to seconds reg
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);
  int seconds = bcdToDec(Wire.read());
  int minutes = bcdToDec(Wire.read());
  int hours = bcdToDec(Wire.read());
  int dayOfWeek = bcdToDec(Wire.read());
  int date = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());

  // Print the time
  Serial.print(hours);
  Serial.print(":");
  Serial.print(minutes);
  Serial.print(":");
  Serial.print(seconds);
  Serial.print(" ");
  Serial.print(month);
  Serial.print("/");
  Serial.print(date);
  Serial.print("/");
  Serial.print(year);
  Serial.println();

  delay(1000);
}

// Convert BCD to decimal
int bcdToDec(byte bcd) {
  return (bcd/16*10) + (bcd%16); 
}

This code sets the initial time to 1:30:00 PM on Monday, January 14, 2023. It then repeatedly reads the current time from the DS1307 every second and prints it to the serial monitor.

Troubleshooting

If you encounter issues with the DS1307, here are a few things to check:

  • Make sure the DS1307 is properly connected, with VCC at 5V, GND grounded, and SDA/SCL connected to the microcontroller (with pull-up resistors).
  • Verify the backup battery is installed and has sufficient charge (>3V).
  • Check that you are using the correct I2C address (0x68).
  • Ensure the crystal is 32.768kHz with a load capacitance of 12.5pF.
  • Confirm that you are reading and writing the time data in BCD format.

DS1307 Applications

The DS1307 real-time clock is widely used in embedded systems, data loggers, clocks, and timers. Some example applications include:

  • Adding a clock to a microcontroller project
  • Timestamping sensor data in a logger
  • Building a digital clock or watch
  • Timing events or triggering actions at specific times
  • Keeping time in batterypowered devices

FAQ

Q1: What happens if the DS1307 loses power?

A1: If the DS1307 loses VCC power but has a 3V backup battery connected to VBAT, it will switch to battery power and continue timekeeping. The time and settings will be maintained. When VCC is restored, it will seamlessly switch back to VCC power.

Q2: How long will the DS1307 run on battery power?

A2: The DS1307 can typically run for more than 10 years on a standard 3V lithium coin cell while maintaining timekeeping, thanks to its low backup current draw of less than 500nA.

Q3: Is the DS1307 Y2K compliant?

A3: Yes, the DS1307 correctly handles leap years and will continue to keep accurate time beyond the year 2000 and up to the year 2100.

Q4: What is the accuracy of the DS1307?

A4: With a quality 32.768kHz crystal, the DS1307 maintains a timekeeping accuracy of ±2ppm from 0°C to +40°C. This equates to an accuracy of better than ±1 minute per year.

Q5: Can I use the DS1307 with a 3.3V microcontroller?

A5: The DS1307 is designed for 5V operation. While it may work with 3.3V logic (the minimum high input is 2.2V), for reliable operation it is recommended to use level shifters to interface the DS1307 with a 3.3V microcontroller. The VBAT and VCC pins must always be supplied with their respective voltages (3V and 5V).

Conclusion

This concludes our in-depth look at the DS1307 real-time clock and its pinout. With its simple I2C interface, battery backup capability, and easy integration, the DS1307 is an excellent choice for adding timekeeping to your electronic projects. I hope this DS1307 guide has given you the knowledge needed to get the most out of this versatile RTC chip.