Arduino Mega - 74HC595 4-Digit 7-Segment Display

Got an Arduino Mega and want to drive a 4-digit 7-segment display? The 74HC595-based display module is your quickest path from wiring to a working readout - and with the DIYables_4Digit7Segment_74HC595 library you can start writing code in minutes.

This step-by-step walkthrough covers:

The Mega brings extra I/O, more SRAM, and additional hardware serial ports - handy when your project needs to talk to sensors alongside the display.

Arduino Mega 74HC595 4-Digit 7-Segment Display

Hardware Preparation

1×Arduino Mega
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×74HC595 4-digit 7-segment Display
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Arduino Uno/Mega
1×Recommended: Sensors/Servo Expansion Shield for Arduino Uno/Mega
1×Recommended: Breadboard Shield for Arduino Mega
1×Recommended: Enclosure for Arduino Mega

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Understanding the 74HC595 4-Digit 7-Segment Display

This display module combines four 7-segment digits with a 74HC595 shift register, letting you control all four digits and their decimal points through just 3 wires. The shift register takes serial data and converts it to parallel output for the segments.

The library handles the multiplexing (rapidly switching between digits) so you only need to call print() with the value you want to show.

Function Pin Description
SCLK (SH_CP) Serial Clock Clocks data into the shift register
RCLK (ST_CP) Register Clock Latches data to the output
DIO (DS) Data Input Serial data line
VCC Power 3.3V or 5V
GND Ground Common ground

Wiring Diagram

Hook up the display module to the Mega:

  • SCLK to pin 4
  • RCLK to pin 3
  • DIO to pin 2
  • VCC to 5V OR 3.3V
  • GND to GND
The wiring diagram between Arduino Mega 74HC595 4-Digit 7-Segment Display

This image is created using Fritzing. Click to enlarge image

Installing the Library

  1. Connect your Arduino Mega to the computer via a USB cable.
  2. Open the Arduino IDE. Pick Arduino Mega or Mega 2560 as the board and choose the correct port.
  3. Go to the Libraries panel on the left side.
  4. Search for "DIYables_4Digit7Segment_74HC595". Find the entry from DIYables.
  5. Press Install. Select version 2.0.0 or later.
Arduino 74HC595 4-Digit 7-Segment Display library

The library has zero external dependencies.

Code Skeleton

The bare minimum to get started:

#include <DIYables_4Digit7Segment_74HC595.h> #define SCLK_PIN 4 #define RCLK_PIN 3 #define DIO_PIN 2 DIYables_4Digit7Segment_74HC595 display(SCLK_PIN, RCLK_PIN, DIO_PIN); void setup() { display.begin(); display.print(1234); } void loop() { display.loop(); }

The begin() call sets up pin modes. After that, set the display value with print() and keep calling display.loop() so the multiplexing runs. If your code includes long waits, swap delay() for display.delay() to avoid blanking the display.

Example - Display Integers

Cycles through a list of integers, then shows a zero-padded number.

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-74hc595-4-digit-7-segment-display */ #include <DIYables_4Digit7Segment_74HC595.h> // Pin configuration - change these to match your wiring #define SCLK_PIN 4 // The Arduino Mega pin connected to the Serial clock pin of 7-segment display #define RCLK_PIN 3 // The Arduino Mega pin connected to the Register clock / latch pin of 7-segment display #define DIO_PIN 2 // The Arduino Mega pin connected to the Data (DS) pin of 7-segment display DIYables_4Digit7Segment_74HC595 display(SCLK_PIN, RCLK_PIN, DIO_PIN); int numbers[] = {0, 42, 1234, -5, -123, 9999}; int numCount = 6; int currentIndex = 0; bool showZeroPad = false; unsigned long lastChange = 0; void setup() { Serial.begin(9600); display.begin(); Serial.println("4-Digit 7-Segment 74HC595 - Integer Example"); } void loop() { display.loop(); // Must be called frequently to refresh the display if (millis() - lastChange >= 2000) { lastChange = millis(); if (!showZeroPad) { display.print(numbers[currentIndex]); Serial.print("Displaying: "); Serial.println(numbers[currentIndex]); currentIndex++; if (currentIndex >= numCount) { currentIndex = 0; showZeroPad = true; } } else { display.print(42, true); // Shows "0042" Serial.println("Displaying: 0042 (zero-padded)"); showZeroPad = false; } } }

Steps to Upload

  • Wire the display to the Mega as described above.
  • Plug in the USB cable.
  • Paste the code into Arduino IDE, select the board and port, and hit Upload.
  • Open the Serial Monitor (9600 baud) to watch the output.

The display runs through 0, 42, 1234, -5, -123, 9999, then 0042 with zero-padding.

API at a Glance

Method Effect Sample Call
print(int) Show an integer display.print(1234)
print(int, true) Show integer with leading zeros display.print(42, true)
loop() Keep the display refreshing display.loop()

Example - Display Floats

Shows floating-point numbers with auto and fixed decimal places.

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-74hc595-4-digit-7-segment-display */ #include <DIYables_4Digit7Segment_74HC595.h> // Pin configuration - change these to match your wiring #define SCLK_PIN 4 // The Arduino Mega pin connected to the Serial clock pin of 7-segment display #define RCLK_PIN 3 // The Arduino Mega pin connected to the Register clock / latch pin of 7-segment display #define DIO_PIN 2 // The Arduino Mega pin connected to the Data (DS) pin of 7-segment display DIYables_4Digit7Segment_74HC595 display(SCLK_PIN, RCLK_PIN, DIO_PIN); void setup() { Serial.begin(9600); display.begin(); Serial.println("4-Digit 7-Segment 74HC595 - Float Example"); } void loop() { // Auto decimal placement display.print(1.5); // Shows " 1.5" Serial.println("Auto decimal: 1.5"); display.delay(2000); display.print(12.34); // Shows "12.34" Serial.println("Auto decimal: 12.34"); display.delay(2000); display.print(3.141); // Shows "3.141" Serial.println("Auto decimal: 3.141"); display.delay(2000); display.print(-1.2); // Shows "-1.20" Serial.println("Auto decimal: -1.20"); display.delay(2000); display.print(0.5); // Shows " 0.5" Serial.println("Auto decimal: 0.5"); display.delay(2000); // Fixed decimal places display.print(23.5, 1); // 1 decimal place: shows "23.5" Serial.println("1 decimal place: 23.5"); display.delay(2000); display.print(1.5, 2); // 2 decimal places: shows "1.50" Serial.println("2 decimal places: 1.50"); display.delay(2000); // Zero-padded display.print(1.5, 2, true); // Shows "01.50" Serial.println("2 decimal places, zero-padded: 01.50"); display.delay(2000); }

Steps to Upload

  • Paste the code into Arduino IDE, select the board and port, and hit Upload.
  • Open the Serial Monitor to watch the output.

Various floats appear with auto decimals, then with 1 and 2 fixed places, and finally with zero-padding.

Example - Text and Temperature

Displays text strings, degree symbols, and temperature readings.

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-74hc595-4-digit-7-segment-display */ #include <DIYables_4Digit7Segment_74HC595.h> // Pin configuration - change these to match your wiring #define SCLK_PIN 4 // The Arduino Mega pin connected to the Serial clock pin of 7-segment display #define RCLK_PIN 3 // The Arduino Mega pin connected to the Register clock / latch pin of 7-segment display #define DIO_PIN 2 // The Arduino Mega pin connected to the Data (DS) pin of 7-segment display DIYables_4Digit7Segment_74HC595 display(SCLK_PIN, RCLK_PIN, DIO_PIN); const char* texts[] = {"HELP", "Hi", "COOL", "done"}; int textCount = 4; int currentIndex = 0; int phase = 0; unsigned long lastChange = 0; void setup() { Serial.begin(9600); display.begin(); Serial.println("4-Digit 7-Segment 74HC595 - Text and Degree Example"); } void loop() { display.loop(); // Must be called frequently to refresh the display if (millis() - lastChange >= 2000) { lastChange = millis(); if (phase == 0) { // Display text strings display.print(texts[currentIndex]); Serial.print("Text: "); Serial.println(texts[currentIndex]); currentIndex++; if (currentIndex >= textCount) { currentIndex = 0; phase = 1; } } else if (phase == 1) { // Display temperature 25 degrees C display.printTemperature(25, 'C'); Serial.println("Temperature: 25 C"); phase = 2; } else if (phase == 2) { // Display temperature 72 degrees F display.printTemperature(72, 'F'); Serial.println("Temperature: 72 F"); phase = 3; } else if (phase == 3) { // Display degree symbol using string with DEGREE_CHAR constant char degStr[5]; degStr[0] = '2'; degStr[1] = '5'; degStr[2] = DEGREE_CHAR; degStr[3] = 'C'; degStr[4] = '\0'; display.print(degStr); Serial.println("String with degree: 25 deg C"); phase = 4; } else { // Display string with dots display.print("1.2.3.4"); Serial.println("Dots: 1.2.3.4"); phase = 0; } } }

Steps to Upload

  • Upload and open the Serial Monitor.

Shows text like "HELP" and "COOL", then temperatures (25 degrees C, 72 degrees F), then inline dots.

Example - Time Display

Shows HH.MM format with a blinking separator dot.

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-74hc595-4-digit-7-segment-display */ #include <DIYables_4Digit7Segment_74HC595.h> // Pin configuration - change these to match your wiring #define SCLK_PIN 4 // The Arduino Mega pin connected to the Serial clock pin of 7-segment display #define RCLK_PIN 3 // The Arduino Mega pin connected to the Register clock / latch pin of 7-segment display #define DIO_PIN 2 // The Arduino Mega pin connected to the Data (DS) pin of 7-segment display DIYables_4Digit7Segment_74HC595 display(SCLK_PIN, RCLK_PIN, DIO_PIN); int hours = 12; int minutes = 30; bool colonOn = true; unsigned long lastToggle = 0; void setup() { Serial.begin(9600); display.begin(); Serial.println("4-Digit 7-Segment 74HC595 - Time Example"); Serial.println("Displaying 12:30 with blinking dot separator"); } void loop() { display.loop(); // Must be called frequently to refresh the display if (millis() - lastToggle >= 500) { lastToggle = millis(); display.printTime(hours, minutes, colonOn); colonOn = !colonOn; // Toggle dot separator every 500ms for blinking effect } }

Steps to Upload

  • Upload and open the Serial Monitor.

Displays 12.30 with the dot toggling every half second.

Common Problems and Fixes

Symptom Likely Cause Fix
Nothing on display Wiring or pin mismatch Check VCC/GND and that SCLK, RCLK, DIO pins match your code
Wrong characters Common anode/cathode mismatch Use false as 4th constructor arg for common cathode
Flickering or blank delay() blocking refresh Replace delay() with display.delay()

Platform Support

Built entirely on Arduino standard APIs - compatible with every Arduino-supported architecture.

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!