Board and part

Arduino Uno 7-Segment Display Simulator: Wiring, Code and a Live Circuit

This is a single-digit common cathode 7-segment display wired to an Arduino Uno and counting from 0 to 9, live: the drawing shows the lit segments and the serial monitor names the digit.

  • 5 V logic
  • No library
  • Segments on 2, 3, 4, 5, 6, 7, 8
  • Common cathode, 220 ohm
Arduino Uno · 7-Segment Displaylive0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.
Connections

Wiring the 7-Segment Display to the Arduino Uno

Eight connections: seven segment pins from pin 2 onward, and the common through a 220 ohm resistor to GND. Open it in the editor and change the DIGITS table to draw letters.

Every connection in the circuit above, with the board’s own pin names.

7-Segment Display pinArduino Uno pinWhy
Segment apin 2Segments are named a to g, clockwise from the top, with g across the middle.
Segment bpin 3
Segment cpin 4
Segment dpin 5
Segment epin 6
Segment fpin 7
Segment gpin 8
Common (COM)GND, through 220 ohmA common cathode display: every segment returns its current through this one resistor.

Power

At 5 V a red segment through 220 ohm gets about 14 mA, inside the pin’s 20 mA. One resistor on the common is a shortcut: the more segments are lit, the less each one gets, so an 8 is dimmer than a 1. The better wiring is one resistor per segment.

Pins to leave alone on the Arduino Uno

Pins 0 and 1 are the USB serial port on a real Uno, so anything wired there fights the upload and the serial monitor. Pin 13 carries the onboard LED.

On a real bench

On a real bench the Uno stands beside the breadboard: the 7-Segment Display goes into the breadboard and male-to-male jumpers run from the Uno’s female headers to its rows. Power the Uno from USB while you build, and unplug it before you move a wire.

The sketch

The code, and what it does.

The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.

7-segment counter on Arduino Uno
// Segments a to g, in order. The common cathode goes to GND through 220 ohm.
const int SEG_PINS[] = {2, 3, 4, 5, 6, 7, 8};

// One bit per segment: bit 0 is a, bit 6 is g.
const byte DIGITS[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};

void show(byte mask) {
  for (int i = 0; i < 7; i++) {
    digitalWrite(SEG_PINS[i], (mask >> i) & 1);
  }
}

void setup() {
  Serial.begin(9600);
  for (int i = 0; i < 7; i++) pinMode(SEG_PINS[i], OUTPUT);
}

void loop() {
  for (int d = 0; d < 10; d++) {
    show(DIGITS[d]);
    Serial.print("Showing ");
    Serial.println(d);
    delay(400);
  }
}

A seven-segment digit is seven LEDs and a decimal point in one package, with the cathodes joined into a common pin. Each digit is stored as one byte with a bit per segment, so 0x06 lights b and c and shows a 1, and 0x7F lights all seven for an 8.

show() writes each bit of the byte to its pin. The loop walks the ten bytes and holds each for 400 ms, and the serial monitor says which digit it is on. On a common anode display the common goes to the supply instead and every bit is inverted.

Serial.begin(9600) opens the serial monitor. Serial goes over the USB cable, and the serial monitor in the editor reads it at whatever baud rate the sketch sets.

Pitfalls

Common mistakes with the 7-Segment Display on the Arduino Uno

The wrong display type

A common anode display needs the common on the supply and every bit inverted. Run common cathode code on it and each digit shows as its own negative. The part has a common property in the editor.

Wiring by position, not by letter

The pins on the package are not in segment order. Wire each by its letter, from the pin table, not from the order they sit on the body.

No current limit

Each segment is an LED. Without a resistor somewhere in its path it draws more than a pin can give, and a real display or pin can burn out.

FAQ

Questions and answers

Can I simulate a 7-segment display with an Arduino Uno?

Yes. The circuit at the top of this page is an Arduino Uno with a 7-segment display, running in your browser on a simulated ATmega328P. The segments are solved as real LEDs, so the brightness changes with the resistor and with how many segments are lit. Press Run it in the editor to change the wiring or the code; it is free and needs no account.

How many pins does a 7-segment display need?

Seven for the segments, an eighth for the decimal point if you use it, and the common. Drive more digits with a shift register or a driver such as the TM1637, which this site also covers.

How do I show the decimal point?

It is an eighth LED on the DP pin. Wire it to another output pin through its own resistor and write it HIGH. This page leaves it unused.

Why does an 8 look dimmer than a 1?

Because all the lit segments share one resistor on the common pin. Put a 220 ohm resistor in series with each segment instead and every digit is equally bright.

Build your own 7-Segment Display project

Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.