Arduino - 74HC595 4-Digit 7-Segment Display

This tutorial instructs you how to use a 4-digit 7-segment display module with a 74HC595 shift register using Arduino. In detail, we will learn:

Arduino 74HC595 4-Digit 7-Segment Display

About 4-Digit 7-Segment Display with 74HC595

The 4-digit 7-segment display module with 74HC595 is a compact display that uses a shift register to reduce the number of pins needed from the microcontroller. Instead of requiring 12+ pins, this module only uses 3 data pins (SCLK, RCLK, DIO) to control all 4 digits and their decimal points.

Each digit consists of 7 LED segments (labeled A through G) plus a decimal point (DP). By turning on different combinations of segments, the display can show digits 0-9, several letters, and special characters.

The module supports both common anode and common cathode configurations. This tutorial uses the common anode version.

Pin Mapping

Function Pin Description
SCLK (SH_CP) Serial Clock Clock signal for shifting data
RCLK (ST_CP) Register Clock Latch signal to output shifted data
DIO (DS) Data Input Serial data input
VCC Power 3.3V or 5V
GND Ground Ground connection

Wiring Diagram

Connect the 74HC595 display module to Arduino as follows:

  • SCLK to Arduino pin 7
  • RCLK to Arduino pin 6
  • DIO to Arduino pin 5
  • VCC to 5V
  • GND to GND
Arduino 74HC595 4-Digit 7-Segment Display wiring diagram

This image is created using Fritzing. Click to enlarge image

Library Installation

  • Connect the Arduino board to your computer with a USB cable.
  • Open Arduino IDE, select the right board and port.
  • Navigate to the Libraries icon on the left bar of the Arduino IDE.
  • Search "DIYables_4Digit7Segment_74HC595", then find the DIYables_4Digit7Segment_74HC595 library by DIYables.
  • Click Install button to install the library. Select version 2.0.0 or later.
Arduino 74HC595 4-Digit 7-Segment Display library

Note: This library is self-contained with no external dependencies.

Basic Structure

Every sketch using the DIYables_4Digit7Segment_74HC595 library follows this basic structure:

#include <DIYables_4Digit7Segment_74HC595.h> #define SCLK_PIN 7 #define RCLK_PIN 6 #define DIO_PIN 5 DIYables_4Digit7Segment_74HC595 display(SCLK_PIN, RCLK_PIN, DIO_PIN); void setup() { display.begin(); display.print(1234); // Set what to display } void loop() { display.loop(); // Must be called frequently to refresh the display }

The display uses multiplexing to show all 4 digits. The display.loop() method handles the refresh cycle and must be called frequently in your loop() function. You set the content once with print(), and it stays on the display as long as loop() keeps running.

If your code has long delays, use display.delay(ms) instead of the standard delay(ms) to keep the display refreshed during the wait.

Arduino Code - Display Integers

This example shows how to display integer numbers on the 7-segment display, including negative numbers and zero-padding.

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-74hc595-4-digit-7-segment-display */ #include <DIYables_4Digit7Segment_74HC595.h> // Pin configuration - change these to match your wiring #define SCLK_PIN 7 // The Arduino pin connected to the Serial clock pin of 7-segment display #define RCLK_PIN 6 // The Arduino pin connected to the Register clock / latch pin of 7-segment display #define DIO_PIN 5 // The Arduino 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; } } }

Quick Steps

  • Wire the display module to Arduino as shown in the wiring diagram above.
  • Connect the Arduino board to your computer with a USB cable.
  • Open Arduino IDE, select the right board and port.
  • Copy the above code and paste it to the editor of Arduino IDE.
  • Click Upload button on Arduino IDE to upload code to Arduino.
  • Open the Serial Monitor to see the output.

The display cycles through several integers: 0, 42, 1234, -5, -123, 9999, and then shows 42 with zero-padding (0042). Each value is displayed for 2 seconds.

API Summary

Method Description Example
print(int) Display an integer (-999 to 9999) display.print(1234)
print(int, true) Display integer with leading zeros display.print(42, true) shows 0042
loop() Refresh the display (call in loop) display.loop()

Arduino Code - Display Floats

This example demonstrates displaying floating-point numbers with automatic and manual decimal places.

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-74hc595-4-digit-7-segment-display */ #include <DIYables_4Digit7Segment_74HC595.h> // Pin configuration - change these to match your wiring #define SCLK_PIN 7 // The Arduino pin connected to the Serial clock pin of 7-segment display #define RCLK_PIN 6 // The Arduino pin connected to the Register clock / latch pin of 7-segment display #define DIO_PIN 5 // The Arduino 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); }

Quick Steps

  • Wire the display module to Arduino as shown above.
  • Copy the above code and paste it to the editor of Arduino IDE.
  • Click Upload button on Arduino IDE to upload code to Arduino.
  • Open the Serial Monitor.

The display shows various float values. With auto decimal places, the library picks the best fit. You can also set a fixed number of decimal places and enable zero-padding.

API Summary

Method Description Example
print(double) Display float with auto decimals display.print(12.34)
print(double, dp) Display float with dp decimal places display.print(1.5, 2) shows 1.50
print(double, dp, true) Float with decimals and zero-pad display.print(1.5, 2, true)

Arduino Code - Display Text and Temperature

This example shows how to display text strings, the degree symbol, and temperature values.

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-74hc595-4-digit-7-segment-display */ #include <DIYables_4Digit7Segment_74HC595.h> // Pin configuration - change these to match your wiring #define SCLK_PIN 7 // The Arduino pin connected to the Serial clock pin of 7-segment display #define RCLK_PIN 6 // The Arduino pin connected to the Register clock / latch pin of 7-segment display #define DIO_PIN 5 // The Arduino 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; } } }

Quick Steps

  • Wire the display module to Arduino as shown above.
  • Copy the above code and paste it to the editor of Arduino IDE.
  • Click Upload button on Arduino IDE to upload code to Arduino.
  • Open the Serial Monitor.

The display shows text like "HELP", "Hi", "COOL", and "done", then temperature values 25 degrees C and 72 degrees F, then a string with the degree constant, and finally dots on every digit (1.2.3.4).

API Summary

Method Description Example
print(const char*) Display a text string (up to 4 chars) display.print("HELP")
printTemperature(int, char) Show temperature with degree and unit display.printTemperature(25, 'C')
DEGREE_CHAR Constant for the degree symbol display.print("25" + degStr)

Arduino Code - Display Time

This example displays a clock-style time in HH.MM format with a blinking dot separator.

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-74hc595-4-digit-7-segment-display */ #include <DIYables_4Digit7Segment_74HC595.h> // Pin configuration - change these to match your wiring #define SCLK_PIN 7 // The Arduino pin connected to the Serial clock pin of 7-segment display #define RCLK_PIN 6 // The Arduino pin connected to the Register clock / latch pin of 7-segment display #define DIO_PIN 5 // The Arduino 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 } }

Quick Steps

  • Wire the display module to Arduino as shown above.
  • Copy the above code and paste it to the editor of Arduino IDE.
  • Click Upload button on Arduino IDE to upload code to Arduino.
  • Open the Serial Monitor.

The display shows 12.30 with the dot between the second and third digit blinking every 500ms, creating a clock-like appearance.

Troubleshoot

If the code is not working, there are some common issues you can troubleshoot:

  • Display shows nothing: Check that VCC and GND are connected. Verify the SCLK, RCLK, and DIO pins match your code. Make sure display.begin() is called in setup() and display.loop() is called in loop().
  • Segments show wrong characters: Confirm whether your module is common anode or common cathode. The default is common anode. For common cathode, pass false as the fourth parameter: DIYables_4Digit7Segment_74HC595 display(7, 6, 5, false).
  • Display flickers or goes blank: The display.loop() must be called frequently. Avoid using delay() in your code - use display.delay() instead to keep the display refreshed.

Platform Support

The library uses only Arduino standard APIs (pinMode, digitalWrite, millis) and supports all Arduino platforms (architectures=*).

The Best Arduino Starter Kit

※ OUR MESSAGES