Working with a 7-segment display

Recently it was announced that many Radioshack stores are closing due to bankruptcy issues. It turned out to be a good opportunity to pick up some parts and equipment to start messing around with Arduinos. I’ve always kind of wanted to get into Arduinos after taking a class on microcontrollers back in college.

I started out checking out a lot of the sample stuff. The “Blink” sample sketch seems to be the “hello world” for Arduinos. I blinked some LEDs, faded some LEDs, read buttons, made LEDs turn on with buttons, etc.

I picked up a “Basic Electronic Parts Kit” as well, and the 7-segment displays stuck out to me, so I gave them a shot.

There were two that came with the kit, the one I’m using for this post had the number 350074 on the side. It took many, many opened, closed, and lost tabs and google searches but I eventually found out that it’s a common-cathode display. Meaning that it only has (needs) one pin to go to ground. There are actually two pins, one on each side that are meant to connect to ground, and you can see which ones they are by looking at the bottom of the component, they’re connected, unlike the other pins.

After that it was just a matter of hooking the cathode up to ground with a resistor, loading the Blink sketch onto the Arduino and using the pin 13 output to check which of the other pins correspond to which segment.

7segment-pin-finding

I wrote down the pins and their segments, and used my MS Paint skills to draw this picture:

7segpins

I wanted to use that information to loop through the numbers 0 – 9 on repeat. The first thing I had to do was come up with a table for which pins would be “on” for which number.

pintable

I chose to start with pin 2 instead of 1 because I didn’t see pin 1 on the board. It seemed like a simple enough task. Start at 0, draw the current number, wait a little bit, then draw the next number, and when the current number reaches 10, reset it to 0.

The hard part was figuring out an efficient way to store the table of pins. I wanted to figure out as much of it myself as I could, but it had been a while since I worked with microcontrollers or languages that “low level”, so I ended up looking up a tutorial and found this one. The 2D array was exactly what I needed. (With all the C# and LINQ code I’ve written lately the concept of 2D arrays sort of got buried in my mind.)

Here is the code that I ended up with:

/* PIN SETUP (arduino pin numbers)
2
--------
|      |
8|      |3
--------
|   7  |
6|      |4
--------
5
*/

//values for pins 2-8
byte segmentPins[10][7] = {
{1,1,1,1,1,0,1}, //0
{0,1,1,0,0,0,0}, //1
{1,1,0,1,1,1,0}, //2
{1,1,1,1,0,1,0}, //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,1,0,1,1}  //9
};

byte currentNumber = 0;

void setup()
{
for(int i = 2; i <= 8; i++)
pinMode(i, OUTPUT);
}

void loop()
{
displayNumber(currentNumber++);
delay(500);
currentNumber %= 10;
}

void displayNumber(byte number)
{
//pin number is segment index + 2, no need for another variable
for(byte segmentIndex = 0; segmentIndex < 7; segmentIndex++)
digitalWrite(segmentIndex + 2, segmentPins[number][segmentIndex]);
}

Unfortunately I couldn’t find a matching 7-segment display in Fritzing so I didn’t make a fancy circuit diagram. Instead I just took a picture of the finished board:

arduino_segment

Next I would like to try chaining together a couple shift registers to have a double 7-segment display that uses less GPIO pins. That seems like it would be a good starting point for making a clone of Simon, where the two displays could show the high/current score.

One thought on “Working with a 7-segment display”

  1. I also took advantage of my local Radioshack closing, (something I’m not particularly happy about) and I bought the “Basic” Electronics parts kit when I was there. I honestly have no idea what quite a lot of the stuff does, and I was wondering if you had any idea what some of the ICs do. If not that’s OK. I’ve been messing with Arduinos for a little over a year, and I still can’t get figure them out, or find them online. (Seriously, a data sheet would have been nice!)
    BTW, in the future, when testing new led based components like the display, you can just connect it to the 5V pin (with a resistor of course) and move the connection to a numbered pin for control. That may be a bit easier than trying to work around the blink code.
    Thanks for the post, it was really helpful with the displays!

Leave a Reply

Your email address will not be published. Required fields are marked *