4-Digit 7-Segment Clock Display Using Arduino Uno in Wokwi (Complete Guide with Code)

 


Introduction

In this project, we will build a 4-Digit 7-Segment Digital Clock using Arduino Uno in the Wokwi simulator. This Arduino digital clock displays time in HH:MM format using a multiplexed 7-segment display.

This is a perfect beginner-to-intermediate level project to understand:

  • Multiplexing technique

  • 7-segment display interfacing

  • Real-time clock logic

  • Arduino timing functions

Diagram.json:
{
  "version": 1,
  "author": "4-Digit Clock Display",
  "editor": "wokwi",
  "parts": [
    {
      "type": "wokwi-arduino-uno",
      "id": "uno",
      "top": 0,
      "left": 0,
      "attrs": {}
    },
    {
      "type": "wokwi-tm1637-7segment",
      "id": "tm1637",
      "top": -96,
      "left": 144,
      "attrs": {
        "color": "red"
      }
    },
    {
      "type": "wokwi-pushbutton",
      "id": "btn_set",
      "top": 124.8,
      "left": -124.8,
      "attrs": {
        "color": "green",
        "label": "SET"
      }
    },
    {
      "type": "wokwi-pushbutton",
      "id": "btn_hour",
      "top": 124.8,
      "left": -28.8,
      "attrs": {
        "color": "blue",
        "label": "HOUR"
      }
    },
    {
      "type": "wokwi-pushbutton",
      "id": "btn_min",
      "top": 124.8,
      "left": 67.2,
      "attrs": {
        "color": "yellow",
        "label": "MIN"
      }
    },
    {
      "type": "wokwi-resistor",
      "id": "r1",
      "top": 220.15,
      "left": -124.8,
      "rotate": 90,
      "attrs": {
        "value": "10000"
      }
    },
    {
      "type": "wokwi-resistor",
      "id": "r2",
      "top": 220.15,
      "left": -28.8,
      "rotate": 90,
      "attrs": {
        "value": "10000"
      }
    },
    {
      "type": "wokwi-resistor",
      "id": "r3",
      "top": 220.15,
      "left": 67.2,
      "rotate": 90,
      "attrs": {
        "value": "10000"
      }
    }
  ],
  "connections": [
    [
      "tm1637:CLK",
      "uno:2",
      "blue",
      [
        "v0"
      ]
    ],
    [
      "tm1637:DIO",
      "uno:3",
      "green",
      [
        "v0"
      ]
    ],
    [
      "tm1637:VCC",
      "uno:5V",
      "red",
      [
        "v0"
      ]
    ],
    [
      "tm1637:GND",
      "uno:GND.1",
      "black",
      [
        "v0"
      ]
    ],
    [
      "btn_set:1.l",
      "uno:6",
      "green",
      [
        "v0"
      ]
    ],
    [
      "btn_set:2.l",
      "r1:1",
      "black",
      [
        "v0"
      ]
    ],
    [
      "r1:2",
      "uno:GND.2",
      "black",
      [
        "v0"
      ]
    ],
    [
      "btn_hour:1.l",
      "uno:4",
      "blue",
      [
        "v0"
      ]
    ],
    [
      "btn_hour:2.l",
      "r2:1",
      "black",
      [
        "v0"
      ]
    ],
    [
      "r2:2",
      "uno:GND.2",
      "black",
      [
        "v0"
      ]
    ],
    [
      "btn_min:1.l",
      "uno:5",
      "yellow",
      [
        "v0"
      ]
    ],
    [
      "btn_min:2.l",
      "r3:1",
      "black",
      [
        "v0"
      ]
    ],
    [
      "r3:2",
      "uno:GND.2",
      "black",
      [
        "v0"
      ]
    ]
  ],
  "dependencies": {}
}


You can simulate everything online in Wokwi without physical hardware.

Code:

/*
 * 4-Digit 7-Segment Clock Display
 * Displays time in HH:MM format using TM1637 display module
 * Features: Clock display, adjustable time, blinking colon
 */

#include <TM1637Display.h>

// Pin definitions for TM1637
#define CLK_PIN 2    // Clock pin
#define DIO_PIN 3    // Data pin

// Button pins for setting time
#define HOUR_BTN 4   // Button to increment hours
#define MIN_BTN 5    // Button to increment minutes
#define SET_BTN 6    // Button to enter/exit set mode

// Create display object
TM1637Display display(CLK_PIN, DIO_PIN);

// Time variables
int hours = 12;
int minutes = 0;
int seconds = 0;

// State variables
bool setMode = false;           // Time setting mode flag
bool colonBlink = true;         // Colon blinking state
unsigned long lastUpdate = 0;   // Last time update timestamp
unsigned long lastBlink = 0;    // Last colon blink timestamp

// Button debouncing
unsigned long lastHourPress = 0;
unsigned long lastMinPress = 0;
unsigned long lastSetPress = 0;
const int debounceDelay = 200;

// Segment data for digits with colon
const uint8_t SEG_COLON = 0b01000000;  // Colon segment

void setup() {
  Serial.begin(9600);
 
  // Initialize button pins
  pinMode(HOUR_BTN, INPUT_PULLUP);
  pinMode(MIN_BTN, INPUT_PULLUP);
  pinMode(SET_BTN, INPUT_PULLUP);
 
  // Initialize display
  display.setBrightness(0x0f);  // Maximum brightness
 
  // Display startup message
  uint8_t data[] = {0x00, 0x00, 0x00, 0x00};
  display.setSegments(data);
  delay(500);
 
  Serial.println("4-Digit 7-Segment Clock Initialized");
  Serial.println("Use buttons to set time:");
  Serial.println("- SET button: Enter/Exit set mode");
  Serial.println("- HOUR button: Increment hours");
  Serial.println("- MIN button: Increment minutes");
}

void loop() {
  unsigned long currentMillis = millis();
 
  // Check button presses
  checkButtons();
 
  // Update seconds and time (only when not in set mode)
  if (!setMode && currentMillis - lastUpdate >= 1000) {
    lastUpdate = currentMillis;
    updateTime();
  }
 
  // Blink colon every 500ms
  if (currentMillis - lastBlink >= 500) {
    lastBlink = currentMillis;
    colonBlink = !colonBlink;
  }
 
  // Display time
  displayTime();
 
  delay(10);  // Small delay for stability
}

void updateTime() {
  seconds++;
 
  if (seconds >= 60) {
    seconds = 0;
    minutes++;
   
    if (minutes >= 60) {
      minutes = 0;
      hours++;
     
      if (hours >= 24) {
        hours = 0;
      }
    }
  }
 
  // Print to Serial Monitor
  Serial.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.println(seconds);
}

void displayTime() {
  // Create time display value
  int displayValue = hours * 100 + minutes;
 
  // Show time with or without colon based on blink state
  if (setMode) {
    // In set mode, always show colon
    display.showNumberDecEx(displayValue, 0b01000000, true);
  } else {
    // Normal mode, blink colon
    if (colonBlink) {
      display.showNumberDecEx(displayValue, 0b01000000, true);
    } else {
      display.showNumberDecEx(displayValue, 0b00000000, true);
    }
  }
}

void checkButtons() {
  unsigned long currentMillis = millis();
 
  // SET button - toggle set mode
  if (digitalRead(SET_BTN) == LOW && currentMillis - lastSetPress > debounceDelay) {
    lastSetPress = currentMillis;
    setMode = !setMode;
   
    if (setMode) {
      Serial.println("*** SET MODE ENABLED ***");
      Serial.println("Use HOUR and MIN buttons to adjust time");
    } else {
      Serial.println("*** SET MODE DISABLED ***");
      Serial.println("Time saved. Clock running.");
      seconds = 0;  // Reset seconds when exiting set mode
      lastUpdate = currentMillis;  // Reset update timer
    }
   
    delay(200);  // Additional debounce
  }
 
  // HOUR button - increment hours (only in set mode)
  if (setMode && digitalRead(HOUR_BTN) == LOW && currentMillis - lastHourPress > debounceDelay) {
    lastHourPress = currentMillis;
    hours++;
    if (hours >= 24) {
      hours = 0;
    }
    Serial.print("Hours set to: ");
    Serial.println(hours);
  }
 
  // MIN button - increment minutes (only in set mode)
  if (setMode && digitalRead(MIN_BTN) == LOW && currentMillis - lastMinPress > debounceDelay) {
    lastMinPress = currentMillis;
    minutes++;
    if (minutes >= 60) {
      minutes = 0;
    }
    Serial.print("Minutes set to: ");
    Serial.println(minutes);
  }
}



Components Required (Wokwi Simulation)

  • Arduino Uno

  • 4-Digit 7-Segment Display (Common Cathode)

  • 220Ω Resistors (8)

  • Jumper Wires


Circuit Connections:

Segment Pins (a–g, dp)

Connect segment pins to Arduino digital pins:

SegmentArduino Pin
a2
b3
c4
d5
e6
f7
g8

Digit Control Pins

DigitArduino Pin
D19
D210
D311
D412

(Use resistors for segment pins)

How the Arduino Digital Clock Works

  1. The code uses the millis() function to track time.

  2. Every 60,000 milliseconds (1 minute), minutes increase.

  3. When minutes reach 60, hours increase.

  4. Multiplexing activates one digit at a time very quickly.

  5. Human eyes see all digits glowing continuously.


Applications of 4-Digit 7-Segment Clock

  • Digital wall clock

  • School electronics project

  • Embedded systems learning

  • IoT display modules

  • Arduino beginner project


Why Use Wokwi Simulator?

  • No hardware required

  • Instant debugging

  • Easy circuit wiring

  • Perfect for students and beginners


FAQ

Q1: Can I add seconds to this Arduino clock?

Yes, you can modify the code to update every 1000 milliseconds and display seconds.

Q2: Can I use RTC module like DS3231?

Yes, for accurate real-time clock, you can connect an RTC module.

Q3: Is this project suitable for beginners?

Yes, this is an excellent intermediate beginner Arduino project.


Conclusion

This 4-Digit 7-Segment Clock using Arduino Uno in Wokwi is a great project to understand multiplexing and time control using millis(). It is ideal for school exhibitions, robotics students, and embedded systems beginners.


Comments