ESP32: Guide for DS3231 Real Time Clock Module
⏰ What is DS1307 RTC?
The DS1307 is a real-time clock IC by Maxim Integrated that keeps track of:
- Seconds
- Minutes
- Hours (12 or 24-hour mode)
- Day, Date, Month, Year
- Automatically adjusts for leap years
✅ It communicates with microcontrollers using SPI (Serial Peripheral Interface) — unlike the more common DS1307, which uses I2C.
🔍 Key Features
| Feature | Details |
|---|---|
| Interface | SPI |
| Voltage | 2.0V – 5.5V |
| Timekeeping | Seconds, Minutes, Hours, Day, Date |
| Battery backup | Yes (CR1220 or CR2032 coin cell) |
| Oscillator | Requires external 32.768 kHz crystal |
| Alarm + Square Wave | Built-in (configurable) |
🧰 Components Required
| Component | Description |
|---|---|
| ESP32 board | Microcontroller |
| DS1306 RTC module | With CR2032 battery and SPI pins |
| Jumper wires | Male-to-female preferred |
| Breadboard | For clean prototyping |
| Arduino IDE | To program ESP32 |
🔌 DS1307 to ESP32 Wiring (SPI)
| DS1306 Pin | ESP32 Pin | Description |
|---|---|---|
| VCC | 3.3V or 5V | Power supply |
| GND | GND | Ground |
| SCLK | GPIO 18 | SPI Clock |
| MISO | GPIO 19 | SPI MISO (Data In) |
| MOSI | GPIO 23 | SPI MOSI (Data Out) |
| CS | GPIO 5 | SPI Chip Select |
Make sure you connect the battery for backup timekeeping.
📚 Step-by-Step in Arduino IDE
✅ Step 1: Install Required Library
Since DS1306 uses SPI, and isn’t as widely supported as DS3231 or DS1307, we’ll use the DS1306 library (by Paul Stoffregen or compatible fork).
- Go to Sketch > Include Library > Manage Libraries
- Search for and install:
DS1306RTC(if not found, you may need to manually add)
You may also use RTClib but it doesn’t directly support DS1306.
⏱️ What’s a Better Option?
If you’re using this RTC for standard timekeeping, you might find DS1307 or DS3231 (I2C-based) much easier and better supported in Arduino.
If you’re bound to DS1306, I can help you:
- Add a working library
- Write custom SPI functions to read/set time
- Convert to RTClib-compatible class
🧠 Applications of RTC Modules (like DS1307)
| Application | Description |
|---|---|
| ⏰ Digital Clock | Show real-world time & date |
| 📝 Data Logging | Timestamp for sensor readings |
| 🔋 Power Saving | Wake up ESP32 at a set time |
| 📦 IoT Projects | Sync events to time-based triggers |
| 📷 Camera Trap | Time-stamp wildlife photos/videos |
⚠️ DS1306 vs DS1307 vs DS3231
| Feature | DS1306 | DS1307 | DS3231 (Recommended) |
|---|---|---|---|
| Protocol | SPI | I2C | I2C |
| Accuracy | Medium | Low | ✅ Very High (±2ppm) |
| Battery Back | Yes | Yes | Yes |
| Code Support | Limited | Excellent | ✅ Excellent |
HOW TO OPERATE
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include "RTClib.h"
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
void setup () {
Serial.begin(57600);
#ifndef ESP8266
while (!Serial); // wait for serial port to connect. Needed for native USB
#endif
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
}
void loop () {
DateTime now = rtc.now();
Serial.print(now.year());
// Serial.print('/');
// Serial.print(now.month(), DEC);
// Serial.print('/');
// Serial.print(now.day(), DEC);
// Serial.print(" (");
// Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
// Serial.print(") ");
// Serial.print(now.hour(), DEC);
// Serial.print(':');
// Serial.print(now.minute(), DEC);
// Serial.print(':');
// Serial.print(now.second(), DEC);
Serial.println();
Serial.println();
delay(3000);
}

Leave a Reply
You must be logged in to post a comment.