Table of Contents
KEY TAKEAWAYS
- The DS1307 is an I2C real-time clock (RTC) that keeps time even when the main system is powered off
- Communication uses the I2C protocol with the DS1307 at address 0x68
- Time data is stored in BCD (Binary-Coded Decimal) format and must be converted for display
- ESP-IDF provides I2C driver APIs for reading and writing RTC registers
Prerequisites:
- An ESP32 development board
- A DS1307 RTC module
- Jumper wires
- ESP-IDF development framework installed on your computer
Connect DS1307 RTC module to ESP32
let’s connect the DS1307 RTC module to the ESP32. Here is the wiring configuration:- Connect VCC of DS1307 to 3.3V of ESP32
- Connect GND of DS1307 to GND of ESP32
- Connect SDA of DS1307 to GPIO21 (SDA) of ESP32
- Connect SCL of DS1307 to GPIO22 (SCL) of ESP32
Configure the I2C Interface
Next, we need to configure the I2C interface in the ESP-IDF project. In themain.c file, include the required headers#include "esp_log.h" #include "driver/i2c.h"
Define I2C parameters:
#define I2C_MASTER_SCL_IO 22 #define I2C_MASTER_SDA_IO 21 #define I2C_MASTER_NUM I2C_NUM_0 #define I2C_MASTER_TX_BUF_DISABLE 0 #define I2C_MASTER_RX_BUF_DISABLE 0 #define I2C_MASTER_FREQ_HZ 100000
static void i2c_master_init() {
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = I2C_MASTER_SDA_IO,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_io_num = I2C_MASTER_SCL_IO,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = I2C_MASTER_FREQ_HZ
};
i2c_param_config(I2C_MASTER_NUM, &conf);
i2c_driver_install(I2C_MASTER_NUM, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
}
Create DS1307 RTC Functions
Create a function to read the time from the DS1307 RTC module:
static esp_err_t ds1307_read_time(struct tm *timeinfo) {
// Your code to read time from DS1307 RTC
}
Create a function to set the time on the DS1307 RTC module:
static esp_err_t ds1307_set_time(const struct tm *timeinfo) {
// Your code to set time on DS1307 RTC
}
Implement the Main Function
In themain function, initialize the I2C interface and read the time from the DS1307 RTC:void app_main() {
i2c_master_init();
struct tm timeinfo;
if (ds1307_read_time(&timeinfo) == ESP_OK) {
char strftime_buf[64];
strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
ESP_LOGI("DS1307", "Current time: %s", strftime_buf);
} else {
ESP_LOGE("DS1307", "Failed to read time from DS1307 RTC");
}
// Set the time if needed
// Example: May 3, 2023, 15:30:00
timeinfo.tm_year = 2023 - 1900;
timeinfo.tm_mon = 5 - 1;
timeinfo.tm_mday = 3;
timeinfo.tm_hour = 15;
timeinfo.tm_min = 30;
timeinfo.tm_sec = 0;
if (ds1307_set_time(&timeinfo) == ESP_OK) {
ESP_LOGI("DS1307", "Time set successfully on DS1307 RTC");
} else {
ESP_LOGE("DS1307", "Failed to set time on DS1307 RTC");
}
// Read the time again after setting it
if (ds1307_read_time(&timeinfo) == ESP_OK) {
char strftime_buf[64];
strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
ESP_LOGI("DS1307", "Updated time: %s", strftime_buf);
} else {
ESP_LOGE("DS1307", "Failed to read updated time from DS1307 RTC");
}
}
Compile and Flash the Firmware
Now, compile the firmware and flash it to the ESP32:idf.py build idf.py -p [YOUR_SERIAL_PORT] flash monitorReplace `[YOUR_SERIAL_PORT]` with the appropriate serial port for your ESP32 development board. After flashing the firmware, you should see the current time from the DS1307 RTC displayed in the serial monitor. If you set a new time, you‘ll also see the updated time after setting it.
Conclusion:
In this tutorial, we showed you how to interface the DS1307 RTC module with the ESP32 using the ESP-IDF development framework. This setup allows you to maintain accurate time information in your projects. You can now use the DS1307 RTC to add timekeeping functionality to various applications, such as data logging, alarms, or home automation systems.This function initializes the I2C interface, reads the current time from the DS1307 RTC, sets a new time if needed, and reads the updated time after setting it. The output can be viewed in the serial monitor. If you have any other questions or need further clarification, please let me know.
Vivek Bhageria — Lead Firmware R&D Engineer, 12+ years. Ex-Bosch (automotive powertrain), MusicTribe (real-time audio), medical devices. M.Tech BITS Pilani. I write at NerdyElectronics — practical, register-level embedded systems for engineers who want to understand what’s actually happening under the hood.







