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

Posted by

What is the DS1307 RTC?

The DS1307 is a low-power, full binary-coded decimal (BCD) clock/calendar plus 56 bytes of NV SRAM. It communicates via an I2C bi-directional bus and requires minimal external components, making it an ideal solution for adding real-time clock functionality to microcontroller-based projects.

Key Features of the DS1307

  • Counts seconds, minutes, hours, date of the month, month, day of the week, and year with leap-year compensation valid up to 2100
  • 56-byte, battery-backed, nonvolatile (NV) RAM for data storage
  • I2C serial interface
  • Programmable square-wave output signal
  • Automatic power-fail detect and switch circuitry
  • Consumes less than 500nA in battery backup mode

DS1307 Pinout Diagram

The DS1307 comes in an 8-pin DIP (Dual Inline Package) or SO (Small Outline) package. The pinout diagram is as follows:

Pin Name Description
1 X1 32.768kHz crystal connection
2 X2 32.768kHz crystal connection
3 VBAT +3V battery input for backup power
4 GND Ground
5 SDA Serial Data I/O
6 SCL Serial Clock input
7 SQW/OUT Square Wave/Output driver
8 VCC +5V power supply

Connecting the DS1307 to a Microcontroller

To use the DS1307 with a microcontroller, you’ll need to connect the following pins:

  1. VCC to the +5V power supply
  2. GND to ground
  3. SDA to the microcontroller’s I2C data pin
  4. SCL to the microcontroller’s I2C clock pin

Additionally, connect a 32.768kHz crystal between the X1 and X2 pins, and a 3V battery to the VBAT pin for backup power.

Pull-up Resistors

The I2C bus requires pull-up resistors on the SDA and SCL lines. The DS1307 includes internal pull-up resistors, but depending on your bus capacitance and clock speed, you may need to add external pull-up resistors. Typical values range from 1.5kΩ to 10kΩ.

Setting and Reading the Time

To set and read the time on the DS1307, you’ll need to use the I2C protocol to communicate with the device. The DS1307 has several registers that store the current time and date information:

Register Description
00h Seconds (BCD format)
01h Minutes (BCD format)
02h Hours (BCD format, 12/24-hour mode)
03h Day of the week (1-7)
04h Date (BCD format)
05h Month (BCD format)
06h Year (BCD format, 00-99)
07h Control Register

To set the time, write the appropriate BCD values to the registers. To read the time, simply read the values from the registers and convert them from BCD to decimal format.

Example Code (Arduino)

Here’s an example of how to set and read the time on the DS1307 using an Arduino:

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;

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

  // Set the time (uncomment to set)
  // rtc.adjust(DateTime(2023, 4, 15, 10, 30, 0));
}

void loop() {
  DateTime now = rtc.now();

  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();

  delay(1000);
}

Square Wave Output

The DS1307 provides a programmable square wave output on the SQW/OUT pin. This can be useful for generating interrupts or providing a clock signal for other devices. The frequency of the square wave can be set by writing to the control register (07h):

Bits Description
7-4 Not used
3-0 Square wave frequency:
– 0000: 1Hz
– 0001: 4.096kHz
– 0010: 8.192kHz
– 0011: 32.768kHz
– Other values: Output is HIGH

To enable the square wave output, set the SQWE bit (bit 4) in the control register to 1.

Battery Backup

The DS1307 includes a built-in battery backup circuit that maintains the time and RAM contents when the main power supply is absent. To use this feature, connect a 3V lithium battery (CR2032 or equivalent) to the VBAT pin. The DS1307 will automatically switch to battery power when the main supply is removed.

Troubleshooting

If you encounter issues with the DS1307, consider the following:

  • Verify that the device is receiving power (VCC and GND connections)
  • Check the I2C connections (SDA and SCL) and ensure that pull-up resistors are present
  • Ensure that the crystal is connected correctly and oscillating at 32.768kHz
  • Make sure the battery is connected and has sufficient charge for backup operation
  • Double-check the I2C address and communication protocol in your code

Conclusion

The DS1307 is a versatile and easy-to-use real-time clock IC that can add timekeeping functionality to a wide range of projects. By understanding the pinout and how to interface with the device, you can easily integrate the DS1307 into your designs and take advantage of its features, such as battery backup and square wave output.

FAQs

  1. Q: Can I use a different crystal frequency with the DS1307?
    A: No, the DS1307 is designed to work with a 32.768kHz crystal. Using a different frequency crystal will result in inaccurate timekeeping.

  2. Q: How long will the DS1307 retain time and RAM contents on battery power?
    A: The battery backup duration depends on the battery capacity and the DS1307’s power consumption. A typical CR2032 battery can maintain time and RAM for several years.

  3. Q: Can I use the DS1307 with a 3.3V microcontroller?
    A: Yes, the DS1307 is compatible with 3.3V logic levels. However, the VCC pin must still be connected to a 5V supply.

  4. Q: How accurate is the DS1307?
    A: The DS1307’s accuracy depends on the accuracy of the 32.768kHz crystal. With a typical crystal, the DS1307 can maintain time within ±2 minutes per month.

  5. Q: Can I use multiple DS1307 devices on the same I2C bus?
    A: No, the DS1307 has a fixed I2C address (0x68) and cannot be changed. If you need multiple RTCs on the same bus, consider using the DS3231 or other RTC ICs with selectable addresses.

Leave a Reply

Your email address will not be published. Required fields are marked *