Four-digit 7-segment display with Arduino

FunduinoPublished on

Vier Ziffern per Multiplexing ansteuern – mit der SevSeg-Bibliothek

Aufgabe: Lass eine beliebige Zahl auf der vierstelligen 7-Segment-Anzeige erscheinen.

Das Display hat auf der Rückseite zwölf Kontakte, sechs oben und sechs unten. Vier davon gehören jeweils zu einer Ziffer – entweder in der Bauart Common Cathode oder Common Anode. Welche du hast, findest du später einfach mit dem Code heraus. Die anderen acht Kontakte gehören zu je einem Segment und zum Punkt neben einer Ziffer.

Bei Displays mit einer oder zwei Ziffern steuerst du die Segmente jeder Ziffer einzeln an. Bei vier Ziffern gäbe das ein noch größeres Kabeldurcheinander – deshalb arbeiten diese Displays mit Multiplexing: Sollen alle vier Ziffern gleichzeitig leuchten, werden sie zügig nacheinander angesteuert. Das geschieht so schnell, dass dein Auge sie als gleichzeitig wahrnimmt.

Four-digit 7-segment display for Arduino
Four-digit 7-segment display
Back of the 7-segment display with labeled pins
The twelve contacts on the back
1 / 2

You need this

5461AS 7-segment display - 4-digit, red, common cathode 5461AS 7-segment display - 4-digit, red, common cathode 1,33 € *Item no. F23106605About the product — 5461AS 7-segment display - 4-digit, red, common cathode
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
830-point Breadboard - solderless breadboard for Arduino 830-point Breadboard - solderless breadboard for Arduino 2,90 € *Item no. FUN-1010553About the product — 830-point Breadboard - solderless breadboard for Arduino
Breadboard Cables Male-to-Male - 40 Pieces, 20 cm Breadboard Cables Male-to-Male - 40 Pieces, 20 cm 1,74 € *Item no. FUN-1010558About the product — Breadboard Cables Male-to-Male - 40 Pieces, 20 cm
Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W 1,72 € *Item no. F23107216About the product — Resistor Assortment 100 Pieces - 5 Values 100 Ω to 10 kΩ, 0.5 W

All 5 parts together 18,81 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

The circuit diagram

Circuit diagram of the 7-segment display with four resistors
Schematic: 7-segment display with Arduino UNO R3
Photo of the setup with a 7-segment display connected to the Arduino
The completed setup on the breadboard
1 / 2

Include the library

To keep the code from getting endlessly long, you need a library that isn’t included in the Arduino software: Dean Reading’s SevSeg library.

Download the SevSeg library from GitHub

Then add it to the Arduino software. The easiest way is to go to Sketch → Include Library → Add .ZIP Library.

The program code

The SevSeg library contains more interesting example programs. You can find them in the Arduino software under File → Examples → SevSeg-master.

If you don't know what type of display you have, try both settings: COMMON_CATHODE and COMMON_ANODE. With the wrong setting, all segments light up at the same time.

Display a number
#include "SevSeg.h" // Die vorher hinzugefügte Bibliothek laden
SevSeg sevseg; // Ein Sieben-Segment-Objekt initialisieren
void setup()
{
  byte numDigits = 4; // Anzahl der Ziffern
  byte digitPins[] = {2, 3, 4, 5}; // Pins zu den Ziffern
  byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13}; // Pins zu den Segmenten
  // Hier gibst du an, ob dein Display COMMON_CATHODE oder COMMON_ANODE ist.
  // Es funktioniert nur mit der richtigen Angabe – sonst leuchten alle Segmente gleichzeitig.
  sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins);
}
void loop()
{
  // Hier trägst du die gewünschte Zahl ein, im Beispiel 1234.
  // Die Zahl dahinter steht für den Punkt hinter einer Ziffer: 3 ist der Punkt
  // neben der ersten Ziffer, 0 der ganz rechts. Willst du keinen Punkt, gib z. B. 4 an.
  sevseg.setNumber(1234, 3);
  // Diese Zeile lässt die Zahl auf dem Display erscheinen.
  sevseg.refreshDisplay();
  // Helligkeit von 0 bis 100, 100 ist am hellsten. 0 bedeutet nicht völlig dunkel.
  sevseg.setBrightness(90);
}
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.