IAN LANG ELECTRONICS

Arduino Project No 2

The 7 segment LED display is one of the handiest things you can have in your repertoire of electronic know-how as it can represent all the digital numbers 0-9 in a highly visible form. It looks rather like this:

Go Back

Driving a 7 Segment LED Display

As you can see, each segment is a bar-shaped LED and is given an identifying letter in the range a-g.  On some there is a decimal point to the right hand side too. The devices come in two general sorts; common anode and common cathode. For our purposes, it is easier to use a common cathode than a common anode, and you can buy these from Maplin:

 

 

 

The common cathode is code FR41, and if you look under the media tab on the Maplin page above you will find the Kingbright datasheet in PDF format.

www.maplin.co.uk/12.7mm-0.5-inch-single-digit-numeric-display-2157

Lighting chosen LEDs and keeping others dark means that we are able to form all the numbers, and some basic letter shapes too. The sense is shown above.  Below are the specs for the Kingbright half inch single digit segment from Maplin:

 

As can be seen, there are ten pins on the segment. Pins 1 to 5 run from left to right across the bottom, pins 6 to 10 right to left across the top, and to the right is a pin out diagram. DP is decimal point, and the cathode can be either pin 3 or pin 8 or both, whichever is the most convenient. We now intend to test this on the breadboard, and assuming you have the same segment, the pins of your Arduino and the pins of your segment are connected together as detailed below. If you have a different segment, it may be the same pinout but refer to your datasheet first.

You can put either 3 or 8 to GND but if you connect it directly the segment shines very brightly and runs hot, which will shorten its  life considerably. I ran to GND via a 560R resistor as supplied in the ARDX kit and it produced a highly visible display and ran without generating heat.

 

Having built your circuit here is the code to run it:

 

int uparray []={0,1,1,0,0,0,0,1,1,0,1,1,0,1,1,1,1,1,0,0,1,0,1,1,0,0,1,1,1,0,1,1,0,1,1,1,0,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,0};

 

void setup () {

  for (int t=2;t<9;t=t+1){

  pinMode (t,OUTPUT);}

 

}

 

 

void loop (){

int k=0;

  for (int t=0;t<10;t++){

  for (int j=2;j<9;j++){

    digitalWrite (j, uparray[k]);

k=k+1;  

}

    delay (1000);}

       

}

 

Assuming all has gone well, what should happen is that the segment should show all the digits in order from 1 to 0 and then begin again.

This code comprises a technique not used before in these pages. Previously, if we wanted to write a pin digitally high or low, we used :

 

digitalWrite (pin,HIGH);

 

or

 

digitalWrite (pin,LOW);

 

However, there is another way. We could write:

 

digitalWrite (pin,1); instead of digitalWrite (pin,HIGH);

 

and

 

digitalWrite (pin,0); instead of digitalWrite (pin,LOW);

 

and this gives rise to the possibility of using a variable with the value of 0 or 1 assigned to it to write the pins low or high. This keeps our code very small, and is a good practice. This is exactly what  I have done by filling the array uparray with 70 digits which are either zero or one. Every seven digits represents the LEDs on the segment which are to be lit (1) or kept dark (0) and if you spread them out they look like this:

 

int uparray []={

0,1,1,0,0,0,0,

1,1,0,1,1,0,1,

1,1,1,1,0,0,1,

0,1,1,0,0,1,1,

1,0,1,1,0,1,1,

1,0,1,1,1,1,1,

1,1,1,0,0,0,0,

1,1,1,1,1,1,1,

1,1,1,1,0,1,1,

1,1,1,1,1,1,0

};

 

Taking two lines as an example then:

 

 

0,1,1,0,0,1,1,                                                 1,1,1,0,0,0,0,

 

The testpiece showing a digit. The wiring diagram for the circuit can be found here:

And so as you may have gathered, the zeroes and ones in a set of seven represent the segments a,b,c,d,e,f,g . You'll notice that it's all ones when it comes to number 8- all the segments get switched on.

The code that causes it to count upwards is:

 

int k=0;

  for (int t=0;t<10;t++){

  for (int j=2;j<9;j++){

    digitalWrite (j, uparray[k]);

k=k+1;  

}

    delay (1000);}

 

Going through line by line, we find:

int k=0;

 

The variable k is going to be the counter that tracks position in the array uparray. It starts at position 0, the beginning.

 

for (int t=0;t<10;t++){

 

This line causes the following code to be executed ten times (as there are ten digits 0-9).

 

   digitalWrite (j, uparray[k]);  

 

This line sets the current pin considered (which depends on the value of j and could be 2,3,4,5,6,7,8,) to the value of the kth position of upparay; either a zero or one in which cases it will be low or high respectively thus lighting the segment that the pin is attached to.

 

k=k+1;  

 

In this line, every time the inner loop completes a cycle, k is incremented by 1, and as there are seven cycles in the inner loop, it goes up by 7. The outer loop has ten positions, and so by the time the outer loop has finished its last cycle, it has gone up by 70. In other words, the outer loop controls what digit is displayed, and the inner loop controls which segments are going to be lit to display it, which it chooses from the seven sets in uparray.

 

The last line:

 

 delay (1000);}

 

Sets a 1 second delay so the change is visible and closes the outer loop. The function void loop ( ) then begins all over again.

 

There is one more thing to note:

 

void setup () {

  for (int t=2;t<9;t=t+1){

  pinMode (t,OUTPUT);}

 

}

 

Hereto we have not been overconcerned with pin modes. But now we need to set pins 2-8 (to which the segments are attached) as output pins. If we don't, they won't supply enough current to light the segments.

 

As fascinating as this is it's not terribly practical. Over the page, let's turn it into another dice.

 

More> Display Circuit