Advertisement
arduino 7 segment display

Arduino 7 Segment LED Display and Counter – Tutorial #8

arduino and 7 segment display on breadboard

This is a simple 0 to 9 counter circuit constructed using Arduino! Here, a common cathode 7-segment LED display is connected to Arduino for displaying the digits.
The code (Arduino sketch) allows push button increment of the counter from 0 to 9.
The whole circuit can be powered from a standard 9V PP3/6F22 battery, or from any suitable Arduino power adaptor.

7 segment led display

The seven segment display is infact a very simple device. It is a combination of 8 LEDs (the decimal point -DP- is the 8th), which can be arranged so that different combinations can be used to make numerical digits.

Related Products: Displays

Details of a common cathode type 7 segment LED display is shown here. Note that pins 3 and 8 of the display is the cathode terminals.

Arduino Schematic

arduino 7 segment display

Just follow the schematic circuit diagram to make the entire project.
Arduino pins 2, 3, 4, 5, 6, 7 and 8 should go to Display pins 7, 6, 4, 2, 1, 9 and 10 in correct order. In case of any doubt refer this table. Push Switch (S1) input point is at pin 9 of the Arduino.

arduino to display pins

Connecting the display pins directly to Arduino I/O pins is not a good practice. For testing purpose only one 330 Ohm resistor (R2) is added between ground rail (0V) and the common cathode pins (3 & 8). It is better to directly connect pins 3 & 8 of the display to ground rail. Next add a 330 Ohm resistor between each of the other connections to the Arduino.

Related Products: Displays Misc

Arduino 7 Segment Display Code

/* 
Arduino 7-Segment LED Display for Common Cathode Displays
Arduino Pins: 2,3,4,5,6,7,8
Display Pins: 7,6,4,2,1,9,10
Display Pins 3&8 should go to GND
Switch Input is at Pin 9
Source: http://www.electroschematics.com/9636/arduino-segment-display-counter/
*/
byte numbers[10] = { 
                    B11111100, B01100000, B11011010, B11110010, B01100110, 
                    B10110110, B10111110, B11100000, B11111110, B11100110 
                    };
void setup() { 
    for(int i = 2; i <= 8; i++) {
        pinMode(i, OUTPUT); 
    }
    pinMode(9, INPUT);
}

int counter = 0;
bool go_by_switch = true;
int last_input_value = LOW;

void loop() {
    if(go_by_switch) {
        int switch_input_value = digitalRead(9);
        if(last_input_value == LOW && switch_input_value == HIGH) {
            counter = (counter + 1) % 10;
        }
        last_input_value = switch_input_value;
    } else {
        delay(500); 
        counter = (counter + 1) % 10;
    }
    writeNumber(counter);
}

void writeNumber(int number) {
    if(number < 0 || number > 9) {
        return;
    }
    byte mask = numbers[number];
    byte currentPinMask = B10000000;
    for(int i = 2; i <= 8; i++) { if(mask & currentPinMask) digitalWrite(i,HIGH); else digitalWrite(i,LOW); currentPinMask = currentPinMask >> 1;
    }
}

21 Comments

Join the conversation!

Error! Please fill all fields.
  • rickie99

    I’ve build the set-up and put the code in the arduino software, but it says that lt was not declared in this line:

    for(int i = 2; i <= 8; i++)

    please help me.

    • T.K.Hareendran

      Refer the code window (see lines 14,27,39,44). I suspect this has undergone an HTML entity translation. You want to reverse this, like & (to &), > (to >), &lt (to <).

  • tkhareendran

    Thanks to all readers for their feedback and support. I will try to give my answers to the unanswered queries one by one when I have enough time to spare. Delay regretted!

  • houra

    If i want to expand this to 2 7 segments controlled by 2 push buttons, how do i modify the circuit to do this ?

  • good ...

    where the DP number 5 in segment will be connecet to?
    where the 5v will be connected to ?
    what the S1 ?

  • Miha

    Hello…

    This article is really useful ! thanks for that !

    but… question.. 🙂
    what do i have to add into the code to get the counting down ??
    so… 2 buttons… 1. counts up… the 2.nd one counts down..

    anny suggestions ?

  • lite

    I ‘ve tried and succeeded , thanks for the instructions. what if I want to make if the button is pressed twice , the counter will run

  • Safyan

    What if we want a counter till 999 then?
    what alterations in code will be made other than the hardware.

    • Ian Gregory

      I don’t want a 3 digit counter. but a 4 digit one for use in a digital multimeter.

  • TerrenceJ

    I’m very new to Arduino and I found this article and project very helpful, I built it and typed the code out manually, I find this helps me understand it better, and it worked perfectly, Thanks, Terry.

  • Ian Gregory

    I learn’t Basic many years ago, and DOES NOT depend on Arduino codes.

    To be honest a lot of the various codes for Arduino circuits depend on knowledge of C++, that is one language I have stayed away from for a variety of reasons.

    Quite a while ago I did ask for any future Arduino projects, such as a multiple 7 segment display, here is a copy of the message:

    “Themisive on January 27, 2014 at 10:49 pm
    member
    reply

    All very well. but at the moment I’m in the middle of another computer project – It hasn’t taken any programming but HAS taken about 4 weeks of my time.

    Just out of curiosity, what language are the coed sections written in?

    What I would like is a 7 segment display counter that does NOT depend on a computer code, so that it may be used anywhere.”

  • david

    thanks for the program, its very useful to me.
    I just need to have it count to 8, then the next click to 2 and then 3,4,etc to 8 then 2.
    I have amended some lines but I still need 3 clicks from 8 to 2, when I would like 1 click from 8 to 2.

    thanks very much

    here is what I am using:

    byte numbers[10] = {
    B11111100, B01100000, B11011010, B11110010, B01100110,
    B10110110, B10111110, B11100000, B11111110, B11100110
    };
    void setup() {
    for(int i = 2; i <= 8; i++) {
    pinMode(i, OUTPUT);
    }
    pinMode(9, INPUT);
    }

    int counter = 2;
    bool go_by_switch = true;
    int last_input_value = LOW;

    void loop() {
    if(go_by_switch) {
    int switch_input_value = digitalRead(9);
    if(last_input_value == LOW && switch_input_value == HIGH) {
    counter = (counter + 1) % 9;
    }
    last_input_value = switch_input_value;
    } else {
    delay(500);
    counter = (counter + 1) % 9;
    }
    writeNumber(counter);
    }

    void writeNumber(int number) {
    if(number 8) {
    return;
    }
    byte mask = numbers[number];
    byte currentPinMask = B10000000;
    for(int i = 2; i > 1;
    }
    }

Looking for the latest from TI?