Back to Tutorial
esp32 with ds3231 tutorial

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

FeatureDetails
InterfaceSPI
Voltage2.0V – 5.5V
TimekeepingSeconds, Minutes, Hours, Day, Date
Battery backupYes (CR1220 or CR2032 coin cell)
OscillatorRequires external 32.768 kHz crystal
Alarm + Square WaveBuilt-in (configurable)

🧰 Components Required

ComponentDescription
ESP32 boardMicrocontroller
DS1306 RTC moduleWith CR2032 battery and SPI pins
Jumper wiresMale-to-female preferred
BreadboardFor clean prototyping
Arduino IDETo program ESP32

🔌 DS1307 to ESP32 Wiring (SPI)

DS1306 PinESP32 PinDescription
VCC3.3V or 5VPower supply
GNDGNDGround
SCLKGPIO 18SPI Clock
MISOGPIO 19SPI MISO (Data In)
MOSIGPIO 23SPI MOSI (Data Out)
CSGPIO 5SPI 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).

  1. Go to Sketch > Include Library > Manage Libraries
  2. 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)

ApplicationDescription
⏰ Digital ClockShow real-world time & date
📝 Data LoggingTimestamp for sensor readings
🔋 Power SavingWake up ESP32 at a set time
📦 IoT ProjectsSync events to time-based triggers
📷 Camera TrapTime-stamp wildlife photos/videos

⚠️ DS1306 vs DS1307 vs DS3231

FeatureDS1306DS1307DS3231 (Recommended)
ProtocolSPII2CI2C
AccuracyMediumLow✅ Very High (±2ppm)
Battery BackYesYesYes
Code SupportLimitedExcellent✅ 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);
}

Share this post

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Tutorial