How to Use DS1307 RTC Module with ESP32 for Clock and Alarm Projects
How to Use DS1307 RTC Module with ESP32 for Clock and Alarm Projects
This guide walks you through using the DS1307 RTC Module with ESP32 to read real time and trigger actions based on scheduled time. It covers circuit wiring, library installation, and code for setting up buzzer alarms.
What is an RTC Module
RTC (Real Time Clock) is a module that provides accurate time data. The DS1307 module includes:
- DS1307 clock IC with I2C communication
- 24C32 EEPROM for additional data storage
- CR2032 battery holder for backup power
- SDA and SCL pins for microcontroller connection
Required Components
- ESP32 NodeMCU ESP-WROOM-32
- DS1307 RTC Module
- Active Buzzer Module
- Breadboard
- Jumper wires (Male-Male and Male-Female)
- Micro USB cable for code upload
- CR2032 battery (for RTC backup)
Wiring the Circuit
Connect DS1307 RTC Module
| ESP32 | DS1307 Module |
|---|---|
| VIN (5V) | VCC |
| GND | GND |
| GPIO 21 (D21) | SDA |
| GPIO 22 (D22) | SCL |
Connect Active Buzzer Module
| ESP32 | Buzzer Module |
|---|---|
| 3V3 | VCC |
| GND | GND |
| GPIO 23 (D23) | I/O |
Important note: Different ESP32 board variants may have different I2C pin locations. Most ESP32 boards use GPIO 21 as SDA and GPIO 22 as SCL. Check your board’s datasheet if unsure.
Installing the Library
- Download the DS1307 Library (available from Arduino Library Manager or reputable websites)
- Extract and place the folder in
Documents/Arduino/libraries/ - Open Arduino IDE and verify installation via Sketch > Include Library
Code for Reading Time and Setting Alarm
#include <DS1307.h>
#include <Wire.h>
// Define buzzer pin
const int buzzerPin = 23;
// Variables for current time
int currentSecond = 0;
int currentMinute = 0;
int currentHour = 0;
// Alarm time settings
const int alarmHour = 0;
const int alarmMinute = 0;
const int alarmSecond = 5;
// Alarm trigger flag
bool alarmTriggered = false;
DS1307 rtc(21, 22); // SDA, SCL
void setup() {
Serial.begin(9600);
rtc.begin();
pinMode(buzzerPin, OUTPUT);
digitalWrite(buzzerPin, LOW);
// Set initial time (second, minute, hour, day, month, year)
// Comment this line after first setup to prevent time reset on every upload
// rtc.set(0, 30, 14, 28, 6, 2025); // Example: 14:30:00 June 28, 2025
}
void loop() {
// Read time from RTC
currentSecond = rtc.second();
currentMinute = rtc.minute();
currentHour = rtc.hour();
// Display time on Serial Monitor
Serial.print(currentHour);
Serial.print(":");
Serial.print(currentMinute);
Serial.print(":");
Serial.println(currentSecond);
// Check alarm condition
if (currentHour == alarmHour &&
currentMinute == alarmMinute &&
currentSecond == alarmSecond &&
!alarmTriggered) {
Serial.println("Alarm triggered");
// Sound buzzer for 2 seconds
for (int i = 0; i < 20; i++) {
digitalWrite(buzzerPin, HIGH);
delay(100);
digitalWrite(buzzerPin, LOW);
delay(100);
}
alarmTriggered = true;
}
// Reset flag when second changes
if (currentSecond != alarmSecond) {
alarmTriggered = false;
}
delay(1000);
}
Setting Initial Time
In line 28 of the code, set time using format (second, minute, hour, day, month, year). Example:
rtc.set(0, 30, 14, 28, 6, 2025); // 14:30:00 June 28, 2025
After setting the time, comment out this line (// rtc.set(...)) to prevent time reset on every code upload.
Configuring Alarm Time
Modify these values in the code:
const int alarmHour = 0; // Hour to trigger alarm
const int alarmMinute = 0; // Minute to trigger alarm
const int alarmSecond = 5; // Second to trigger alarm
For a 7:30 AM alarm, change to:
const int alarmHour = 7;
const int alarmMinute = 30;
const int alarmSecond = 0;
Uploading Code and Testing
- Select Board as ESP32 Dev Module
- Select connected Port
- Click Upload
- Open Serial Monitor at 9600 baud
- You will see time updating every second
- When alarm time is reached, buzzer will sound and Serial Monitor will display “Alarm triggered”
[image: Serial Monitor screen showing time values read from DS1307 with alarm trigger message when scheduled time is reached]
Important Notes
- CR2032 Battery - Must be installed for RTC to maintain time when powered off. Without it, time will reset on every power cycle.
- SDA/SCL Pins - Some ESP32 board variants may use different pins. Always verify from your board’s datasheet.
- I2C Address - DS1307 default address is 0x68. If using with other I2C devices, watch out for address conflicts.
Reference Video
อยากทำโปรเจคแบบนี้?
รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน
If you need Arduino project service or urgent IoT development, see full service details on the home page
จ้างทำโปรเจคเลย