Hello guys, 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 display 16X2,
  4. I2C,
  5. Breadboard,
  6. Jumper Wires.

I am using I2C for LCD if you don’t know you can learn here how to use LCD with I2C.

How to make 12 hour format clock.

Do connection as shown in diagram.

Download library for I2C and Real Time Clock library from Github  OR you can download it from here because github library may change by the time or it may not work.

Download l2C library Arduino-LiquidCrystal-I2C-library-master.zip

Download Real time clock library DS1302.zip

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.

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();
}
Explanation:

First, add I2c and DS1302 library.

#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>

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 used this code for date, hour, minute and second. What if we don’t use this? You can see it is showing correct time but it is single digit, which is not looking like time.

error in 12 format clock

For this, we have added thats 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

t.hour = t.hour-12;
if (t.hour== 0) {
t.hour = 12; // Day 12 PM
}

But there is a problem. It becomes zero at 12 o’clock so we have given “if condition” that will make it to show 12 o’clock. We have added this code so that it will show 12 o’clock in AM. 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
}

Upload the sketch, now you can see time from PM to AM.

12 Hour format clock

It is working fine, we can also see it is changing time from 12 o’clock to 1 o’clock.

Till then keep learning keep making.