Setting up DS1302 RTC from Geekcreit kit with Arduino

As part of a Geekcreit sensor kit which I recently got from banggood there was this RTC aka Real Time Clock module. Out of the list of 45 sensors I picked this one as my first to get it working with Arduino and Orange Pi Zero. The Real Time Clock module is used in project where you need to know the date and the time. Now there are many methods to fetch time to a system. Some of the common methods depending on the location of the project are Network Time Server, GPS and if you are in Germany you can also use DCF77. In these methods it depends on wireless or Internet communication and depending on your project it might be an unreliable source of time keeping. So here comes the RTC, where there is a small chip which is always run by a coin cell battery to maintain the time. If you ever open your PC or laptop you can see BIOS battery which does keep your RTC running and that is how you get the right date and time even after you shutdown.

I bought the following kit which contains 45 sensors which can be used with Arduino and Orange Pi.

https://www.banggood.com/Geekcreit-45-In-1-Sensor-Module-Board-Kit-Upgrade-Version-For-Arduino-p-1137050.html?p=W214159476515201703B

The RTC Module is using the chip DS1302 from Dallas. In the first glance its deceivingly looking like a chip which supports  I2C  and then later after reading the datasheet it was clear that it does not use I2C instead it uses a simple serial interface.The DS1302  provides seconds, minutes, hours, day, date, month, and year information. The end of the month date is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24 hour or 12 hour format with an AM/PM indicator. The chip pinout looks like this

 

 

 

 

 

The Vcc2 and GND are the power supply to the chip, X1 and X2 are connected to oscillator, Vcc1 and GND is used by 3V CR2032 coin cell battery and the pins CLK, IO and CE are part of the simple serial interface or otherwise called 3-wire interface. Basically its much similar to SPI just that MISO and MOSI are multiplexed in to a single IO line.

Arduino Connection:

I used Arduino Leonardo for this connection. I used Pin 6, 7 and 8 for CLK , IO and CE.

 

 

 

 

 

Arduino  DS1302 Library:

There are plenty of working code available but I found this one to be easy to understand and use as well https://github.com/msparks/arduino-ds1302.

You can download the latest zip file from here https://github.com/msparks/arduino-ds1302/archive/master.zip and then open your Arduino IDE and navigate to Sketch->Include library->ADD .Zip library and then point to the downloaded master.zip file.

 
// Example sketch for interfacing with the DS1302 timekeeping chip.
//
// Copyright (c) 2009, Matt Sparks
// All rights reserved.

#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. See the DS1302
// datasheet:
//
//   http://datasheets.maximintegrated.com/en/ds/DS1302.pdf
const int kCePin   = 8;  // Chip Enable
const int kIoPin   = 7;  // Input/Output
const int kSclkPin = 6;  // Serial Clock

// Create a DS1302 object.
DS1302 rtc(kCePin, kIoPin, kSclkPin);

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(t.day);

  // 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",
           day.c_str(),
           t.yr, t.mon, t.date,
           t.hr, t.min, t.sec);

  // Print the formatted string to serial so we can see the time.
  Serial.println(buf);
}

}  // namespace

void setup() {
  Serial.begin(9600);

  // 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.
  // Sunday, September 22, 2013 at 01:38:50.
//  Time t(2013, 9, 22, 1, 38, 50, Time::kSunday);

  // Set the time and date on the chip.
//  rtc.time(t);
}

// Loop and print the time every second.
void loop() {
  printTime();
  delay(1000);
}

The library has implemented all the required stuff to get the time and all we need to do is just configure the pins properly. The Chip enable or CE corresponds to the RST of the RTC breakout board. The IO Pin refers to DAT pin of the breakout, and SCLK refers to CLK.

const int kCePin   = 8;  // Chip Enable
const int kIoPin   = 7;  // Input/Output
const int kSclkPin = 6;  // Serial Clock

The most important thing about the RTC modules is that you need to set
the time before using it. The Below code is commented out and it should be
only used when you want to set the time.

// Make a new time object to set the date and time.
// Sunday, September 22, 2013 at 01:38:50.
//  Time t(2013, 9, 22, 1, 38, 50, Time::kSunday);

// Set the time and date on the chip.
//  rtc.time(t);


You can use this module for making project such as alarm clock, stop watch, pomodoro timer, scheduled timer for task like watering plant and many more.

One thought on “Setting up DS1302 RTC from Geekcreit kit with Arduino

Leave a Reply

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