How to make 12 hour format clock (DS1302) with Arduino

Arduino Tutorial 31# How to make 12 hour format clock (DS1302) with Arduino

Posted Leave a commentPosted in Arduino

I am getting lots of request for 12 hour format clock. So, in this tutorial we will see how to make 12 hour format clock.  So let’s get started.

For this you will need

  1. Arduino,
  2. Real time clock (DS1302),
  3. LCD (16X2),
  4. I2C,
  5. Breadboard,
  6. Jumper wires.

I am using I2C for LCD if you don’t know how to use LCD with I2C click here. Do connection as shown in diagram.

Cicuit for DS1302 12 hour format clock
Cicuit for DS1302 12 hour format clock

The main problem is DS1302 library is in 24-hour format but we can make it 12-hour format by just subtracting 12 hour from 24 hours and also we have to modify it further to get time in proper AM/PM format.

 

12hour format clock DS1302 sketch
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x3F for a 16 chars and 2 line display // note: it may be different for your LCD please find it.
LiquidCrystal_I2C lcd(0x3F, 16, 2);
#include <DS1302.h>

// Init the DS1302
DS1302 rtc(2, 3, 4);

// Init a Time-data structure
Time t;

void setup()
{

// Set the clock to run-mode, and disable the write protection
 rtc.halt(false);
 rtc.writeProtect(false);
 
 // The following lines can be commented out to use the values already stored in the DS1302
 rtc.setDOW(TUESDAY); // Set Day-of-Week to TUESDAY
 rtc.setTime(23, 59, 50); // Set the time to 24hr format it will automatically take time according to AM/PM
 rtc.setDate(06, 2, 2018); // Set the date to Febrauray 6th, 2018
 // initialize the LCD
 lcd.begin();
}

void loop()
{
 // Get data from the DS1302
 t = rtc.getTime();
 
 lcd.setCursor(0, 0);
 if (t.date<=9){
 lcd.setCursor(0,0);
 lcd.print("0");
 lcd.setCursor(1,0);
 lcd.print(t.date, DEC);
 }
 else {
 lcd.print(t.date, DEC);}
 
 lcd.setCursor(3, 0);
 lcd.print(rtc.getMonthStr());
 lcd.setCursor(12,0);
 lcd.print(t.year, DEC);
 lcd.setCursor(4,1);
 
 {if (t.hour>=12){
 lcd.setCursor(13,1);
 lcd.print("PM");
 lcd.setCursor(4,1);
 t.hour = t.hour-12;
 if (t.hour== 0) { 
 t.hour = 12; // Day 12 PM
 }
 if (t.hour<=9){
 lcd.setCursor(4,1);
 lcd.print("0");
 lcd.setCursor(5,1);
 lcd.print(t.hour, DEC);
 }
 else {
 lcd.print(t.hour, DEC);}}
 else { if(t.hour==0){
 t.hour=12; // Night 12 AM
 }
 lcd.setCursor(13,1);
 lcd.print("AM");
 lcd.setCursor(4,1);
 if (t.hour<=9){
 lcd.setCursor(4,1);
 lcd.print("0");
 lcd.setCursor(5,1);
 lcd.print(t.hour, DEC);
 }
 else {
 lcd.print(t.hour, DEC);}} }
 lcd.setCursor(6,1);
 lcd.print(":");
 lcd.setCursor(7,1);
 if (t.min<=9){
 lcd.setCursor(7,1);
 lcd.print("0");
 lcd.setCursor(8,1);
 lcd.print(t.min, DEC);
 }
 else {
 lcd.print(t.min, DEC);}

lcd.setCursor(9,1);
 lcd.print(":");
 lcd.setCursor(10,1);
 if (t.sec<=9){
 lcd.setCursor(10,1);
 lcd.print("0");
 lcd.setCursor(11,1);
 lcd.print(t.sec, DEC);
 }
 else {
 lcd.print(t.sec, DEC);}

delay (1000);
 lcd.clear();
}

First, add I2c and DS1302 library.  Set time in 24-hour format it will take time automatically in AM/PM format.

    if (t.date<=9){

      lcd.setCursor(0,0);

      lcd.print("0");

      lcd.setCursor(1,0);

      lcd.print(t.date, DEC);

    }

    else {

  lcd.print(t.date, DEC);}

I have use this code for date, hour, minute and second. What if we don’t use this?

This happen when we dont use that code

You can see it is showing correct time but it is single digit, which is not looking like time. For this, we have added this piece of code so that it will show two digit if it is single digit. If time is greater than 12 hours means it is PM, and it will subtract 12 hours from 24 hours but there is a problem. It becomes zero at 12 o’clock so we have given “if condition” that will make it show 12 o’clock.

    t.hour = t.hour-12;

    if (t.hour== 0) {

     t.hour = 12;       // Day 12 PM

    }

If hours is less than 12 hours, it will be AM. But it will show 0 hours at nights 12 o’clock.

{ if(t.hour==0){

             t.hour=12;    // Night 12 AM

    }

We have added this code so that it will show 12 o’clock in AM.

Upload the sketch, now you can see time from PM to AM. It is working fine, we can also see it is changing time from 12 o’clock to 1 o’clock.

LIST OF COMPONENT BUY ONLINE:

http://amzn.to/2fvSRJq   (Arduino)

http://amzn.to/2gRFSiv (RTC DS1302)

http://amzn.to/2yBk7eT (LCD display)

http://amzn.to/2zg8aeX (I2C)

http://amzn.to/2wxPmWz  (Breadboard)

http://amzn.to/2vJ3lvo   (Jumper wire)

TILL THEN KEEP LEARNING KEEP MAKING 🙂