Skip to content

DS1307 RTC with ESP32 on ESP-IDF

DS1307 RTC with ESP32

The DS1307 Real-Time Clock (RTC) is a widely used module for adding accurate timekeeping functionality to microcontroller projects. Using DS1307 RTC with ESP32 can add great functionality for the projects. Why would we need an RTC with ESP32? There may be situations where you want to take some action at a particular time, but there is no internet connection to read the current time using NTP. In this case, the RTC can be very helpful.

Some time back, I explained how to interface RTC with Arduino; DS1307 and DS3231

In this tutorial, we will demonstrate how to interface the DS1307 RTC module with an ESP32 using the ESP-IDF development framework. This will allow you to synchronize and maintain accurate time information in your projects.

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 the main.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

 

Now, create a function to initialize the I2C interface:

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 the main 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 monitor

 

Replace `[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.

Leave a Reply

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