Introduction: Quickly Set Up a 7 Segment Display

About: I am a physician by trade. After a career in the pharmeceutical world I decided to take it a bit slower and do things I like. Other than my hobbies that involves grassroots medicine in S.E.&P Asia. I have buil…

This is just a small instructable to help people to quickly use a 7 segment display with say an Arduino or other microcontroller. These displays require 8 datalines and that is a bit much to sacrifice on most microcontrollers, so there is a better way: using a HC595 or HCT595 shift register. In that case the display only needs 3 data lines.
Now it can be a bit daunting for the novice to figure out which pins to connect with what and then how to get the proper numbers to light up, but if you stick to some ground rules, it is fairly easy. (Note. most places I checked the HCT595 is substantially more expensive than the HC595, unless u really need/want/have the HCT version, pick the HC version.)

For one thing... it doesn’t really matter what pins of the 7 segment display you connect to the datapins of the 595, because you can sort it all out in the program code, but it is easiest to stick to some standard connections.

If you look at the diagram of the display, you will see that the segments have a more or less standardized name: A-G and DP. if you connect the 595 as follows:
Q0->A
Q1->B
Q2->C
Q3->D
Q4->E
Q5->F
Q6->G
Q7->DP
then the coding becomes standardized as well.
Determining which pin corresponds to what segment is quickly done with a multi-meter, just as establishing if you have a common cathode or common anode display. A look at the backside usually already tells you what the common connection is. Very often this is the middle pin on top and bottom.
As the 7 segment display -like LED's- cannot really take the full 5 volt of most micro controllers, a resistor in each line is necessary. I use 470 Ohm. It IS possible to use only one resistor in the common line, but then the light intensity will differ depending on the amount of segments being active.
This all is easy to solder on some perf-board as the picture shows, obviously if your pin lay-out differs from mine, you have to change the connections.... or send a different value to the HC595 . The circuit that i gave is for a common cathode display, but of course for a common anode display the principle is the same, albeit that one connects the common anode to +Vcc.

The code is rather self explanatory. Obviously one cannot just send a '0' or '9' character to the display to get the desired numbers. Every number requires a specific amount of segments to light up and that corresponds with specific ports of the 595 to be high or low. So for instance for the '8' you need all the segments to line up, except the decimal point and according to our standardized way of connecting, that requires the number 127 to be send to the 595 (127= 01111111= all segments except dp). If you want the '8' AND the DP, send the number 255.
So the easiest is to just set up an array with the required values for each number and use the number as an index to that array. So if you want a '0' you use member '0' from the array (that is 63), for the 1 you use member 1 from the array (that is value 6) etc.
If you want the digital point to light up as well, just add '128' to the value that is being sent.

The coding then is as follows
/*
Using the 74HC595 shift register with a 7 segment display
*/

int latchpin = 5;// connect to pin 12 on the '595
int clockpin = 7; // connect to pin 11 on the '595
int datapin = 6; // connect to pin 14 on the '595
// the array contains the binary value to make digits 0-9
// uncomment the proper line for common cathode or common anode int segment[10] = {63,6,91,79,102,109,125,7,127,111 }; // for common cathode //int segment[10] = {192,249,164,176,153,146,130,248,128,144 }; // for common anode //These values are always the right ones if you follow the rule of Q0->A, Q1->B etc. void setup() { pinMode(latchpin, OUTPUT); pinMode(clockpin, OUTPUT); pinMode(datapin, OUTPUT); } void loop() { digitalWrite(latchpin, LOW); shiftOut(datapin, clockpin, MSBFIRST, 0); // clear the display digitalWrite(latchpin, HIGH); delay(1000); for (int lus=0; lus<10; lus++) // counts from 0 to 9, using the values in the array // those values correspond to the binary outputs 0~9 // to get the digital point: add 128 { digitalWrite(latchpin, LOW); shiftOut(datapin, clockpin, MSBFIRST, segment[lus]+128); digitalWrite(latchpin, HIGH); delay(800); } digitalWrite(latchpin, LOW); shiftOut(datapin, clockpin, MSBFIRST, 0); // clear the display digitalWrite(latchpin, HIGH); delay(800); digitalWrite(latchpin, LOW); shiftOut(datapin, clockpin, MSBFIRST, 128); // light the decimal point digitalWrite(latchpin, HIGH); delay(800); }