Arduino library 路 Displays

SevSeg: a 4-digit display with no driver chip

The bare four-digit seven-segment display in most starter kits has twelve pins and no chip behind it: eight segment lines (A to G and the decimal point) shared by all four digits, and one common pin per digit. Only one digit can be lit at a time, so the board has to flick through them fast enough that your eye sees all four. Dean Reading鈥檚 SevSeg library does that switching for you.

Built in on Mokxi: #include <SevSeg.h> and it builds, with nothing to install. Free, and no account needed.

What SevSeg does

You tell begin() whether the display is common cathode or common anode (it is printed on the part number, or test with a diode tester), which pins go to the digits and which to the segments, and whether the current-limiting resistors are on the segment pins or the digit pins. Getting the first one wrong lights the opposite of what you asked for.

Because the library lights digits one after another, sevseg.refreshDisplay() has to be called constantly from loop(). A delay() anywhere in loop() stops the multiplexing, and the display shows one dim digit or flickers. Use millis() for timing instead.

Library Manager nameSevSeg
AuthorDean Reading
Include#include <SevSeg.h>
Boardsevery board Mokxi compiles for but ATtiny85
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesFour-digit seven segment, multiplexed

Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <SevSeg.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "SevSeg" by Dean Reading from the Arduino IDE鈥檚 Library Manager (Sketch, Include Library, Manage Libraries), or add SevSeg to lib_deps in PlatformIO. Mokxi follows the calls of release 3.7.1, so a sketch written here builds there too.

What to know in the simulator: One digit is lit per refreshDisplay(), so loop() must call it often, as on the bench.

ESP32-C3 路 Arduino Uno 路 Arduino Nano 路 Arduino Mega 2560 路 Arduino Leonardo 路 SparkFun Pro Micro 路 Arduino Micro 路 ESP32-C6 路 Raspberry Pi Pico 路 Raspberry Pi Pico W 路 Seeed XIAO SAMD21 路 STM32 路 STM32 Nucleo-F411RE 路 STM32 Blue Pill 路 micro:bit V2 路 Arduino Uno R4 路 ESP32 DevKit 路 ESP32-S3-DevKitC-1 路 ESP8266 NodeMCU 路 Arduino Pro Mini 3.3 V 路 Seeed XIAO ESP32-C3 路 ESP32-C3 SuperMini 路 Seeed XIAO ESP32-C6 路 ESP32-DevKitC V4 路 LOLIN32 Lite 路 Wemos D1 R32 路 Cheap Yellow Display 路 TTGO T-Display 路 Seeed XIAO ESP32-S3 路 Arduino Nano ESP32 路 Arduino Nano RP2040 Connect 路 Seeed XIAO RP2040 路 Waveshare RP2040-Zero 路 Arduino Uno R4 WiFi

The calls you will use

In the order a sketch meets them.

SevSeg sevseg

Creates the display object. Configure it in setup() with begin().

sevseg.begin(COMMON_CATHODE, 4, digitPins, segmentPins, true)

Hardware type, number of digits, the pin arrays (segments in the order A to G, then DP) and true when the resistors are on the segment pins.

byte digitPins[] = {2, 3, 4, 5};
byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13};
sevseg.begin(COMMON_CATHODE, 4, digitPins, segmentPins, true);

sevseg.setBrightness(90)

Brightness by how long each digit stays lit, from -200 to 200. Higher values slow the refresh a little.

sevseg.setNumber(3141, 3)

Shows a number, here with the decimal point three places from the right: 3.141. Call it when the value changes, not every loop.

sevseg.setChars("HELP")

Shows letters, as far as seven segments can draw them.

sevseg.refreshDisplay()

Lights the next digit. Call it every time through loop(), as often as possible.

void loop() {
  if (millis() - last >= 1000) { last = millis(); sevseg.setNumber(++count); }
  sevseg.refreshDisplay();
}

sevseg.blank()

Turns all digits off until the next setNumber() or setChars().

Example: A number on a four-digit display

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino 路 Arduino Uno R3
#include "SevSeg.h"

SevSeg sevseg;

void setup() {
  byte numDigits = 4;
  byte digitPins[] = {2, 3, 4, 5};
  byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13};  // A B C D E F G DP
  sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins);
  sevseg.setBrightness(90);
  sevseg.setNumber(1234);
}

void loop() {
  sevseg.refreshDisplay();  // call it often: one digit is lit at a time
}

Wiring

  • Arduino Uno R3 2 to Four-digit seven segment, multiplexed DIG1
  • Arduino Uno R3 3 to Four-digit seven segment, multiplexed DIG2
  • Arduino Uno R3 4 to Four-digit seven segment, multiplexed DIG3
  • Arduino Uno R3 5 to Four-digit seven segment, multiplexed DIG4
  • Arduino Uno R3 6 to Four-digit seven segment, multiplexed A
  • Arduino Uno R3 7 to Four-digit seven segment, multiplexed B
  • Arduino Uno R3 8 to Four-digit seven segment, multiplexed C
  • Arduino Uno R3 9 to Four-digit seven segment, multiplexed D
  • Arduino Uno R3 10 to Four-digit seven segment, multiplexed E
  • Arduino Uno R3 11 to Four-digit seven segment, multiplexed F
  • Arduino Uno R3 12 to Four-digit seven segment, multiplexed G
  • Arduino Uno R3 13 to Four-digit seven segment, multiplexed DP

Open it in the editor, wired and ready to run

Common errors and how to fix them

Only one digit lights, or the display flickers badly

Something in loop() is blocking: a delay(), a long Serial wait or a slow sensor read. Time events with millis() and let refreshDisplay() run every pass.

Every segment is lit except the ones you asked for

The display is the other type. Swap COMMON_CATHODE for COMMON_ANODE in begin(), or COMMON_ANODE for COMMON_CATHODE.

Digits show the wrong shapes, such as 1 drawn as a 7

The segment pins are not in the order A, B, C, D, E, F, G, DP. Check each pin of the display against its datasheet and reorder the segmentPins array to match.

Digits have different brightness depending on the number

The resistors are on the digit pins, so a digit with more segments lit gets less current each. Move the resistors to the segment pins and pass true as the resistorsOnSegments argument.

Try SevSeg without installing anything

Open the example, change the sketch, and keep your version in a free account.