Source: https://mokxi.com/learn/arduino-4-digit-7-segment-display
Updated: 2026-10-05

Learn

# A four-digit counter on an Arduino, with no driver chip

Advanced

Written by the Mokxi team, updated October 5, 2026

Start building free Open the editor

Or see every lesson.

- 261 parts on the bench
- 31 boards running now
- 1.00x real time, on every board

Click to open it in the editor

The Four-digit counter example on a simulated ATmega328P, multiplexing a bare 5641AS display four milliseconds a digit.

A 4 digit 7 segment display looks like it needs 32 wires, one per segment. It only has twelve legs. The trick is called multiplexing: the Arduino lights one digit at a time, very fast, and your eye sees all four at once. Once you understand it, you understand how most LED displays work.

The circuit above is an Arduino Uno driving a bare 4 digit display, a common-cathode 5641AS, on twelve pins. It counts up in tenths of a second, from 000.0 to 999.9. It runs in your browser on the sketch below, so you can slow the scan down and watch the digits take turns.

## What you need

- An Arduino Uno
- A 4 digit 7 segment display, common cathode (5641AS)
- Eight 220 ohm resistors, one per segment
- A full-size breadboard and jumper wires

## Wiring

Part and pin | Goes to | Note

Segment A | Pin 2 | Through a 220 ohm resistor

Segment B | Pin 3 | Through a 220 ohm resistor

Segment C | Pin 4 | Through a 220 ohm resistor

Segment D | Pin 5 | Through a 220 ohm resistor

Segment E | Pin 6 | Through a 220 ohm resistor

Segment F | Pin 7 | Through a 220 ohm resistor

Segment G | Pin 8 | Through a 220 ohm resistor

Segment DP | Pin 9 | The decimal point, through a 220 ohm resistor

DIG1 to DIG4 | Pins 10 to 13 | The four common legs; LOW turns a digit on

## Twelve legs for four digits

Eight legs are segments: A to G and the decimal point. Each segment leg is joined to that same segment in all four digits. The other four legs are the digits’ commons, one per digit.

On a common-cathode display, you turn a digit on by pulling its common LOW. You light a segment by driving its leg HIGH. So to show a 7 on digit 1, you set the segment pins for 7 and pull DIG1 LOW, with the other three digits HIGH.

Put the resistors on the segment legs, not on the commons. A resistor on a common is shared by every lit segment, so a digit dims as more of its segments come on. An 8 would look dimmer than a 1.

## Multiplexing: one digit at a time

Since the segment legs are shared, only one digit can show at a time. So the sketch shows digit 1 for 4 ms, then digit 2, then 3, then 4, and starts over. That is about 60 full scans a second, fast enough that your eye sees four steady digits. Each one is a quarter as bright as a digit held on.

Watch the order inside show(). It turns every digit off first, then sets the new segments, then turns on the one digit it wants. If you set the segments before turning the old digit off, the old digit flashes the new pattern for a moment. That faint ghost shows up in Mokxi too.

Showing one digit, from the Four-digit counter example

## Try it in the editor

Change delay(4) to delay(100). Now the scan is slow enough to see: the digits light one after another. Somewhere in between, the display flickers. That tells you how fast a real scan has to be.

Move the "all off first" line to after the segment loop and look closely for the ghost.

Click the display and set common to anode. Now the wrong segments light on the wrong digits, because a common-anode display needs every level flipped. Swap HIGH and LOW in show() to make it work.

## Or let a chip do the work

Twelve pins is a lot of an Uno. If you need those pins, a TM1637 module is this same display with a driver chip on the back. It needs only two pins, and the chip does the multiplexing. Our DS3231 desk clock tutorial uses one. A 74HC595 shift register, or a MAX7219, can also drive the segments from three pins.

## Common mistakes

Driving a common-anode display the common-cathode way. The digits come out wrong or dark. Check the part number: 5641AS is common cathode, 5641BS is common anode.

Putting the resistors on the commons, so digits dim as more segments light.

A delay() somewhere else in loop(). It stops the scan, and one digit stays lit while the others go dark. Keep the scan running, and time other things with millis().

Too much current on one pin. A common leg carries the current of every lit segment. At 220 ohms that is fine for a lesson, but the textbook circuit puts a transistor on each common for a brighter display.

## Questions

How many pins does a 4 digit 7 segment display need?

Twelve on a bare display: eight segments (with the decimal point) and four digit commons. A TM1637 module needs only two, because its chip does the multiplexing.

Do I need a library for a 4 digit 7 segment display?

No. The sketch above uses only digitalWrite and a small font table. On a real board, libraries such as SevSeg do the same scanning for you; SevSeg is not built into Mokxi, so the sketch here does it by hand.

Why does my display flicker?

The scan is too slow. Something in loop() takes too long between digits, usually a delay(). Each digit should get only a few milliseconds.

Related

## Keep going

Arduino 7 Segment Display: an Electronic Die Arduino DS3231 Clock With a TM1637 Display Shifting a Byte In, One Edge at a Time Arduino Blink Without Delay: Two LEDs, Two Rates How the four-digit display is modeled The single seven segment display The Arduino Uno simulator The project page, with the full sketch Advanced

## Build this for real

Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.

Start building free Open the editor
