7-Segment Display Wiring and Programming

Hey everyone, I was wondering if someone could help me figure out how to wire and eventually program two 7-segment displays of 4 digits each. I'd like to learn about it as I go.

Here is the data sheet for the 7-segment displays: http://www.us.kingbright.com/images/catalog/SPEC/SA18-11EWA.pdf

I'll be using an Arduino Mega, but I assume I'll need bit registers or something like that? Thanks for your help!

7 Segment displays are fairly easy.

You'll need 7 I/O pins for each digit if you do it without an additional components.

Otherwise, pick up a shift register from Sparkfun. You can then drop the I/O requirements significantly.

The datasheet shows that each of the seven segments on the display are labeled as a,b,c,d,e,f,g and dp (decimal point). Apply a voltage to pin 1 or 5, and the apply a voltage to the pin for the segment which you want to turn on. Pretty easy.

The trickier part will be wiring up your shift registers correctly, as you'll have a mess of wires for 8 digits (8x7 = 56 wires minimum).

You'll likely need to write some code that extracts a single decimal digit from an integer and figures out which display it goes on.

Hi, there is 2 digit 7-segment display example in my book.

So apply a voltage to the segment I want to light up and have it all connected to a common ground? Does the Arduino even have enough power/current for this?

How would I wire it with a shift register and which should I use?

The mega has lots of pins for this, you can use a pin per segment.

It can also provide up to 40mA per output, so thats fine too as you should probably aim for about 10mA per segment.

You will need a series resistor for each segment.

How would I wire it with a shift register and which should I use?

Here’s a shift register from Sparkfun… costs about $1.50

Read the datasheet on it and it will explain how to use it.

Basically, you connect it to the serial connection on your board, and then supply it with power and ground. Each time you send a byte on the serial connection, it shifts it into the register (1 byte = 8 bits). Until you send it another byte, those 8 bits will remain set. So, you just send a byte representing the digital outputs you want to turn on/off.

Hex       Binary
0xFF = 11111111       This turns all 8 outputs of the shift register on.
0x00 = 00000000       This turns all 8 outputs of the shift register off.
0xAA = 10101010       This turns every-other output on.

So, now, all you have to do is connect each of the 8 shift register outputs to one of the segments on your display. (Remember to connect the display to ground as well.)

The datasheet you submitted is for a common anode part. If this was a typical 7-segment display, the way it is usually used is that the commone anode, pins 1&5, are connected to 3.3V or to 5v. Then a current limiting resister of say 330 ohm is connected from each of the segment leads to an output pin on the arduino (or, to a shift register output pin). When the pin is low, the segmet will turn on. When the output is high, it will turn off (as both sides of the LED are now high).

However, your part has 3 LEDs per segment, so it will need a higher voltage, the spec sheet shows 6-7.5. For that you will need a part that can withstand a higher off voltage and not be damaged, such as a tpic6b595 open drain shift register, which can take 50V. Or, put an NPN transistor at the bottom of each cathode, which is basically what the output of the tpic6b595 has.

There are some other open collector/open drain parts as well.

I'm sorry, but that last explanation went a bit over my head. I think I understand how the shift register works. I'd still have to figure out how to convert the numbers that have been calculated into signals to be sent to the shift register though.

So I won't be able to power the 7-segment displays with the Arduino no matter what? That's fine, but the diagram confused me. Is there 2 shift registers per digit or are those the digits themselves? I think I see transistors there, so that would make them seem like the digits, right? Can all 8 be hooked up off that 18 volt power supply? Idk what the D arrow things are and I'm still working on calculating the resistance there. Sorry for all the questions. I'm trying to get it.

Also I believe the Arduino mega has only 4 (or is it 3) transmitting serial pins. Is there a way around this? I know that Sparkfun has a 4 digit display hooked up to a single serial output, but I wanted larger digits.

Here is the data sheet for the 7-segment displays: http://www.us.kingbright.com/images/catalog/SPEC/SA18-11EWA.pdf

Those LEDs take over 7.5V to drive - do you realise this? The common anodes will require a driver circuit each that takes logic levels up to the appropriate supply voltage (9V would be an obvious choice). LED displays compatible with 5V are much simpler to drive.

Okay, I may have two posts mixed up. But not totally. I had 6 LEDs strings & 18V supply on the mind from something else.

Did you look at the internal schematic for your part? The segments all have 3 LEDs in series. 5V from an arduino will not be enough to turn them on. You will need to apply a voltage of >6V to pins 1&5 for all the digits, then individually apply a logic low (close to 0V) to turn an individual segment on.

When a segment is not on, the bottom of the last LED will go up to whatever voltage you are suppying them with and blow up the output of the arduino or standard shift register if that is above 5V.

To turn the segments on, you will use the shiftout command with 1s & 0s. The shiftout command looks like this shiftOut(shiftdataout, serialclock, MSBFIRST, outdata); where shiftdataout is the pin that data is coming from on the arduino. serialclock is the clock line from the arduino, MSBFIRST indidatces bit 7 goes first, and outdata is the 8 bits you want sent out.

If for example your shift register bits were wired like this: Output 0 = segment A O1 = B O2 = C O3 = D O4 = E O5 = F O6 = G O7 = decimal point

Thus to send out a "1" character, you set outdata to (bit # / segment -on/off) 0/a-off 1/b-on 2/c-on 3/d-off 4/e-off 5/f-off 6/g-off 7/dp-off

outdata would be set to 0b11111001 if you had a shift register that could handle >5V on its outputs, such as the tpic6b595. The 2 zero bits would result in low outputs for O2 & O3 to turn on segments B & C. On the other hand, if you used a standard shift register and added transistors to its outputs to control the LEDs instead, such as the last schematic I posted (where I showed 6 LEDs instead of 3 by accident), then you would shift in 1's to turn on the transistors; outdata would be set to 0b00000110. The two 1 bits would turn on the 2 transistors for segments B & C and turn them on.

You will need 1 shift register per digit. The >6V supply will go to pins 1&5 on all the digits.

Another option is to put a part like an 74LS47 in there, which can work with up to 15V. A shift register could supply 4 bits to each of two 74LS47s. The '47s will decode the 4 bits into 15 characters (plus a blank state), so you coud just shift out a "1" and it will show up as 1, "2" for a 2, etc. and hex "f" to blank the display. However, you are stuck with the decode that is built in. If you wanted characters that looked like some letters (E, h, P, n, L, A, J, C or c, etc.) you're better off with the shift register so you have more control.

But I'd need to use the serial outputs one per digit if I used shift registers right?

  1. You could shift out to each shift register individually (have the data line and the clock line common to all parts, with unique load or enable lines). Takes 10 arduino output lines.
  2. You could have the data shift thru all the parts (output becomes the input to the next) and then shift out the 64 bits of data for all of them every time. Uses less pins that way. Takes 3 output arduino lines.
  3. Another option is to use a MAX7221, which combines both these approaches - it has additional logic so that you are only shifting out to 1 part (uses the SPI format, ties up 4 specific output pins, D10-11-12-13). The registers are individually addressable (you shift out an address and then the data for that address). However, it won't do 6-7.5v outputs. You would have to add two sets of transistors - one set to switch on the 7.5V to each digit's common anode, and one set to switch the common segments lines to ground. See Figures 1 & 4 in this application note: http://www.maxim-ic.com/app-notes/index.mvp/id/1196

So an output line would be a pin? If I don't need to use the serial pins for this then I don't mind taking a lot of output pins since I'm using an Arduino Mega. 10 pins would be fine, but if you need to use a serial pin to send data to the shift register then I don't believe there is enough on the Arduino Mega.

Thanks a ton for your help btw!

You will not be using the Serial Rx/Tx lines, just regular I/O lines. Define a pin as the common data output line. Define a pins as the common shift clock line. Define 8 pins as the unique Strobe, or Load, or whatever it is called for the 595 variant that you are using.

Glad to help. Talking others thru these helps me get things clear in my mind for my designs as well. I don't have any shift registers handy, so I am wiring up a 74F374 as shift registers, which also comes in handy for driving (single) LEDS per output (with current limit resister) directly.

So would 4 AA or similar batteries in series be a suitable power source? I saw the voltage is typically 6 volts per segment.

That would have a ~3 A current? So then a resistor of roughly 2 Ohm? That can't be right.

So then I have a wire going from each of the segments to the shift register (which pins of the register?). I have one wire going from the Arduino to the shift register. I have a common ground that would connect to the battery, the 7 segment, and the Arduino. Could I get away with a resistor only between the battery and the 7-segment display? Is that how it is connected?

4 AAs would probably be marginal, as soon as they dropped a little you'd be out of voltage. 5 would be okay. You could power the arduino from batteries 1-3 (4.5V into VCC, not Vin), and the segments from batteries 1-5 (7.5v). I have used 3 AAs like this, the arduino was still going well at 4.25V. So if you had 5 batteries, thats 7.5V, less say 6.5V for a segment, that's 1V across the resistor and transistor of the shift register, so you'd see maybe 0.5V across the resister. So with ohms law, V=IR, V/I = R => 0.5V/0.02A = 25 ohms. 27 or 33 are standard values. You need a resister per segment of each LED if you are using shift registers. More below on why 1 resister per segment. If you had all segments on in 1 digit, that would be 140mA. How many digits were you planning on? I don't recall.

You have 3 wires going from the arduino to the shft registers, assuming you have the output of one shift register feeding the input of the next. The shift_out_clock goes to each part. The shift_data_out goes to part1, its output goes to part 2, etc. To prevent a bunch of flickering while all this shifting is going on, you also have a load_data line that moves the data from the input shift register to the output drive pin so all the displays update at once. Then you shiftout bytes until all digits are shifted, and toggle the load line to update the outputs. The other option is to use more wires, and shift to each digit individually. Have a common shift_data_out, shift_clock, but individual load_data to each part for the actual output update.

Wire up your shift register - which one are you using? tpic6b595 open drain shift register, which can take higher output voltages (the voltage will go up to the battery voltage when the pin is high)? Or a standard TTL/CMOS part, in which case you will need an NPN transistor like I showed in an earlier post. Select the mapping you want to use, maybe this: bit 7 = decimal point bit 6 = segment G bit 5 = F bit 4 = E bit 3 = D bit 2 = C bit 1 = B bit 0 = segment A Then to turn on a segment, a bit = LOW will equal a segment turned on for the tpic6b595, or if the shift register drives a transisor as shown earlier, a bit = HIGH will equal a segment turned on. Lets go with HIGHs: for #1, data out for a shift register would be 0B00000110 for segments B & C for #0, 0B00111111 for segments A,B,C,D,E,F for #7, 0B00000111 for segments A,B,C and so on. for L, 0B00111000 for segments D,E,F, and so on.

You have 7.5V going to the common ANODE of each digit, the segments go thru resistors to the shift register, the battery- goes to the arduino ground. You could try just 1 resister between the 7.5V and the ANODE of each digit, but you will find the segments changing brightness as the different segments turn on & off. 1 or 2 segments vs 5,6,7 segments all sharing the same 20mA, you see? And you have to limit for the least amount of segments on so you don't burn it out.

I will be using 8 digits. There will be two different 4-digit numbers, so essentially two different displays, so I think it'd be easier as far as programming is concerned to wire those two separately?

I have the 9 volt pack thing for the Arduino.

So I think I follow all that. I may just wire each individually, but idk which would be easier to code. I have a keypad that the user inputs numbers on and I want to display the user's input on the display one number at a time, so they know that their input has been registered. I also need to simply display 4 digit numbers arrived at in different parts of the code as well. Whichever wiring would work best for that would be good.

At this point I supposed I'll just need to translate the input and calculated digits into outputs for the display. With the user input I could have several if then statements that say, for example, if button "1" is pressed then output for #1, data out for a shift register would be 1B11111001 for segments B & C, but that seems a little sloppy.

Then the other portion will be converting say 3097 into data to be sent to the shift register.

I was planning on using the tpic6b595 open drain shift register you suggested.

"9 volt pack thing" what is that? The 9V wallwart?

With the mega, you have plenty of IO, you can have separate control pins for each shift register.

Take a look at this code. I totally do not get how the first highlighted part sets up the shift register for the second highlighted part to display digits, but am told it works.

unsigned long currentmillis = 0;
unsigned long previousmillis = 0;
unsigned long interval = 10;

int latchpin = 8; // connect to pin 12 on the 74HC595
int clockpin = 12; // connect to pin 11 on the 74HC595
int datapin = 11; // connect to pin 14 on the 74HC595

int ones_seconds = 0;
int tens_seconds = 0;
int ones_minutes = 0;
int tens_minutes = 0;
int tenths = 0;
int hundredths= 0;

[glow]int segdisp[10] = {
  63,6,91,79,102,109,125,7,127,111 }; //segment references using 74HC595 Shift Registers
//The above numbers light up different segments of a digit[/glow]
int time_update = 0;// added new flag
void setup()
{
  pinMode(latchpin, OUTPUT);
  pinMode(clockpin, OUTPUT);
  pinMode(datapin, OUTPUT);
}

void loop()
{
  currentmillis = millis();  // read the time.
  if (currentmillis - previousmillis >= interval) // 10 milliseconds have gone by
  {
    previousmillis  = currentmillis;  // save the time for the next comparison

    time_update = 1;  
  }  // set flag to upate & shift out

  if (time_update == 1){  // no updating if not at 10ms interval, skip this whole section
    // increment the counters, roll as needed, shift the digits out
    time_update = 0; // reset for next pass thru

    hundredths = hundredths +1;
    if (hundredths == 10){
      hundredths = 0;
      tenths = tenths +1;
    }

    if (tenths == 10){
      tenths = 0;
      ones_seconds = ones_seconds +1;
    }

    if (ones_seconds == 10){
      ones_seconds = 0;
      tens_seconds = tens_seconds +1;
    }

    if (tens_seconds == 6){
      tens_seconds = 0;
      ones_minutes = ones_minutes +1;
    }

    if (ones_minutes == 10){
      ones_minutes = 0;
      tens_minutes = tens_minutes +1;
    }
    if (tens_minutes == 10){
      tens_minutes = 0;
    }

[glow]    // counters are all updated now, just do the shiftout one time here:
    digitalWrite(latchpin, LOW); // send the digits down to the shift registers!
    shiftOut(datapin, clockpin, MSBFIRST, segdisp[hundredths]); // print the % first "hundredths" digit
    shiftOut(datapin, clockpin, MSBFIRST, segdisp[tenths]); // print the tens of hundredths digit
    shiftOut(datapin, clockpin, MSBFIRST, segdisp[ones_seconds]); // print the % first "seconds" digit
    shiftOut(datapin, clockpin, MSBFIRST, segdisp[tens_seconds]); // print the tens of seconds digit
    shiftOut(datapin, clockpin, MSBFIRST, segdisp[ones_minutes]); // print the % first "minute" digit
    shiftOut(datapin, clockpin, MSBFIRST, segdisp[tens_minutes]); // print the tens of minutes digit
    digitalWrite(latchpin, HIGH);[/glow]
  } // end if time to be updated

} // end void loop

http://www.sparkfun.com/products/734

Is this the shift register you recommend?