## March 29, 2013

### Introducion

In this tutorial I'm going to show you the basics of the working of a 7 segment display, and how to use, when you have 4 in a row. So lets take look:

So that"s it. I ordered it from dx.com half years ago, because it was only \$2, and I was interested in it. But in the beginning it was too hard for me, I just can't make it work. It has 6+6 so 12 pins in the back, and has no sign about them, as here:
So I took it away, and didn't even touch it till this month. We learned about 7 segment displays, and I was wondering, what if I could make my work. So I looked up on the internet, and find, that this display has its own library, here: https://github.com/sparkfun/SevSeg First I just downloaded and tried it, and it worked. So I looked into the code, and started to understand the inner working. So basically this display has 4*8 leds ( because each the number has 7, and there are 4 dots also). How is it possible then to control them only with 12 pins? Don't we need 4*8=32 pins for that? Yes, theoretically we would need 32 pins. But then, how does it works? The trick is, that we don't need to light up all lightning led constantly. If they blink very fast, we will see as if they're lighting. So there is 4 pin for specifying the number, and then whit the rest 8 pins we can control the 8 leds of the number. And if we change between the numbers very fast, nobody will be able to see the blinking. And changing it very fast is not a problem, when we are using arduino.

### Inner working

Okay, you can say, but how is it possible? I mean, how does it work? It's quiet simple, using the special attribution of the led, that it's a diod, so the current can flow only in one direction. So here is  how to show a 5 in the 3. place:
(Red lines are HIGH, blues are LOW, because my display is common anode type. If yours is common cathode, just change every HIGH to LOW and )So as you can see 8 of the 12 pins is connected to each led, but in every number. The other 4 pins are connected to the other side of the leds but one by every number. The led will show up, if it get a red line (HIGH) from the top and a blue line (LOW) from the button. Otherwise it stays dark. Try to understand this part, because many-many things works in this principle like led cubes, and matrix displays

### Using it

The pins are numbered this way:
And connect these pins of the display to those pins of the arduino:

```   //Declare what pins are connected to the digits
int digit1 = 2; //Pin 12 on my 4 digit display
int digit2 = 3; //Pin 9 on my 4 digit display
int digit3 = 4; //Pin 8 on my 4 digit display
int digit4 = 5; //Pin 6 on my 4 digit display

//Declare what pins are connected to the segments
int segA = 6; //Pin 11 on my 4 digit display
int segB = 7; //Pin 7 on my 4 digit display
int segC = 8; //Pin 4 on my 4 digit display
int segD = 9; //Pin 2 on my 4 digit display
int segE = 10; //Pin 1 on my 4 digit display
int segF = 11; //Pin 10 on my 4 digit display
int segG = 12; //Pin 5 on my 4 digit display
int segH= 13; //Pin 3 on my 4 digit display```

After that we can control the display from the arduino. For this here is my code:
(NOTE: I've used Arduino MEGA for this tutorial and connected the display to pins 22-33, so if you use an UNO, change it as shown under)

```boolean show[4][8];

void setup()
{
//actually we don't need this part, but it's always good to know the wiring
//Declare what pins are connected to the digits
int digit1 = 22; //Pin 12 on my 4 digit display
int digit2 = 23; //Pin 9 on my 4 digit display
int digit3 = 24; //Pin 8 on my 4 digit display
int digit4 = 25; //Pin 6 on my 4 digit display

//Declare what pins are connected to the segments
int segA = 26; //Pin 11 on my 4 digit display
int segB = 27; //Pin 7 on my 4 digit display
int segC = 28; //Pin 4 on my 4 digit display
int segD = 29; //Pin 2 on my 4 digit display
int segE = 30; //Pin 1 on my 4 digit display
int segF = 31; //Pin 10 on my 4 digit display
int segG = 32; //Pin 5 on my 4 digit display
int segDP= 33; //Pin 3 on my 4 digit display

for(int i=22; i < 34; i++) pinMode(i, OUTPUT);

//adding the text or numbers
for(int i=0; i < 4; i++){for(int j=0; j < 8; j++) show[i][j]=true;} //if true: the led is dark, if false the led shows up
//A
show[0][0]=false;
show[0][1]=false;
show[0][2]=false;
show[0][4]=false;
show[0][5]=false;
show[0][6]=false;
//n
show[1][2]=false;
show[1][4]=false;
show[1][6]=false;
//n
show[2][2]=false;
show[2][4]=false;
show[2][6]=false;
//a
show[3][2]=false;
show[3][3]=false;
show[3][4]=false;
show[3][6]=false;
show[3][7]=false;

}

void loop()
{
for(int i=0; i < 4; i++) //run on the digits
{
for(int j=0; j < 8; j++) digitalWrite(j+26, true); //turn off every segment
digitalWrite(22+i, true); //turn the actual number on
digitalWrite(22+(i+1)%4, false); //and
digitalWrite(22+(i+3)%4, false); //the rest

for(int j=0; j < 8; j++)
{
digitalWrite(j+26, show[i][j]); //turn the specified segments on as defined in the array show
}
delay(1);
}

}```

And the same code for arduino UNO:

```boolean show[4][8];

void setup()
{
//I've deleted the unnecessary part from here
for(int i=2; i < 14; i++) pinMode(i, OUTPUT);

//adding the text or numbers
for(int i=0; i < 4; i++){for(int j=0; j < 8; j++) show[i][j]=true;} //if true: the led is dark, if false the led shows up
//A
show[0][0]=false;
show[0][1]=false;
show[0][2]=false;
show[0][4]=false;
show[0][5]=false;
show[0][6]=false;
//n
show[1][2]=false;
show[1][4]=false;
show[1][6]=false;
//n
show[2][2]=false;
show[2][4]=false;
show[2][6]=false;
//a
show[3][2]=false;
show[3][3]=false;
show[3][4]=false;
show[3][6]=false;
show[3][7]=false;

}

void loop()
{
for(int i=0; i < 4; i++) //run on the digits
{
for(int j=0; j < 8; j++) digitalWrite(j+26, true); //turn off every segment
digitalWrite(2+i, true); //turn the actual number on
digitalWrite(2+(i+1)%4, false); //and
digitalWrite(2+(i+3)%4, false); //the rest

for(int j=0; j < 8; j++)
{
digitalWrite(j+6, show[i][j]); //turn the specified segments on as defined in the array show
}
delay(1);
}

}```

If it runs perfectly you should see "Anna" on the screen, like shown here:

You can control the text with changing the array show. For example for the first character "A" we need 6 segment from the first display: a,b,c,e,f,g. So because it's the first number, we change show[0][*] and because a=0, b=1, c=2, e=4, f=5, g=6, so

```   //A
show[0][0]=false;
show[0][1]=false;
show[0][2]=false;
show[0][4]=false;
show[0][5]=false;
show[0][6]=false;```
I think it's not so complicated :)

### Deeply into the case

As you see, it use 12 pins, which actually all of the digital pin of a normal arduino UNO. So what can we do? If you think about it, there is a redundant information in the system, because every time only one of the 4 digit-pin is HIGH, so it can be controlled with a demultiplexer (aka demux). It works so:
So I0 is connected to Vcc, and F0-F3 to the 4 digit pin of the display. So now if you want a HIGH on F0, you only have to give LOW and LOW on S1 and S0, for F1 LOW and HIGH, for F2 HIGH and LOW and for F3 HIGH and HIGH. So in the end you save 2 pins, which are not the word, but sometimes can be very helpful.

I hope this tutorial was helpful, if you have any question, please feel free to write or comment

Best regards:

Mark

1. Looks good away to work through it now. Only questions, shouldn't you have resistors inline with the leds?

1. Yes, theoretically I should, but it worked this way too.

But if you would use it for hours, you should definitely use resistors.

Mark

2. I don't understand the wiring. Can you help ?

1. Oups, sorry, I wasn't clear. For numbering the pins of the display use this image: http://1.bp.blogspot.com/-JN2WLnrPo7w/UVYMPei-oyI/AAAAAAAAAIY/afge-t4ZyWw/s1600/7segment2.png
And for the corresponding pins on the arduino, see this part of the post:
//Declare what pins are connected to the digits
int digit1 = 2; //Pin 12 on my 4 digit display --> it means connect the 12th pin of the display to the 2nd pin of the arduino
int digit2 = 3; //Pin 9 on my 4 digit display
int digit3 = 4; //Pin 8 on my 4 digit display
int digit4 = 5; //Pin 6 on my 4 digit display

//Declare what pins are connected to the segments
int segA = 6; //Pin 11 on my 4 digit display
int segB = 7; //Pin 7 on my 4 digit display
int segC = 8; //Pin 4 on my 4 digit display
int segD = 9; //Pin 2 on my 4 digit display
int segE = 10; //Pin 1 on my 4 digit display
int segF = 11; //Pin 10 on my 4 digit display
int segG = 12; //Pin 5 on my 4 digit display
int segH= 13; //Pin 3 on my 4 digit display

Is it ok right now? If anything still unclear, feel free to ask :)

Mark

3. Hello, can you make it count up too?

1. I made a modified version that counts, It handles pins differently as well.

https://github.com/xt23/7-seg-4-dig-timer/tree/master

4. Hello,
of course, that would just need some arduino code, like this:

(..) //all previous part
int number=0;
loop()
{
//original part stays untouched

//change the show array regarded to the number (aka say to it which leds should be on by a 1...)

number++;
}

Sorry, now I don't have more time, to specify it more. If it doesn't enough or doesn't work, feel free to ask again :)

Mark

5. this helped me understand how to use the 7 segment . thanks a lot :)

6. How do you transform this to work with common cathode displays?

1. Reverse the booleans in the beginning of loop():

void loop()
{
for(int i=0; i < 4; i++) //run on the digits
{
for(int j=0; j < 8; j++) digitalWrite(j+26, false); //change it to false
digitalWrite(22+i, false); //and here also
digitalWrite(22+(i+1)%4, true); //to true here
digitalWrite(22+(i+2)%4, true); //and here
digitalWrite(22+(i+3)%4, true); //and here

for(int j=0; j < 8; j++)
{
digitalWrite(j+26, !show[i][j]); //either reverse the array, or just negate here
}
delay(1);
}

7. Sir, I am Satyaban and stay in India where proper guidance on Arduino is rarely available. Last year, I made a project (digital clock)
with Arduino Uno and 4 digit seven segment (12 pins) display (common anode) which ran successfully, But recently I found that
the project is not working with the same code and Board. Can I get any help please? Regards.

1. In my experience it is usually some hardware issue, namely miss-connected wires or similar. Can you check those? Sometimes removing all wires and reconnecting them can help