DS1307 Arduino Tutorial: Keep Time with an RTC Module and Show It on an LCD1602 I2C
The DS1307 is a low-cost real-time clock that keeps the time, the date and 56 bytes of RAM, and it continues to count from a small backup battery when the Arduino loses power. This tutorial explains the module pinout, the wiring to an Arduino UNO, the RTClib setup, and the correct way to set the time once without resetting it at every restart. It then shows the clock on the Serial Monitor and on an LCD1602 I2C display, and it covers the square wave output and the battery-backed RAM.
The display part builds on the I2C LCD1602 tutorial, and the Arduino I2C tutorial explains the bus and the address scanner. For a more accurate clock with alarms, the DS3231 RTC tutorial is the better choice, and the Arduino sensor interfacing tutorials collect other I2C modules.
Table of contents
DS1307 RTC Module Overview
The DS1307 counts seconds, minutes, hours, the day of the week, the date, the month and the year, and it stores them in registers that the Arduino reads over I2C. A 32.768 kHz crystal sets the timing, and the chip switches to a backup battery when the main supply drops, so the time continues while the Arduino is off.

DS1307 Specifications
The values below come from the manufacturer datasheet.
| Feature | DS1307 |
|---|---|
| Supply voltage (VCC) | 4.5 to 5.5 V |
| Backup battery voltage | 2.0 to 3.5 V (3 V typical) |
| Interface | I2C, 7-bit address 0x68 |
| Maximum I2C clock | 100 kHz (standard mode only) |
| Time format | 12-hour or 24-hour |
| Battery-backed RAM | 56 bytes |
| Square wave output | 1 Hz, 4.096 kHz, 8.192 kHz or 32.768 kHz |
| Timing source | External 32.768 kHz crystal |
| Alarms | None |
DS1307 vs DS3231
The DS1307 is a good choice for a basic clock, but it is not as accurate as the DS3231. The datasheet gives no accuracy figure in ppm and states that the accuracy of the clock depends on the crystal, so temperature and noise change the result. Each 10 ppm of error adds up to about 26 seconds per month, which is why a DS1307 clock can drift by a minute or more over a few months. The DS3231 includes its own temperature-compensated oscillator and supports alarms, and it uses the same RTClib library, so the code for setting and reading the time needs only the class name changed. The DS3231 also runs from 3.3 V, while the DS1307 needs 4.5 V or more.
DS1307 Applications
The DS1307 suits data loggers, digital clocks, timers, scheduled switching and any project that needs a time stamp. A project that needs alarms or better accuracy should use the DS3231, and a project with a WiFi board can also get the time from the network.
DS1307 Pinout
A typical DS1307 breakout board has the pins listed below. The labels differ slightly between manufacturers, so the printing on the board should be checked.
DS1307 Module Pin Description
| Pin | Function |
|---|---|
| SCL | I2C clock |
| SDA | I2C data |
| VCC | Power supply, 5 V |
| GND | Ground |
| SQ (SQW) | Square wave output of the DS1307, which is an open-drain pin |
| DS | Data line of the optional DS18B20 footprint, found on some modules only |
| BAT | Backup battery pin, for a coin cell in the holder or an external battery |
DS1307 Arduino Wiring
The RTC and the LCD share the same two I2C wires, so the whole circuit needs only four connections per module.
DS1307 and LCD1602 I2C Connections
| Module pin | Arduino UNO pin |
|---|---|
| DS1307 VCC and LCD1602 VCC | 5V |
| DS1307 GND and LCD1602 GND | GND |
| DS1307 SDA and LCD1602 SDA | A4 |
| DS1307 SCL and LCD1602 SCL | A5 |
A4 and A5 are the default I2C pins of the UNO and the Nano. The wires should be kept short for stable I2C communication, and the Arduino UNO pinout guide lists the pins of the board.
I2C Addresses on the Shared Bus
| Device | I2C address |
|---|---|
| DS1307 | 0x68 |
| LCD1602 I2C backpack | 0x27 (0x3F on some backpacks) |
The two addresses are different, so both devices work on the same bus without a conflict. An I2C scanner can confirm the address of each device.
DS1307 Supply Voltage and Battery
The datasheet specifies a supply of 4.5 V to 5.5 V, so the module should be powered from 5 V and not from the 3.3 V pin. A module that is powered from 5 V usually has its I2C pull-up resistors tied to VCC, so it should not be connected directly to a 3.3 V board without checking the schematic.
The backup battery must be between 2.0 V and 3.5 V. Some DS1307 modules are designed for a rechargeable LIR2032 cell and include a small charging circuit, which is not suitable for a non-rechargeable CR2032. The documentation or the schematic of the module should be checked before a battery is inserted.
DS1307 Arduino Library Setup
The sketches use the RTClib library from Adafruit for the clock and the LiquidCrystal_I2C library for the display.
Install RTClib
- Open the Arduino IDE and go to Sketch, then Include Library, then Manage Libraries.
- Search for RTClib.
- Install the library published by Adafruit, and accept the offer to install its dependencies.
Install LiquidCrystal_I2C
- Search for LiquidCrystal I2C in the Library Manager.
- Install the version by Frank de Brabander.
Test the Library Examples
Before the tutorial sketches are used, the library examples can confirm the wiring. The ds1307 example of RTClib prints the time on the Serial Monitor, and the HelloWorld example of the LCD library prints text on the display.
Set DS1307 Time and Date
The time is written to the DS1307 with rtc.adjust(), and the chip then keeps it running on its own. The call should therefore run once and not at every start of the sketch, because a sketch that adjusts the clock in setup() sets the time back to the compile time after every reset.
At the first power-up the registers of the DS1307 are undefined, and the datasheet requires the Clock Halt bit to be cleared during the initial setup. The function rtc.isrunning() reports whether the oscillator is running, which makes it possible to set the time only when the clock has never been started.
Set the Time from the Compile Time
This sketch copies the date and the time of the computer from the moment of compilation. It should be uploaded immediately after it is compiled, because the time of the clock is behind by the time that the upload takes.
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
void setup() {
Serial.begin(9600);
Wire.begin();
if (!rtc.begin()) {
Serial.println("Couldn't find the DS1307. Check the wiring and the 5V supply.");
while (true) { }
}
// Sets the RTC to the date and time of the computer at compile time
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
Serial.println("RTC time set. Upload the reading sketch next so that the time is not set again.");
}
void loop() { }Set the Time Manually
A fixed time is set by passing the year, month, day, hour, minute and second to the DateTime constructor. The line replaces the compile-time line of the previous sketch.
rtc.adjust(DateTime(2026, 11, 22, 21, 30, 0)); // Year, month, day, hour, minute, secondDS1307 Time and Date Functions
| Function | Purpose |
|---|---|
| rtc.begin() | Starts I2C and returns true when the DS1307 answers |
| rtc.adjust(DateTime) | Writes a new date and time to the clock |
| rtc.isrunning() | Returns 1 when the oscillator runs and 0 when the clock is halted |
| rtc.now() | Returns the current date and time as a DateTime object |
| now.year(), now.month(), now.day() | Return the date |
| now.hour(), now.minute(), now.second() | Return the time in 24-hour format |
| now.dayOfTheWeek() | Returns the day of the week as a number from 0 (Sunday) to 6 (Saturday) |
| now.unixtime() | Returns the seconds since 1 January 1970 |
Read DS1307 Time and Date
The sketch reads the clock once per second and prints the date and the time on the Serial Monitor. It uses snprintf() with the format %02d, so every field has two digits and a time such as 9:05:03 is not printed as 9:5:3. It also shows the 12-hour format, which is calculated from the 24-hour hour value.
Read Time and Date Sketch
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
const char *daysOfTheWeek[7] = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
void setup() {
Serial.begin(9600);
Wire.begin();
if (!rtc.begin()) {
Serial.println("Couldn't find the DS1307. Check the wiring and the 5V supply.");
while (true) { }
}
// A halted clock returns a wrong time until it has been set
if (!rtc.isrunning()) {
Serial.println("The DS1307 is not running. Upload the set time sketch first.");
}
}
void loop() {
DateTime now = rtc.now();
// 24-hour format with leading zeros
char line[40];
snprintf(line, sizeof(line), "%s %02d/%02d/%04d %02d:%02d:%02d",
daysOfTheWeek[now.dayOfTheWeek()],
now.day(), now.month(), now.year(),
now.hour(), now.minute(), now.second());
Serial.print(line);
// 12-hour format calculated from the 24-hour value
uint8_t hour12 = now.hour() % 12;
if (hour12 == 0) hour12 = 12; // 0 and 12 o'clock are both shown as 12
const char *period = (now.hour() >= 12) ? "PM" : "AM";
char clock12[16];
snprintf(clock12, sizeof(clock12), " (%02d:%02d:%02d %s)",
hour12, now.minute(), now.second(), period);
Serial.println(clock12);
delay(1000);
}How the Read Sketch Works
- rtc.now() reads all time registers in one transfer and returns them as a DateTime object, so the fields always belong to the same moment.
- The daysOfTheWeek array turns the number from dayOfTheWeek() into a name.
- The 12-hour value is derived with a modulo operation, and an hour value of 0 is replaced by 12.
- The 24-hour format is the better choice for logs, because it has no AM and PM and sorts correctly.
DS1307 LCD1602 I2C Clock
This sketch shows the date on the first line and the time on the second line of an LCD1602 with an I2C backpack. The LCD and the DS1307 share the same SDA and SCL lines, and their addresses (0x27 or 0x3F, and 0x68) do not collide. The basics of the display are covered in the I2C LCD1602 Arduino tutorial.
Clock Sketch with Automatic First-Time Setup
The time is set from the compile time only when the DS1307 is not running. After that the clock keeps its own time through resets and power cycles, as long as the battery is fitted. Set FORCE_SET_TIME to 1 for one upload when the time has to be corrected, and set it back to 0 afterwards.
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
#define FORCE_SET_TIME 0 // Set to 1 for one upload to overwrite the stored time
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change 0x27 to 0x3F if the display stays blank
void setup() {
Serial.begin(9600);
Wire.begin();
lcd.init();
lcd.backlight();
if (!rtc.begin()) {
lcd.print("DS1307 not found");
Serial.println("Couldn't find the DS1307.");
while (true) { }
}
// Set the time only on the first start or when it is forced
if (!rtc.isrunning() || FORCE_SET_TIME) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
DateTime now = rtc.now();
char line[17];
lcd.setCursor(0, 0);
snprintf(line, sizeof(line), "Date %02d/%02d/%04d", now.day(), now.month(), now.year());
lcd.print(line);
lcd.setCursor(0, 1);
snprintf(line, sizeof(line), "Time %02d:%02d:%02d", now.hour(), now.minute(), now.second());
lcd.print(line);
delay(500);
}How the LCD Sketch Works
- Each line has exactly 16 characters, so the old text is fully overwritten and the display does not need lcd.clear(), which avoids flicker.
- The function lcd.init() is used here because it belongs to the LiquidCrystal_I2C library by Frank de Brabander. The older sketch mixed it with lcd.begin(), which belongs to other libraries.
- The loop refreshes twice per second, so the seconds never appear to skip.
DS1307 Square Wave Output
The SQ pin of the DS1307 can output a square wave of 1 Hz, 4.096 kHz, 8.192 kHz or 32.768 kHz. The pin is open-drain, so it needs a pull-up resistor to VCC before it can be read by an Arduino pin or drive an LED. Some modules already carry a pull-up on the board, and this should be checked on the module in use.
Square Wave Sketch
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
void setup() {
Serial.begin(9600);
Wire.begin();
if (!rtc.begin()) {
Serial.println("Couldn't find the DS1307.");
while (true) { }
}
// Choose one: DS1307_OFF, DS1307_ON, DS1307_SquareWave1HZ,
// DS1307_SquareWave4kHz, DS1307_SquareWave8kHz, DS1307_SquareWave32kHz
rtc.writeSqwPinMode(DS1307_SquareWave1HZ);
Serial.print("SQW mode register value: ");
Serial.println(rtc.readSqwPinMode());
}
void loop() { }With the 1 Hz setting, an LED whose anode goes to VCC through a resistor and whose cathode goes to the SQ pin blinks once per second, because the open-drain output pulls the cathode to ground on every second half-period. A logic analyzer or an oscilloscope is needed to inspect the 4 kHz and higher outputs, because they are too fast to see on an LED.
DS1307 RAM Read and Write
The DS1307 has 56 bytes of battery-backed RAM. RTClib addresses them from 0 to 55, and the values stay stored as long as the module has power from either VCC or the backup battery. This RAM is not EEPROM. A dead battery combined with a power loss clears it, so it suits small settings and counters but not data that must survive for years.
RAM Sketch
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Wire.begin();
lcd.init();
lcd.backlight();
if (!rtc.begin()) {
lcd.print("DS1307 not found");
while (true) { }
}
// Write two bytes to RAM addresses 0 and 1 (valid range is 0 to 55)
rtc.writenvram(0, 123);
rtc.writenvram(1, 45);
// Read the same bytes back
uint8_t first = rtc.readnvram(0);
uint8_t second = rtc.readnvram(1);
char line[17];
lcd.setCursor(0, 0);
snprintf(line, sizeof(line), "RAM[0] = %3u", first);
lcd.print(line);
lcd.setCursor(0, 1);
snprintf(line, sizeof(line), "RAM[1] = %3u", second);
lcd.print(line);
}
void loop() { }The display should show 123 and 45. A different result means that the write failed, and the wiring and the supply voltage should be checked first.
DS1307 Arduino Troubleshooting
| Problem | Likely cause | Fix |
|---|---|---|
| “Couldn’t find the DS1307” | Wrong wiring, no 5V supply or a missing common ground | Check SDA on A4, SCL on A5, VCC on 5V and GND, then run an I2C scanner and look for 0x68 |
| Time stays at 00:00:00 or does not change | The oscillator is halted because the clock was never set | Upload the set time sketch once, or use FORCE_SET_TIME |
| Time resets to the compile time after every upload | rtc.adjust() runs unconditionally in setup() | Guard it with !rtc.isrunning() as in the clock sketch |
| Time is lost when the Arduino loses power | The backup battery is missing, empty or fitted the wrong way round | Fit a CR2032 with the correct polarity and check its voltage |
| Time drifts by seconds per day | A low-accuracy 32.768 kHz crystal that is sensitive to temperature | Use a DS3231 where better accuracy is needed |
| LCD stays blank or shows boxes | Wrong I2C address or contrast | Scan for 0x27 or 0x3F and turn the contrast potentiometer on the backpack |
| Random or wrong values | Missing I2C pull-ups or a long, noisy cable | Keep the wires short and make sure the module or the Arduino provides pull-ups |
Conclusion
A DS1307 module gives an Arduino a clock that keeps running while the board is off. The important points are to set the time only once, to keep the supply at 5V, to fit a battery with the right polarity and to use the library calls that match the chip. With these in place the same code runs on an LCD1602, a Serial Monitor log or any other output.
For a clock with better accuracy, the next step is the DS3231 module, and the sensor tutorials show how to attach a timestamp to measurements.
Browse More Arduino Modules Tutorials

Arun is an embedded systems engineer with 10+ years of experience in STM32, ESP32, and AVR microcontrollers. He created ControllersTech to share practical tutorials on embedded software, HAL drivers, RTOS, and hardware design — grounded in real industrial automation experience.
Recommended Tools
Essential dev tools
Categories
Browse by platform








