Rollend Materieel
Basis Electronica
Symbols Electronica
Programming Arduino

10. 7 Segment LED display

The circuit that we are going to build shown as a schematic; where A -G and DP connect to sequential output ports on the Arduino board. Each segment of the display has a resistor in series to lower the current through the LED's. Note that although it may seem quicker to connect one resistor to the common pin, this would alter the brightness of the LED's depending how many segments were turned on.

the graphical view of a 7 segment display showing one common arrangement for internal wiring and pin arrangement. This shows a common anode unit so the two center common pins are connected to 5V. For common cathode this would be GND.

7 segment display
1k Ohm
Arduino mega

Place the 7 segment display
Connect the wires to the arduino
In the sources 'ledStart' is the first output port. The segment 'A' is connected to this port.
All other segments must follow on sequentially.

The code has two variable:
  • ledStart is the first output port that you have used (for segment A and all other segments must follow on sequentially).
  • Common should be at either 5V or GND depending on polarity, variable commonHigh should be set to true if common is connected to 5V or false if common is connected to GND.
// set this to the first pin where wiring starts.
int ledStart = 30;

// set to true if the common pin is HIGH, false otherwise
boolean commonHigh = false;

void setup() {
  // Enable all A-G and DP and outputs,
  // set them to OFF (if common is high, then 1 is off).
  for (int i = 0; i < 8; ++i) {
    pinMode(ledStart + i, OUTPUT);
    digitalWrite(ledStart + i, commonHigh ? 1 : 0);

void loop() {
  // write the number 0 - F (hex)
  // onto the display each half second


  for (int i = 0; i < 16; i++)

// Now we define the bits that are on and off
// for each segment, These are used in the
// function below to turn the right bits on.
int dig[16] = {
  0b1000111 //f

void writeDigitToDisplay(int digit) {
  // iterate through each bit
  for (int i = 0; i < 7; ++i) {
    // isolate the current bit in the loop.
    int currentBit = (1 << (6 - i));
    // and compare with that bit in the digit
    // definition above.
    int bitOn = (currentBit & dig[digit]) != 0;

    // if common is high we invert the logic, as 0 is on.
    if (commonHigh) {
      bitOn = !bitOn;

    // and lastly set the bit
    digitalWrite(ledStart + i, bitOn);

Download source
7 segment display live
7 Segment Display
Extra info 7 segment Display