Real time clock with DS1302

Real time clock with DS1302 is designed using Arduino Uno.

About DS1302

The DS1302 is a trickle-charge timekeeping chip containing a real-time clock/calendar and 31 bytes of static RAM. It communicates with a microprocessor via a simple serial interface. The real-time clock/calendar provides seconds, minutes, hours, day, date, month, and year information. DS1302 requires only three wires to communicate with the clock/RAM: CE, I/O (data line), and SCLK (serial clock). It has dual power pins, one for primary and another for backup.  In this example the Primary power will be provided by the Arduino, and the back up by a CR2032 battery.

During reading, the clock could rollover. That would result in bad clock data. To prevent that, the DS1302 has a buffer to store the clock data. That buffer can be read in a single communication session, called a “burst” mode.

The Year data of the DS1302 is only two digits (0-99). The Year ‘0’ is 2000, and not 1970. It has a Leap-Year compensation from 2000 up to 2099 (for a value of 0-99).The data in this ram will get lost if the Arduino is off, and the battery gets empty.

What is required to build the real time clock?

You will need following parts/components

  • Arduino Uno
  • DS1302 Module
  • Button cell

How to connect to Arduino?

The DS1302 can be easily connected to the Arduino. Three pins are needed for the interface (CE, I/O, SCLK), and Vcc2 should be connected to +5V (or +3.3V). The Vcc1 is for a battery .

A crystal of 32.768kHz should be connected to X1 and X2.

The DS1302 can run with a voltage from 2.0V to 5.5V.

Sample Code

// Example sketch for interfacing with the DS1302 timekeeping chip.
#include <stdio.h>
#include <DS1302.h>
namespace {
// Set the appropriate digital I/O pin connections. These are the pin
// assignments for the Arduino as well for as the DS1302 chip.
const int CePin   = 5;  // Chip Enable
const int IoPin   = 6;  // Input/Output
const int SclkPin = 7;  // Serial Clock

// Create a DS1302 object.
DS1302 rtc(CePin, IoPin, SclkPin);
String dayAsString(const Time::Day day) {
  switch (day) {
    case Time::kSunday: return "Sunday";
    case Time::kMonday: return "Monday";
    case Time::kTuesday: return "Tuesday";
    case Time::kWednesday: return "Wednesday";
    case Time::kThursday: return "Thursday";
    case Time::kFriday: return "Friday";
    case Time::kSaturday: return "Saturday";
  return "(unknown day)";
void printTime() {
  // Get the current time and date from the chip.
  Time t = rtc.time();
  // Name the day of the week.
  const String day = dayAsString(;
  // Format the time and date and insert into the temporary buffer.
  char buf[50];
  snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
           t.yr, t.mon,,
 , t.min, t.sec);
 // Print the formatted string to serial so we can see the time.

}  // namespace
void setup() {
 // Initialize a new chip by turning off write protection and clearing the
  // clock halt flag. These methods needn't always be called. See the DS1302
  // datasheet for details.

 //  rtc.writeProtect(false);

  // rtc.halt(false);
  // Make a new time object to set the date and time.
 // This need to be done first time to set the Date and time.
  // Thursday, July 6, 2017 at 22:58:50.
  // Time t(2017, 07, 6, 22, 58, 50, Time::kThursday);
  // Set the time and date on the chip.
  // rtc.time(t);
// Loop and print the time every second.
void loop() {

Note: Download the library from

DS1302 Datasheet:


In the next Post I will modify this to make Alarm clock using LCD display.

How to test Arduino UNO


You have received your Arduino UNO and want to quickly verify and test arduino uno that it is in good condition or you want to write your first Arduino UNO program. To do so you need to follow the following steps:

  • Launch the Arduino IDE. If you have not installed, you can download from following site and install it.

  • Connect the Arduino to USB port.
  • Write the following code to blink the on board LEDtest arduino uno 1
  • Compile and upload the code.
  • The on board LED should start blinking every second.