7-segment-display

Task:

Control a 7-segment display!

The basics:

It’s not difficult to use a 7-segment display. You need only some things to know …
There are two types of displays – with common anode or common cathode.
If these are kingbright elements, you identify SAxxx for a display with common anode, and SCxxx for a displays with common cathode.
A 7-segment display is similar a small circuit with 8 LEDs. Seven LEDs are the main, the 8st is the dot. So we have 7 input pins for the main LEDs, one input pin for the dot and the other two are for common anode or cathode. Thats all!

In this tutorial, I use an 7-segment display with common cathode:

1.4-7-segment-data1  1.4-7-segment-data

Now, you will see, we need a lot of wires! Eight wires for the LEDs, and each LED needs a resistor (like a single LED) to determine the current. Eight wires additional. And two for the common anode or cathode.

In this tutorial, I show you a little trick to save wires.
Normaly, you use one wire from the Arduino to the resistor on the breadboard. Another one from the resistor to the 7-segment display (or to an LED). This are two wires (female – female wires). If you have female-male wires, use this and connect the resistor into the male wire pin directly. The other side of the resistor is the female wire part to connect at the breadboard. Look here:

wire-resistor

The circuit:

1.4-7-segment

For this tutorial, wee need the following components:

  • SC56-11GMA (common cathode, green) or similar
  • 8x resistors (min. 150 ohm), I use 220 ohm

Here is the wiring diagram:

1.4-7-segment-circuit

The Arduino sketch:

/*
7-segment-display
playground2014.wordpress.com
*/
byte digitArray[11][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
                           { 0,0,0,0,0,0,0 } // = off
                         };

void setup() {
  pinMode(2, OUTPUT);    // kingbrigth - a
  pinMode(3, OUTPUT);    // kingbrigth - b
  pinMode(4, OUTPUT);    // kingbrigth - c
  pinMode(5, OUTPUT);    // kingbrigth - d
  pinMode(6, OUTPUT);    // kingbrigth - e
  pinMode(7, OUTPUT);    // kingbrigth - f
  pinMode(8, OUTPUT);    // kingbrigth - g
  pinMode(9, OUTPUT);    // kingbrigth - dot
  digitalWrite(9, LOW);  // "dot" off
}

// we define a function to display one digit
void setDigit(byte digit) {
  byte pin=2;
  for (byte i = 0; i < 7; ++i) {
    digitalWrite(pin, digitArray[digit][i]);
    ++pin;
  }
}

void loop() {
  setDigit(10); // display off
  delay(500);

  for (byte count = 0; count < 10; ++count) {
    setDigit(count);
    delay(700);
  }

  setDigit(10); // display off
  delay(500);

  for (int count = 9; count >= 0; --count) {
    setDigit(count);
    delay(700);
  }

  delay(1000);
}

First, we define our variables.

To display one digit, you must control seven pins (we don’t use the “dot” here). One example: to display the digit “1”, you must set “b” and “c” HIGH (pin 3 and 4), and all the other elements must set LOW.
To make the sketch easy, we define the pin pattern in an array. We define eleven rows, and each row has seven colums. In each row we store the pin pattern of one digit. We start with the “a” value, then “b” value and we finish with the “g” value. The correct row for the digit “4” is: 0,1,1,0,0,1,1.

void setDigit() { … }

void setDigit(byte digit) {
  byte pin=2;
  for (byte i = 0; i < 7; ++i) {
    digitalWrite(pin, digitArray[digit][i]);
    ++pin;
  }
}

We use (define) a function to set a digit. We start the function with the digit, which we want to see. The digit number and the row numer in our array are the same. The loop pick up each value from the array and send the correct value with digitalWrite to the related pin.

void setup() { … }
We define the direction – we use all pins as output pins.
We set pin 9 to LOW, we don’t use the dot pattern in this sketch.

void loop() { … }
setDigit(10) means, we want to see row 10 from our array. Row 10 is “display off”, each pin is set to LOW. Than, after waiting a little bit, we start a loop to set each digit from 0 to 9. After this, the same in the other direction.

But there is a stumbling block! If the step down counter is of type BYTE, the loop will work not correctly. In a variable of type byte you can store values from 0 to 255. Look at our loop: The “last” step, the counter is 0. Then, we start again and we calculate 0 minus 1. The result is not -1, the result is 255! And, 255 is greater than 0, we stay in the loop and try to display row 255!

2 thoughts on “7-segment-display

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s