### 7-seg Display

How many TV shows and movies have you seen with some mysterious electronic device counting down to zero on one of those 7 segment LED displays?  If we were in that situation, we would be thinking:

"Wow, where did they get that in cool blue?  They are usually red."

"I wonder if it has a common anode or cathode?"

"That would take up a lot of IO pins on an Arduino."

TOO LATE!

The seven segment display is a pretty simple device.  It is actually 8 LEDs (the decimal point is the 8th). It can be arranged so that different combinations can be used to make numerical digits.  This tutorial will show you how to wire one up and drive it with an Arduino.

Video Demo of Arduino 7 Segment LED

* This tutorial has been updated with info for our new common cathode, seven segment LEDs *

Hardware used in this tutorial:

Arduino board, Solderless breadboard, jumper wires, and the blue or red seven segment LED.

Instructions:

-----

If this is your first Arduino project, first go through our “Arduino: Getting Started” and “Beginning Solderless Breadboards” tutorials.

-----

Use our LED resistor calculator to calculate the resistor value that won't destroy your LED!  Connecting these LEDs directly to Arduino IO pins will eventually burn them out!  Connect LED pins 3 and 8 to GND.  Use a resistor between each of the other connections to your Arduino.

Here are the pin mappings of the display:

Use your solderless breadboard to make the connections between the seven segment LED and your Arduino board:

 Arduino Pin 7 Segment Pin Connection 2 7 (A) 3 6 (B) 4 4 (C) 5 2 (D) 6 1 (E) 7 9 (F) 8 10 (G) 9 5 (DP)

Software

This Arduino software example counts down from 9 to 0.  This is a nice, compact version that uses a 2 dimensional array to hold the LED bit patterns, and "for" loops to get things done.  If you want a longer, more verbose method, scroll down for that.

`// Arduino 7 segment display example software`
```// http://www.hacktronics.com/Tutorials/arduino-and-7-segment-led.html```
```// License: http://www.opensource.org/licenses/mit-license.php (Go crazy)```

```// define the LED digit patters, from 0 - 9```
```// 1 = LED on, 0 = LED off, in this order:```
```//                                    Arduino pin: 2,3,4,5,6,7,8```
`byte seven_seg_digits[10][7] = { { 1,1,1,1,1,1,0 },  ````// = 0```
```                                                           { 0,1,1,0,0,0,0 },  ````// = 1`
```                                                           { 1,1,0,1,1,0,1 },  ````// = 2`
```                                                           { 1,1,1,1,0,0,1 },  ````// = 3`
```                                                           { 0,1,1,0,0,1,1 }, ```` // = 4`
```                                                           { 1,0,1,1,0,1,1 },  ````// = 5`
```                                                           { 1,0,1,1,1,1,1 },  ````// = 6`
```                                                           { 1,1,1,0,0,0,0 },  ````// = 7`
```                                                           { 1,1,1,1,1,1,1 },  ````// = 8`
```                                                           { 1,1,1,0,0,1,1 }   ````// = 9`
```                                                           };```

`void setup() {                `
```  pinMode(2, OUTPUT);   ```
`  pinMode(3, OUTPUT);`
```  pinMode(4, OUTPUT);```
`  pinMode(5, OUTPUT);`
`  pinMode(6, OUTPUT);`
```  pinMode(7, OUTPUT);```
`  pinMode(8, OUTPUT);`
`  pinMode(9, OUTPUT);`
```  writeDot(0);  // start with the "dot" off```
`}`

```void writeDot(byte dot) {```
`  digitalWrite(9, dot);`
`}`
`    `
```void sevenSegWrite(byte digit) {```
`  byte pin = 2;`
```  for (byte segCount = 0; segCount < 7; ++segCount) {```
```    digitalWrite(pin, seven_seg_digits[digit][segCount]);```
`    ++pin;`
`  }`
`}`

```void loop() {```
`  for (byte count = 10; count > 0; --count) {`
```   delay(1000);```
`   sevenSegWrite(count - 1); `
`  }`
`  delay(4000);`
`}`

` `

` `

OK, that was the short, tricky version.  Here is a version that does the same thing, but is easier to understand:

```// Longer, more obvious example for Arduino 7 segment display```
```// http://www.hacktronics.com/Tutorials/arduino-and-7-segment-led.html```
```// License: http://www.opensource.org/licenses/mit-license.php (Go crazy)```

```void setup() {               ```
`  pinMode(2, OUTPUT);  `
```  pinMode(3, OUTPUT);```
`  pinMode(4, OUTPUT);`
`  pinMode(5, OUTPUT);`
```  pinMode(6, OUTPUT);```
`  pinMode(7, OUTPUT);`
`  pinMode(8, OUTPUT);`
```  pinMode(9, OUTPUT);```
```  digitalWrite(9, 0);  // start with the "dot" off```
`}`

`void loop() {`
` // write '9'`
` digitalWrite(2, 1);`
``` digitalWrite(3, 1);```
` digitalWrite(4, 1);`
` digitalWrite(5, 0);`
``` digitalWrite(6, 0);```
` digitalWrite(7, 1);`
` digitalWrite(8, 1);`
` delay(1000);`
``` // write '8'```
` digitalWrite(2, 1);`
` digitalWrite(3, 1);`
``` digitalWrite(4, 1);```
` digitalWrite(5, 1);`
` digitalWrite(6, 1);`
``` digitalWrite(7, 1);```
` digitalWrite(8, 1);`
` delay(1000);`
` // write '7'`
``` digitalWrite(2, 1);```
` digitalWrite(3, 1);`
` digitalWrite(4, 1);`
``` digitalWrite(5, 0);```
` digitalWrite(6, 0);`
` digitalWrite(7, 0);`
``` digitalWrite(8, 0);```
` delay(1000);`
` // write '6'`
` digitalWrite(2, 1);`
``` digitalWrite(3, 0);```
` digitalWrite(4, 1);`
` digitalWrite(5, 1);`
``` digitalWrite(6, 1);```
` digitalWrite(7, 1);`
` digitalWrite(8, 1);`
` delay(1000);`
``` // write '5'```
` digitalWrite(2, 1);`
` digitalWrite(3, 0);`
``` digitalWrite(4, 1);```
` digitalWrite(5, 1);`
` digitalWrite(6, 0);`
``` digitalWrite(7, 1);```
` digitalWrite(8, 1);`
` delay(1000);`
` // write '4'`
``` digitalWrite(2, 0);```
` digitalWrite(3, 1);`
` digitalWrite(4, 1);`
``` digitalWrite(5, 0);```
` digitalWrite(6, 0);`
` digitalWrite(7, 1);`
``` digitalWrite(8, 1);```
` delay(1000);`
` // write '3'`
` digitalWrite(2, 1);`
``` digitalWrite(3, 1);```
` digitalWrite(4, 1);`
` digitalWrite(5, 1);`
``` digitalWrite(6, 0);```
` digitalWrite(7, 0);`
` digitalWrite(8, 1);`
` delay(1000);`
``` // write '2'```
` digitalWrite(2, 1);`
` digitalWrite(3, 1);`
``` digitalWrite(4, 0);```
` digitalWrite(5, 1);`
` digitalWrite(6, 1);`
``` digitalWrite(7, 0);```
` digitalWrite(8, 1);`
` delay(1000);`
` // write '1'`
``` digitalWrite(2, 0);```
` digitalWrite(3, 1);`
` digitalWrite(4, 1);`
``` digitalWrite(5, 0);```
` digitalWrite(6, 0);`
` digitalWrite(7, 0);`
``` digitalWrite(8, 0);```
` delay(1000);`
` // write '0'`
` digitalWrite(2, 1);`
``` digitalWrite(3, 1);```
` digitalWrite(4, 1);`
` digitalWrite(5, 1);`
``` digitalWrite(6, 1);```
` digitalWrite(7, 1);`
` digitalWrite(8, 0);`
` delay(4000);`
`}`

Send feedback for this tutorial here.

Happy hacking.

You should be able to adapt the program to use a common anode.  You
will need to wire the Arduino IO pins to the cathode segment pins
instead of the anode segment pins.  Then just change the software
logic.  In this array, change all of the 0 to 1, and all the 1 to 0:

------------------------------
`-----------------------------`
``` byte seven_seg_digits[10][7] =            { { 1,1,1,1,1,1,0 },  // = 0```
```                                                           {```
``` 0,1,1,0,0,0,0 },  // = 1```
```                                                           {```
``` 1,1,0,1,1,0,1 },  // = 2```
```                                                           {```
``` 1,1,1,1,0,0,1 },  // = 3```
```                                                           {```
``` 0,1,1,0,0,1,1 },  // = 4```
```                                                           {```
``` 1,0,1,1,0,1,1 },  // = 5```
```                                                           {```
``` 1,0,1,1,1,1,1 },  // = 6```
```                                                           {```
``` 1,1,1,0,0,0,0 },  // = 7```
```                                                           {```
``` 1,1,1,1,1,1,1 },  // = 8```
```                                                           {```
``` 1,1,1,0,0,1,1 }   // = 9```
```                                                           };```
``` ------------------------------````-----------------------------`

``` That should probably do it.```

``` Best,```
``` Mark McComb```