Quantcast
Viewing all articles
Browse latest Browse all 45

TUTORIAL ON HOW TO COUNT UP WITH A SINGLE 7 SEGMENT DISPLAY

Image may be NSFW.
Clik here to view.

This tutorial will introduce you to a 7 segment display, and show you how to display a number (0 - 9) on a single 7 segment display. We see these displays every day and don't even think about them. These displays can be found on your digital alarm clock, elevator numbers, gas station price changer signs, stove tops, and on microwave oven display screens.

Here is a list of items we will need for this tutorial...

Hardware:

Arduino Uno
A to B USB cable
10 Jumper wires
Bread board
1 single 7 segment

Software:

Arduino IDE software

 

The first thing we must understand is how to identify and turn on 1 segment of a 7 segment display. On the back side of the display we notice 10 pins. If you need to review how to identify each of these 10 pins with their respective letters on the display (A through G) you might want to reference our last tutorial (What is a 7 Segment display? And how do they work?).

Insert the single digit 7 segment into the breadboard (be sure to allow room for your jumper wires).

Image may be NSFW.
Clik here to view.

Select any digital pin on your Arduino Uno (we choose digital pin 2 on the board), and assign it to PIN1 (Letter E) on the segment.

Image may be NSFW.
Clik here to view.

Continue until all pins on the 7 segment, are connected to their respected pins. Since we started with pin 2 on the Arduino Uno, we felt it would be easy to continue with pins 3 through 9 respectively.

Image may be NSFW.
Clik here to view.

Remember pins 3 & 8 are ground. The rest are control signals (5v since we have a common cathode segment). Connect the Arduino Uno to the computer, using the A to B USB cable and open the Arduino IDE software.
Go to Tools > Serial Port and make sure you have selected the proper serial port.
Go to Tools > Board and make sure you have selected the Arduino Uno.
Now it's time to for the code: You can copy the code I've developed below (feel free to play with and edit this code). If you prefer, go ahead and enter your own code.

Here is code #1:

int A = 7;   //Defines all pins on the Arduino Uno board in order of connection.
int B = 6;
int C = 4;  // DOT is pin 5, not used in this example.
int D = 3;
int E = 2;
int F = 8;
int G = 9;
 
byte num0 = 0x3F;  //Hexadecimal format based upon the A-G, 0-9 Chart in excel and the wiring      // of the segment (refer to the on/off table image below).
byte num1 = 0x6;
byte num2 = 0x5B;
byte num3 = 0x4F;
byte num4 = 0x66;
byte num5 = 0x6D;
byte num6 = 0x7C;
byte num7 = 0x7;
byte num8 = 0x7F;
byte num9 = 0x6F;
 
void on(byte num)   // This function turns on the correct pins to display numbers passed to it         // through the variable "num".
{
  int result = bitRead(num, 0);  // Read the first binary entry in num and stores it in result.
 
      if (result == 1)  // Check to see if this segment should be on.
 
    {digitalWrite(A, HIGH);}   // Turns on the segment.
    else   // Otherwise, it turns it off.
    {digitalWrite(A, LOW);}  // Turns segment off.
 
      result = bitRead( num, 1);  // Same thing for the 6 remaining segments.
 
      if (result == 1)
 
    {digitalWrite(B, HIGH);}
    else
    {digitalWrite(B, LOW);}     
    result = bitRead( num, 2);
 
      if (result == 1)
 
    {digitalWrite(C, HIGH);}
    else
    {digitalWrite(C, LOW);}    
   result = bitRead( num, 3);
 
      if (result == 1)
 
    {digitalWrite(D, HIGH);}
    else
    {digitalWrite(D, LOW);}    
   result = bitRead( num, 4);
 
      if (result == 1)
    {digitalWrite(E, HIGH);}
    else
    {digitalWrite(E, LOW);}
    
   result = bitRead( num, 5);  
      if (result == 1)
    {digitalWrite(F, HIGH);}
    else
    {digitalWrite(F, LOW);}    
    
   result = bitRead( num, 6);
      if (result == 1)
    {digitalWrite(G, HIGH);}
    else
    {digitalWrite(G, LOW);}
  }
void setup() {       // Our setup routine         
  pinMode(A, OUTPUT); // Making all pins outputs
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(10,OUTPUT);
}
void loop() {   // Loops forever
  on(num0);   // Passing "num0" to the function  "on()" defined above to display "0"
 delay(1000);   // Delay for 1 second to see the "0"
  on(num1);  // Change to "1"
  delay(1000); 
  on(num2);  
  delay(1000);
  on(num3);  
  delay(1000);
  on(num4);  
  delay(1000);
  on(num5);  
  delay(1000);
  on(num6);  
  delay(1000);
  on(num7);
  delay(1000);
  on(num8);  
  delay(1000);  
  on(num9);  
  delay(1000);
}

Note about this code:

Line 1 to Line 7 : We choose to use "int" to store our values. Of course we could have used a different variable, but we are comfortable with "int". Read more about "int" (http://arduino.cc/en/Reference/Int).

Line 8 to Line 17: We choose to use hex format, you could have choosen binary, or decimal format.

Image may be NSFW.
Clik here to view.


Viewing all articles
Browse latest Browse all 45

Trending Articles