Seven Segment (Common ANODE) on Arduino

It feels like forever since I’ve posted! However, I have a very small post for beginners.

We’re going to use an Arduino to drive a single digit seven segment display. Why something so simple?
So this way we can build on this fundamental. This will also give a gentle introduction to reading datasheets.

Let’s start out with what we need:

  1. Arduino Uno (other variants will work, but for ease, I’m saying the Uno).
  2. A common anode seven segment. I’m using this one, but at the time of writing they are out of stock. Places like Digikey, Adafruit, and many other retails should have them if you search for those words.
  3. A 400Ohm Resistor (greater is OK)
  4. Our datasheet. If you can’t find one, then here is one.

Now, first things first: when we say common anode, we mean that it has a common “Vcc” rail[1]. This is opposed to our common cathode which means that each segment shares a “GND” rail.

Let’s take a look at our datasheet and open to page 2. Here we can see where Pin 1 is (lower left hand corner), how the segments are laid out, and how each segment works.

If we look at the following, we can see that we should provide pin 3 and 8 with our Vcc line:

Now if we look deeper into the data sheet, it says our MAX rating should be 2.5V. Now I could go into Ohm’s law (and I really should), but I’ll just say for 3.3V use a 400Ohm resistor. That said, 400Ohm is not a standard value. The closest is 390, which may burn out our 7-segment LED, so the next closest standard is 430Ohm which will work. To be completely honest, I’m using a 1KOhm resistor as I write this up, so don’t be “afraid” of not using the exact same value. 🙂

Let’s start out with just a “Hello World”. Connect your display as the following:

In this case, my 7-segment has the pins on the top and bottom and are five across. When we apply power, our segment should look like this:

Now if we compare this to our two images above, we can see that Segment E matches up with Pin 1. Likewise, you can take that wire on Pin 1 and turn on different segments.

Now with this done, we can write some Arduino code to count from 0 to 9 for us. To turn on multiple segments at the same time, just connect more segments to ground.

#define A 7
#define B 6
#define C 5
#define D 4
#define E 3
#define F 8
#define G 9

/**
 * Let's define what some numbers look like:
 * NUM | Segments on
 * -----------------
 *  0  | A,B,C,D,E,F
 *  1  | B,C
 *  2  | A,B,G,E,D
 *  3  | A,B,G,C,D
 *  4  | F,G,B,C
 *  5  | A,F,G,C,D
 *  6  | A,F,G,C,D,E
 *  7  | A,B,C
 *  8  | A,F,B,G,E,C,D
 *  9  | A,F,B,G,C
 */

/**
 * @brief turns off all of the segments
 */
void turnOffEverything()
{
  digitalWrite(A, HIGH);
  digitalWrite(B, HIGH);
  digitalWrite(C, HIGH);
  digitalWrite(D, HIGH);
  digitalWrite(E, HIGH);
  digitalWrite(F, HIGH);
  digitalWrite(G, HIGH);
}

void setup() {
  // put your setup code here, to run once:
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);

  turnOffEverything();

}

void loop() {
  // put your main code here, to run repeatedly:
  for(int x = 0; x < 10; x++)
  {
    turnOffEverything();
    switch(x)
    {
      case 0:
        digitalWrite(A, LOW);
        digitalWrite(F, LOW);
        digitalWrite(B, LOW);
        digitalWrite(E, LOW);
        digitalWrite(C, LOW);
        digitalWrite(D, LOW);
        break;
      case 1:
        digitalWrite(B, LOW);
        digitalWrite(C, LOW);
        break;
      case 2:
        digitalWrite(A, LOW);
        digitalWrite(B, LOW);
        digitalWrite(G, LOW);
        digitalWrite(E, LOW);
        digitalWrite(D, LOW);
        break;
      case 3:
        digitalWrite(A, LOW);
        digitalWrite(B, LOW);
        digitalWrite(G, LOW);
        digitalWrite(C, LOW);
        digitalWrite(D, LOW);
        break;
      case 4:
        digitalWrite(F, LOW);
        digitalWrite(G, LOW);
        digitalWrite(B, LOW);
        digitalWrite(C, LOW);
        break;
      case 5:
        digitalWrite(A, LOW);
        digitalWrite(F, LOW);
        digitalWrite(G, LOW);
        digitalWrite(C, LOW);
        digitalWrite(D, LOW);
        break;
      case 6:
        digitalWrite(A, LOW);
        digitalWrite(F, LOW);
        digitalWrite(G, LOW);
        digitalWrite(E, LOW);
        digitalWrite(C, LOW);
        digitalWrite(D, LOW);
        break;
      case 7:
        digitalWrite(A, LOW);
        digitalWrite(B, LOW);
        digitalWrite(C, LOW);
        break;
      case 8:
        digitalWrite(A, LOW);
        digitalWrite(F, LOW);
        digitalWrite(B, LOW);
        digitalWrite(G, LOW);
        digitalWrite(E, LOW);
        digitalWrite(C, LOW);
        digitalWrite(D, LOW);
        break;
      case 9:
        digitalWrite(A, LOW);
        digitalWrite(F, LOW);
        digitalWrite(B, LOW);
        digitalWrite(G, LOW);
        digitalWrite(C, LOW);
        break;
    }
    delay(1000);
  }  
}

You'll hook it up like this:

Again, remember what was mentioned earlier. Also to note, that those are bends in the wire and not junctions and that I left Pin 5 (DP) unconnected.

You'll notice that to turn on a segment, we write LOW to it. Remember when we were switching things on by hand, we connected a segment to Ground to turn it on.

Anyway, upload it and watch your counter go from 0 to 9 and back over again.

If you want this code for yourself and you're too lazy to copy and paste, then here it is.

[1]: This is not 100% accurate, but it'll work for now.

This entry was posted in Engineering. Bookmark the permalink.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.