How to make digital clock using Arduino and DS1302

To make a digital clock using Arduino and DS1302 we need following materials.

  1. Arduino UNO ( you can use any arduino)
  2. 16*2 LCD display
  3. DS1302 module/chip
  4. 1.2 k resistance
  5. Variable resistance 10k Pot

Make the connections as mentioned below:

Connecting the LCD:

  • VSS –> GND Arduino
  • VDP –> 5V Arduino
  • VO –> output potentiometer (potentiometer VCC -> 5V Arduino, potentiometer GND -> Arduino GND).
  • RS –> pin 12 Arduino
  • RW –> GND Arduino
  • E –> pin 11 Arduino
  • D4 –> pin 5 Arduino
  • D5 –> pin 4 Arduino
  • D6 –> pin 3 Arduino
  • D7 –> pin 2 Arduino
  • A –> 5V Arduino with 1.2 k resistor
  • K –> GND Arduino

Refer my other post on How to connect 16*2 LCD display Arduino UNO  for more details

ConnectingDS1302:

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.

If you are using DS1302 module, connect the pins as mentioned below:

  • Arduino pin 6 – CE / Chip Enable pin
  • Arduino pin 7 – Input/Output pin
  • Arduino pin 8 – SclkPin/ Serial Clock

Refer my post on DS1302 for more details.



CODE:
Upload the following code to the Arduino board.

// DIGITAL CLOCK USING ARDUINO AND DS1302

#include <stdio.h>
#include <DS1302.h>
#include<LiquidCrystal.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 = 6; // Chip Enable
const int IoPin = 7; // Input/Output
const int SclkPin = 8; // Serial Clock

//LCD display pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// 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(t.day);

// Format the time and date and insert into the temporary buffer.
 
 char buf_dt[50];
 char buf_tm[50];
 //format date
 snprintf(buf_dt, sizeof(buf_dt), "%s %04d-%02d-%02d ",
 day.c_str(),
 t.yr, t.mon, t.date );
//Format time
snprintf(buf_tm, sizeof(buf_tm), "%02d:%02d:%02d",
 t.hr, t.min, t.sec);
// Print the formatted string to serial so we can see the time.
// Serial.println(buf_tm);
//Print the formated string on the LCD.
 lcd.display();
 lcd.setCursor(0, 1);
 lcd.begin(16, 2);
 lcd.print(buf_dt);
 lcd.setCursor(0, 1);
 lcd.print("Time: ");
 lcd.setCursor(7, 1);
 lcd.print(buf_tm);
}

} // 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. 
 // This need to be done first time to set the Date and time. 
 // Thursday, July 6, 2017 at 22:58:50.
// Time t(2018, 01, 7, 16, 25, 50, Time::kSunday);
 // Set the time and date on the chip.
// rtc.time(t);
 // Set Default Display at startup
 lcd.begin(16, 2);
 lcd.print("Digitalab.org");
}

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

Below is the screenshot of the completed project “Digital clock using Arduino and DS1302”. You can assemble it in a box and use it as a regular watch.

3 Comments

  1. pls help i dnt know how to change its time bcz script is soo complicated plss help. i was working on it from 3 hours and i cant find out plsss help

    • You need to first un comment the line where option to set date/time is there and update the chip current date/time. After that comment it back.

Leave a Reply

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