bclose

38 – 1 digit 7 segment displays

Objectives

 
    • Our first 1 digit 7 segment display.
    • More on arrays.
 

Bill of materials

Arduino UNO

 Arduino Uno or equivalent.

This chapter accepts any other Arduino board.

 

Breadboard

 A solderless Breadboard.

Jumper wires

Some jumper wires.

330 Ohm resistor

 A 330 Ω resistor.

1 digit 7 segment display  A 1 digit 7 segment display.

 

LED displays

 

As you begin to assemble a prototype that performs any practical function, there is a 50% chance that you end up needing to send information to the outside world (and of course you are not going to leave your laptop stuck to the gadget).

So it’s time to see what offers the market to display information and just to start, nothing better than a simple 1 digit 7 segment LED display.

LEDs no longer hold secrets for those who follow us, but this one instead of being a display seems to be something else. Well, not at all, it is the same but instead of being a luminous dot, as the familiar LED diodes, it has a square shape, in fact, it contains 7 bars or segments plus 1 dot point.

These displays are quite familiar to us and we can see them in alarm clocks and the like. They are formed by 7 bars or segments and can show the digits from 0 to 9, depending on which segments are turned on.

Each of the 7 segments are given a letter to represent them, usually a, b, c, d, e, f, g apart from the point (or two points depending on the manufacturer), and it is a good time to take a look at the specifications of each manufacturer to make sure which pin is which.

All circuits, displays included, have a manufacturer’s code labelled on them. It is good policy to search in internet the specification sheet (or data sheet) and take a look. If you can not understand anything, it is normal for the moment, but certainly there will be a drawing explaining the meaning of each pin. I searched the model I am going to use, (one Kingbright SC08-11HWA), but do not trust, yours may be different.

Chapte 38, 1 digit 7 segment display information
 
  • Since there is Internet, to find a data sheet is trivial (not like before, to get them you had to ask favors and show signs of respect and submission to those who owned those valuable catalogues).
  • Try to search in Google something like XXXXX data sheet, where XXXXX are the codes that you can find labeled on the display or component..  

The important thing is to know the meaning of each pin, and to be able to know which is each segment. It is actually much easier than it seems, first locate one GND pin. In the above picture these are the pins 3, 5, 11 and 16.

Connect a resistor (a 330 Ω resistor as always) from the display output to GND. This is a common cathode display, meaning that all LEDs share the mass, but we need to put a resistor to limit the current intensity or you will burn out the display, and this costs enough to see it funny.

Once you have connected the common mass to the resistor, start wiring only one bar. For example, connect the a pin to the pin 2 of the Arduino and write an Arduino sketch to turn the a bar using the pin 2 and see how the segment is turned on.

You can keep on wiring and testing the rest of the pins slowly, ensuring the connections. It is very easy to make mistakes if you don’t walk gingerly.

 

We are going to use the 5161AS 1 digit 7 segment display whose pin-out and diagram is the following:

Chapter 38, 5161AS - 1 digit 7 segment display pinout and diagram

In order to know which is the pin 1, set the display so that the decimal point be placed down at your right. The pin number 1 is at the bottom, the first on the left, then keep counting in the opposite direction clockwise.

7 segment LED display pinout

 

 

CIRCUIT SCHEMATIC DIAGRAM

 

Chapter 38, Schematic diagram

The circuit can’t be easier. It is like the one we made using 8 diodes, but this time the 8 diodes are in a box and arranged to display numbers.

 
  • It is important that you follow the scheme above for the sketch to match your connections.
  • So it doesn’t matter which pin of your display is the C segment (which depends on the manufacturer and model), but it is important that the C segment be connected to Arduino digital pin 4 and the same for the rest. 

We will not include the circuit wiring diagram, because it is difficult to make it clearer.

Control program

 

First we need to check that all connections are okay. Try this:

Sketch 38.1
void setup()
   {
        for (int i = 2; i<11; i++)
        pinMode(i, OUTPUT);
   }

void loop()
   {
        for ( int j= 2; j<11 ; j++)   // Turns on the segments
           {
              digitalWrite(j, HIGH);
              delay(400) ;
           }
        for ( int j= 2; j<11 ; j++)   // Turns off all the segments
              digitalWrite(j, LOW);
        delay(400) ;
   }

Inside the setup() function, we initialize the 8 output pins using a for loop, remember that we are lazy. Later in the loop() function, we use a first for loop that will turn on the 8 segments with delay, then the second for loop turns them off and back again. The idea is to check that all the connections are okay. You will see something like this:

The following is to show the digits from 0 to 9.

To do this, the faster is to make a table to see which segments have to be turned on for each decimal digit, and then you write 1 (on) or 0 (off) according to it.

  DIGIT a b c d e f g point
Chapter 38,7 segment display segment numbering
0 1 1 1 1 1 1 0 0
1 0 1 1 0 0 0 0 0
2 1 1 0 1 1 0 1 0
3 1 1 1 1 0 0 1 0
4 0 1 1 0 0 1 1 0
5 1 0 1 1 0 1 1 0
6 1 0 1 1 1 1 1 0
7 1 1 1 0 0 0 0 0
8 1 1 1 1 1 1 1 0
9 1 1 1 0 0 1 1 0
 
  • Although we give you the homework done, do the test with one of the digits, simply to assure that you understand the idea
 

Now we have just to write the sketch that draws the numbers on the display, so let’s go.

THE SKETCH THAT DRAWS THE DIGITS

 

The first idea that comes in to your head is to make a function for each digit we want to represent. For example we could do:

void Display7()
   {
       digitalWrite(2,1) ;        // Yes, we can use 1 and 0 instead of HIGH and LOW
       digitalWrite(3,1);
       digitalWrite(4,1);
       digitalWrite(5,0);
       …………..
   }

That is, we turn on the pins corresponding to each segment of the previous table.

The problem with this approach is that we have to do 10 functions like the previous one and then write something in the main program to call the corresponding function according to the number you want to show.

This is, again, the brute force solution and no, here we can’t allow these things, we like smart solutions (and besides we are very lazy and we get tired just thinking about doing it that way).

 
  • By the way, to be someone in the freaky community, we must talk about the elegance of the code whenever the occasion arises, if you really want to be a developer is very important that you begin to take the poses of the guild.
 

So let’s find a more elegant solution. For a start, everyone who saw the last table, showing the segments that have to be turned on, and thought that it looked suspiciously like an array of 10 x 8 can score a point.

We will create an array with all that, of course (no, we have no commissions for selling arrays) and you will see that the program simplifies a lot.

Again, as with everything in life, it is much easier than it seems. Let’s start by defining the two-dimensional array (because it is a table, another thing is the number of elements per dimension):

byte  Digit[10][8] =                // Arduino UNO has not very much flash memory. 
{                                   // That's why we define a byte array instead of an int array
   { 1,1,1,1,1,1,0,0 },    // 0   
   { 0,1,1,0,0,0,0,0 },    // 1
   { 1,1,0,1,1,0,1,0 },    // 2
   { 1,1,1,1,0,0,1,0 },    // 3
   { 0,0,1,0,0,1,1,0 },    // 4
   { 1,0,1,1,0,1,1,0 },    // 5
   { 1,0,1,1,1,1,1,0 },    // 6
   { 1,1,1,0,0,0,0,0 },    // 7
   { 1,1,1,1,1,1,1,0 },    // 8
   { 1,1,1,0,0,1,1,0 }     // 9
};

If you look carefully, I have copied directly the table display digits, but in an array of 10 elements, each of which is an array of 8 (segments). The syntax is more or less complicated, but that’s all and we had already done it before.

We can now create a function that selects the row of the array based on a number from 0 to 9, that we have previously passed, and finds the appropriate internal array.

void Display(int N)
   {
       for (int i= 0 ; i<8 ; i++)
            {   
                int value= Digit[N][i];
                int pin = i+2;
                digitalWrite(pin , value);
            }
   }

We look for a value in the table whose main index, N, points to the corresponding internal array. The for loop reads the 8 elements of the array to be written to the corresponding output pin. We calculate the iteration variable as i +2 because we use the pins from 2 to 10 to connect the display segments and thus HIGH values from pins 0 to 8 pins become the numbers from 2 to 10.

Sketch 38.2
void setup()
   {   
        for (int i= 2; i<11; i++)
            pinMode(i, OUTPUT);
   }

void loop()
   {
       for ( int k=0 ; k<10 ; k++)        // Calls Display() giving k values from 0 to 9 to show the digits
         {  
             Display(k);
             delay(1000);
         }
   }

void Display(int N)
   {
      for (int i= 0 ; i<8 ; i++)
           {   
               int value= Digit[N][i];
               int pin = i+2; 
               digitalWrite(pin , value);    // digitalWrite(i+2 , Digit[N][i]);
           }
   }

Notice that we could have written this way (The compact code also scores with your mates):

void Display(int N)
   {      for (int i= 0 ; i<8 ; i++)
               digitalWrite(i+2 , Digit[N][i] ) ;
   }

I used this example to insist with arrays, because they are very useful for solving problems that otherwise may be complicated enough.

A problem that a priori, was very heavy, or complex, becomes a trifle looping through the table. I have seen several examples of this program online and most of them gave me a headache (with some very honorable exceptions).

The thing went quite well using only one digit, but … what if we want to show 4 digits? Are we going to multiply 8 cables x 4 digits plus voltage and GND = 34 pins?

In the next chapter we will see how to handle a 4-digit seen segment display, because they are sold assembled, and do not need to go crazy plugging I do not know how many wires to make a watch.

 
  • As this chapter is already dense enough, I will not include the example of connecting a single digit to a shift register to govern it, but I recommend you do it as a practice, and if necessary, we will further hung the example on the website.
 

 

 

Summary

 

 
    • We have introduced the 1 digit 7 segment displays.
    • They are completely normal LED diodes, but with the shape of bars, not just dots.
    • Some of the functions that have already defined will be useful to us in the following chapters to control more kind of displays.
 

 

 

 

No Comments

Give a Reply