Seven Segment Display Library

Written by Dean Reading, 2013

This library allows an arduino to easily display numbers in decimal format on a 4 digit 7-segment display without a separate 7-segment display controller.

Download it here:

Version 2.3: Adds brightness control.

Version 2.2: Allows for 1, 2 or 3 digit displays to be used.

Version 2.1: Includes a bug fix that could cause some stray segments lighting up unintentionally.

Version 2.0: Now works for any digital pin arrangement, common anode and common cathode displays.

Direct any questions or suggestions to

Thanks to Mark Chambers and Nathan Seidle for code used in updates.


Seven Segment Displays

4 digit, 7 segment displays use 12 digital pins.

There are:
4 common pins; 1 for each digit. These will be cathodes (negative pins) for common cathode displays, or anodes (positive pins) for common anode displays. I refer to these as digit pins.
8 pins for the individual segments (seven segments plus the decimal point). I refer to these as segment pins.


Connect the four digit pins with four limiting resistors in series to any digital or analog pins.
Connect the eight cathodes to any digital or analog pins.

Transistors and Big Displays

If you are using transistors to perform switching, then you will require different polarities to illuminate segments. You will have to edit the SevSeg.cpp file and change either DigitOn & DigitOff, or SegOn & SegOff, depending on your configuration.


I have a cheap common anode display from China, and the pins of the display are in the following order when viewed from the front:

Top Row

Bottom Row

Where the digit pins are 1-4 and the segment pins are a-g + dp



Call sevseg.Begin in setup. The first argument (boolean) tells whether the display is common cathode (0) or common anode (1).
The next four arguments (bytes) tell the library which arduino pins are connected to the digit pins of the seven segment display. Put them in order from left to right. The next eight arguments (bytes) tell the library which arduino pins are connected to the segment pins of the seven segment display. Put them in order a to g then the dp.

In summary: Begin(type, digit pins 1-4, segment pins a-g,dp)


The calling program must run the PrintOutput() function repeatedly to display the number.

Setting Number

To set the number displayed, use the NewNum function. Any number between -999 and 9999 can be displayed. Out of range numbers show up as ----.

To move the decimal place one digit to the left, use '1' as the second argument in NewNum. For example, if you wanted to display '3.141' you would call NewNum(3141,3);


To change the brightness of the display, use the brightness function.
The brightness is defined by a value from 0 to 100. Default is 100. e.g. call sevseg.Brightness(90); Obtain the current brightness value with the same function. e.g. Serial.println(sevseg.Brightness()); 0 does NOT correspond to no brightness. If you wish for the display to be any dimmer than brightness=0, run PrintOutput less frequently.

1, 2 or 3 digit displays

Change NUM_DIGITS in the file SevSeg.h to use a 1, 2 or 3 digit display.
The number processing is not altered, so it is really just a quick fix. This means that 'out of bounds' is still considered above 9999 or below -999, regardless of NUM_DIGITS. You must still feed in all 12 pins. However, the program will not alter the digit pins that are higher than NUM_DIGITS. eg if NUM_DIGITS is 2, then D3 and D4 won't be altered.