Saturday, May 23, 2015

Intro to seven segment displays

Display Numbers

There are many occasions when it is useful to display numbers from an arduino.  This tutorial will cover how to use a 4 digit seven segment display.

Components

For this tutorial I will be using a display from spark fun. It has 4 segments with a decimal point for each number.  If you don't have access to this device then your wiring might be a little different but the software is the same.  Seven segments are labeled as shown in this diagram:

Seven segment - from Wikipedia

Software

The first thing to do is create a method for displaying single digits.  This takes a bit of time and is repetitive but it is good practice to figure out how the segments work.  To ensure that the code can work with any wiring we will use the letters for the variables rather than specific pins.

Numbers

Before we start programming anything have a glance at the datasheet which will show how the LEDs are connected.  Notice that the letters are connected to the positive end and the negative end is connected to the segment (DIG1...).  This means that we want to turn the letters high when we want to turn on a segment.  We have to turn the other segments low otherwise everything will stay lit up!

To start with declare the letters a-g to be specific pins on the arduino.  Then use the code below to light the display up with specific numbers.

void zero(){
  digitalWrite(a,HIGH);
  digitalWrite(b,HIGH);
  digitalWrite(c,HIGH);
  digitalWrite(d,HIGH);
  digitalWrite(e,HIGH);
  digitalWrite(f,HIGH);
  digitalWrite(g,LOW);
}

void one(){
  digitalWrite(a,LOW);
  digitalWrite(b,HIGH);
  digitalWrite(c,HIGH);
  digitalWrite(d,LOW);
  digitalWrite(e,LOW);
  digitalWrite(f,LOW);
  digitalWrite(g,LOW);
}

void two(){
  digitalWrite(a,HIGH);
  digitalWrite(b,HIGH);
  digitalWrite(c,LOW);
  digitalWrite(d,HIGH);
  digitalWrite(e,HIGH);
  digitalWrite(f,LOW);
  digitalWrite(g,HIGH);
}

void three(){
  digitalWrite(a,HIGH);
  digitalWrite(b,HIGH);
  digitalWrite(c,HIGH);
  digitalWrite(d,HIGH);
  digitalWrite(e,LOW);
  digitalWrite(f,LOW);
  digitalWrite(g,HIGH);
}

void four(){
  digitalWrite(a,LOW);
  digitalWrite(b,HIGH);
  digitalWrite(c,HIGH);
  digitalWrite(d,LOW);
  digitalWrite(e,LOW);
  digitalWrite(f,HIGH);
  digitalWrite(g,HIGH);
}

void five(){
  digitalWrite(a,HIGH);
  digitalWrite(b,LOW);
  digitalWrite(c,HIGH);
  digitalWrite(d,HIGH);
  digitalWrite(e,LOW);
  digitalWrite(f,HIGH);
  digitalWrite(g,HIGH);

}

void six(){
  digitalWrite(a,HIGH);
  digitalWrite(b,LOW);
  digitalWrite(c,HIGH);
  digitalWrite(d,HIGH);
  digitalWrite(e,HIGH);
  digitalWrite(f,HIGH);
  digitalWrite(g,HIGH);
}

void seven(){
  digitalWrite(a,HIGH);
  digitalWrite(b,HIGH);
  digitalWrite(c,HIGH);
  digitalWrite(d,LOW);
  digitalWrite(e,LOW);
  digitalWrite(f,LOW);
  digitalWrite(g,LOW);
}

void eight(){
  digitalWrite(a,HIGH);
  digitalWrite(b,HIGH);
  digitalWrite(c,HIGH);
  digitalWrite(d,HIGH);
  digitalWrite(e,HIGH);
  digitalWrite(f,HIGH);
  digitalWrite(g,HIGH);
}

void nine(){
  digitalWrite(a,HIGH);
  digitalWrite(b,HIGH);
  digitalWrite(c,HIGH);
  digitalWrite(d,HIGH);
  digitalWrite(e,LOW);
  digitalWrite(f,HIGH);
  digitalWrite(g,HIGH);
}

Once you have the code written you can test the program with the segment.

Hardware

The wiring is starting to get more complex.  You should start to have a color scheme for your wires so you know where each wire should be.  Each segment is an LED so it is important to remember your resistors.
Wiring
Once you have completed the wiring change the loop method to call some of the number methods with a delay.

void loop(){
  zero();
  delay(1000); // wait one second
  one();
  delay(1000);
}

You will notice that all of the segments are lit up at the same time.  This is because all of the DIG# pins are connected to ground.  Multiplexing, or displaying different digits will be covered in another tutorial.

Software improvements

It is irritating to have to keep calling individual functions to display certain numbers.  To solve this we are going to create a method which will accept a number as a parameter and select the correct method. 

void digitSelect(int num){
  switch(num){
    case 0: zero();
            break;
    case 1: one();
            break;
    case 2: two();
            break; 
    case 3: three();
            break; 
    case 4: four();
            break;
    case 5: five();
            break; 
    case 6: six();
            break;
    case 7: seven();
            break; 
    case 8: eight();
            break; 
    case 9: nine();
            break;
  }
}


We create a switch statement to select the method based on the number.  A switch statement is the same as a large block of if/else if statements.  The most important thing to remember with switch statements is to include break after the end of the case.



With this implemented we can create a cool counter

for (int i=0;i<10;i++){
 digitSelect(i);
 delay(1000);
}

Place this in the loop and the seven segment will count from 0 to 9!

No comments:

Post a Comment