Translate

Digital Clock in Arduino Serial Monitor

 Introduction

In this project, we will design a digital clock using Arduino that works in the Serial Monitor.
Unlike simple timer programs, this clock will start only after the user enters the present time manually in AM/PM format.

This project is very useful for beginners because it helps to understand:

  • Serial communication

  • Time calculation using millis()

  • String input handling

  • AM/PM logic

  • Digital clock formatting

The user must enter the time in this format:

HH:MM:SS AM or HH:MM:SS PM

Example:

10:30:00 PM

After entering the time, the clock starts running like a real digital clock.


Components Required


Arduino UNO (or any Arduino board)
  • USB cable

  • Arduino IDE 

  • Serial Monitor

(No other external hardware is required.)


Working Principle

  1. When the program starts, Arduino waits for the user to enter the current time.

  2. The clock does not run until valid time input is received.

  3. After setting the time, Arduino counts seconds using the millis() function.

  4. Every second, it updates:

    • Seconds

    • Minutes

    • Hours

    • AM/PM

  5. The updated time is displayed in the Serial Monitor continuously.


Time Input Format

The user must enter time in the following format:

HH:MM:SS AM HH:MM:SS PM

Examples:

08:45:00 AM 11:20:30 PM

Arduino Program Code

int hours = 0; int minutes = 0; int seconds = 0; bool isPM = false; bool timeSet = false; // clock will start only after time is entered unsigned long previousMillis = 0; void setup() { Serial.begin(9600); Serial.println("Enter time in format: HH:MM:SS AM or PM"); Serial.println("Example: 10:25:30 PM"); } void loop() { // Wait for user to enter time if (!timeSet && Serial.available()) { String input = Serial.readStringUntil('\n'); input.trim(); int h, m, s; char ampm[3]; if (sscanf(input.c_str(), "%d:%d:%d %s", &h, &m, &s, ampm) == 4) { hours = h; minutes = m; seconds = s; if (ampm[0] == 'P' || ampm[0] == 'p') isPM = true; else isPM = false; timeSet = true; // start clock previousMillis = millis(); Serial.println("Time Set! Clock Started..."); } else { Serial.println("Wrong format! Use: HH:MM:SS AM/PM"); } } // Run clock ONLY after time is set if (timeSet) { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= 1000) { previousMillis = currentMillis; seconds++; if (seconds == 60) { seconds = 0; minutes++; } if (minutes == 60) { minutes = 0; hours++; } if (hours == 12) { isPM = !isPM; // toggle AM/PM } if (hours == 13) { hours = 1; } // Print time if (hours < 10) Serial.print("0"); Serial.print(hours); Serial.print(":"); if (minutes < 10) Serial.print("0"); Serial.print(minutes); Serial.print(":"); if (seconds < 10) Serial.print("0"); Serial.print(seconds); Serial.print(isPM ? " PM" : " AM"); Serial.println(); } } }

How to Use

  1. Upload the program to Arduino

  2. Open Serial Monitor

  3. Set:

    • Baud Rate = 9600

    • Line Ending = Newline

  4. Enter time like:

    09:30:00 AM
  5. Press Enter

  6. The digital clock will start running


Sample Output

09:30:01 AM 09:30:02 AM 09:30:03 AM 09:30:04 AM

Detailed Program Explanation

This program uses variables to store hours, minutes, seconds, and AM/PM status. A flag variable (timeSet) ensures the clock does not start until the user enters time. The millis() function is used to create a 1-second interval without using the delay() function.

The program reads the input time from the Serial Monitor, separates hours, minutes, seconds, and AM/PM using sscanf(), and stores them in variables. Once time is set, the clock increments seconds every second. When seconds reach 60, minutes increase. When minutes reach 60, hours increase. When hours reach 12, AM/PM toggles. If hours reach 13, they are reset to 1 to maintain 12-hour format.

Finally, the time is printed in HH:MM:SS AM/PM format with leading zeros.


Advantages

  • No RTC module required

  • Works in Tinkercad

  • Easy to understand

  • Suitable for beginners

  • Supports AM/PM format


Applications

  • Learning Arduino timing

  • Digital clock project

  • Serial communication practice

  • Base code for alarm clock

  • Base code for LCD clock


Conclusion

This project demonstrates how to build a digital clock using Arduino Serial Monitor that starts only after entering the present time in AM/PM format. It is an excellent beginner project to understand time logic and serial input handling. The same program can be extended to include LCD display, alarm feature, or RTC module for real-time accuracy.

തുടക്കക്കാർക്കായി ഇലക്ട്രോണിക്സ് ലളിതമായി പഠിക്കാം.

Empowering students in Kerala with hands-on technical skills.