By dave | February 2, 2014

In this tutorial I show you how to connect a 7 segment display to an Arduino board, along with a sketch that can display hexadecimal values on the 7-segment display.

The circuit can be easily built onto breadboard, but there are quite a few connections to wire up, so take your time and check the wiring carefully before powering up. Along with this sketch for single digit displays, we also have a tutorial for driving multi digit 7 segment displays with Arduino.

Things you’ll need for this tutorial:

• A 7 segment display
• 8 resistors that work with your display (range: 300R-2K2)
• An Arduino board with wire and bread board

Driving a single 7 segment display

Figure 1: Schematic showing how we will connect the display device to the Arduino; where A-G and DP connect to sequential pins on the Arduino board. Each segment of the display has a resistor in series to lower the current through the LED (avoiding damage to the LED or Arduino). 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 and therefore not recommended.

Figure 2: 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. If you are in any doubt as to which type you have check the polarity of your 7segment unit.

Counting in hex on a single 7segment display

To run the example, just paste this code into a new Arduino IDE session and change the variables to match the pins you’ve used.

• Variable `ledStart` should be set to the pin you started wiring the LED on, (for segment A and all other segments must follow on sequentially)
• Variable `commonHigh` should be true if you connected common to 5V, false if you connected it to GND.

Once you’ve made the adjustments, upload the sketch to the board, now you should see the single digit count up from 0 - F and reset.

``````
// 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 = true;

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++) {
writeDigitToDisplay(i);
delay(500);
}
}

// 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] = {
// bits     6543210
// digits   abcdefg
0b1111110,//0
0b0110000,//1
0b1101101,//2
0b1111001,//3
0b0110011,//4
0b1011011,//5
0b1011111,//6
0b1110000,//7
0b1111111,//8
0b1111011,//9
0b1110111,//a
0b0011111,//b
0b1001110,//c
0b0111101,//d
0b1001111,//e
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);
}
}

``````

Another single digit example analog measurements using a single digit 7segment display