7 Segment display on Arduino with 595

7_segment_display7 Segment displays are easy to attach to an arduino (or other microprocessor). Just connect the digital Outputs of the Arduino with a segment of the display (via a current limiting 470-560 ohm resistor: the IC can in total only deliver 70mA) and fire away.

Nothing wrong with that except for normally needing 8 digital pins for that (if you are using the point as well), and in some cases that might just be too much.

It is possible to use only 3 output pins if you are using a serial shift register like the 74HC595.
This chip will take a base-10 number and translate that into a binary number on its 8 output pins. So if I send  ‘0’ the output will be ‘00000000’ and if I send ‘255’ the output will be ‘11111111’ so with the proper connections to the pins of the display, the correct segments can be made to lit up.

When you get a 7 segment display it is not always clear what the pin configuration of that chip is. Sometimes it is not even clear if it is a common anode or a common cathode display, so that is what we need to figure out first. Ofcourse if you have a datasheet, that helps, but it can’t hurt to check, just to make sure. If you look at the picture below, it is already clear what the common pins are. Use a simple multimeter, keep  one probe on the common pin and see what segments light up  when you touch a pin with the other probe

7seg_dispA simple way  to set up the proper connections is to follow the more or less standardized connections below:
Q0 ->A  (or, to be more precise, the pin that lights up segment ‘A’)
Q1->B
Q2->C
Q3->D
Q4->E
Q5->F
Q6->G
Q7->DP
It is obvious that  sending ‘0’  to the display will not light up any segments while sending ‘255’ to the display will light up all the segments, including the DP.
As the DP is connected to Q7 it is clear that sending 127 will light up all the segments except DP, which means it will form an ‘8’.

For the ‘0’ you need all the elements to light except G. As G is connected to  Q6, you will need ‘11111100’ on the 595, which is ’63’
With the chosen connections, the following numbers will make the ‘0’ til ‘9’:

63, 6, 91,79, 102, 109, 125,7, 127,111.

(192,249,164,176,153,146,130,248,128,144  for common anode)
If for whatever reason (easier in yr print design perhaps), just set up a truth table and determine the base numbers you need to send

After you have those base-numbers that represent a specific number, store those in an array, from where you can send them to the HCT595.

Your connections between Arduino, 74HC595 and 7 segment will look like this:

Arduino pin 5 => 74HC595 pin 12 (latch)
Arduino pin 6 => 74HC595 pin 14 (data)
Arduino pin 7 => 74HC595 pin 11 (clock)

74HC595 pin 1  (Q1)   => LED Pin 5  (B)
74HC595 pin 2  (Q2)   => LED Pin 9  (C)
74HC595 pin 3  (Q3)   => LED Pin 7  (D)
74HC595 pin 4  (Q4)   => LED Pin 6  (E)
74HC595 pin 5  (Q5)   => LED Pin 2  (F)
74HC595 pin 6  (Q6)   => LED Pin 1 (G)
74HC595 pin 7  (Q7)   => LED Pin 10  (DP)
74HC595 pin 8  (GND)  => Ground
74HC595 pin 9  (Q7S)  => Not connected
74HC595 pin 10 (MR)   => Vcc (High)
74HC595 pin 11 (SHCP) => Arduino pin 7
74HC595 pin 12 (STCP) => Arduino pin 5
74HC595 pin 13 (OE)   => Ground (Low)
74HC595 pin 14 (DS)   => Arduino pin 6
74HC595 pin 15 (Q0)   => LED Pin 4  (A)
74HC595 pin 16 (Vcc)  => Vcc

the choice for the arduino pins connecting to the 595 lines… that is  randomly chosen. they could have been other pins

LED pin 3 or 8 => 470 Ohm resistor => Vcc  (for a common anode display)  or  => Ground (for a common cathode display)
With regard to the last line there is some debate whether with a 7 segment display it would be enough to use only one resistor in the common anode or cathode or that they each should have their own resistor. Obviously, displaying a ‘1’ involves only 2 segments to light up, whereas a ‘0’ requires 6 segments to light up and therefore would be less bright. For testing purposes you could use 1 resistor of say 330 ohm in the common lead. For final circuit I would advise separate resistors.

7segment

7_segmentPrint

7segm_action

Program:

/*
Using a 7-segment display with the 74HC595 shift register
*/

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
// for the number '11'  use code '31' for CC and '224' for CA
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

void setup()
{
pinMode(latchpin, OUTPUT);
pinMode(clockpin, OUTPUT);
pinMode(datapin, OUTPUT);
}
void loop()
{

digitalWrite(latchpin, LOW);
shiftOut(datapin, clockpin, MSBFIRST, 0); // clears 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
// whose values correspond to the binary outputs 0~9
// add 128 for digital point
{
digitalWrite(latchpin, LOW);
shiftOut(datapin, clockpin, MSBFIRST, segment[lus]+128);
digitalWrite(latchpin, HIGH);
delay(500);
}

digitalWrite(latchpin, LOW);
shiftOut(datapin, clockpin, MSBFIRST, 0); // clear the display
digitalWrite(latchpin, HIGH);
delay(1000);

digitalWrite(latchpin, LOW);
shiftOut(datapin, clockpin, MSBFIRST, 128); // light the decimal point
digitalWrite(latchpin, HIGH);
delay(1000);

}

mind you that it is possible to count to ’11’ on the display. just write ’31’ (‘00011111’) to the 595

VQB 27 E
VQB 27 E

A popular 7 segment matrix is the VQB 27 E Y3. The pin out is a bit different. You will find it in the image

6 thoughts on “7 Segment display on Arduino with 595

  1. You switched CA/CC in the code, Common Cathode is common to 0V, so the 595 is sourcing +Vcc to the resistors of the segments.

    So:

    // for the number ’11’ use code ’31’ for CC and ‘224’ for CA
    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

    should be:

    // for the number ’11’ use code ’31’ for CA and ‘224’ for CC
    int segment[10] = {192,249,164,176,153,146,130,248,128,144 }; // for common cathode
    // int segment[10] = {63,6,91,79,102,109,125,7,127,111 }; // for common anode

    Just tested with CC display.

    Please tell me if I’m wrong.

    Best regards,

    Gerard

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