≡ Menu

Arduino 2 Digit 7 Segment Display with Buttons | Part 4

* This is a multi-part post. Here are links to all parts:

Part 1: Intro, bill of materials and simple sketch
Part 2: The circuit for the 2-digit 7-segment counter
Part 3: Sketch broken down in sections, explained
Part 4: Added two buttons, and modified sketch; this post
Part 5: Code for buttons, explained

This week we modify the original circuit and sketch to include two buttons, one to control each digit of the display.

Here’s what the setup of our 2 digit 7 segment display with buttons looks like:

2 Digit 7 Segment Display with Buttons

And here’s the complete sketch:

// www.TinkerHobby.com
// Natalia Fargasch Norman
// Dual seven-segment LED Display with buttons
// Common Anode digit 1 pin 10
// Common Anode digit 2 pin 5

//       CA1 G  F  A  B
//        |  |  |  |  |      -> pins and segments they control
//   ---------    ---------
//   |   A   |    |   A   |
//  F|       |B  F|       |B
//   |---G---|    |---G---|
//  E|       |C  E|       |C
//   |   D   |    |   D   |
//   ---------    ---------
//        |  |  |  |  |      -> pins and segments they control
//        D  DP E  C CA2         

// Segments that make each number when lit:
// 0 => -FEDCBA
// 1 => ----BC-
// 2 => G-ED-BA
// 3 => G--DCBA
// 4 => GF--CB-
// 5 => GF-DC-A
// 6 => GFEDC-A
// 7 => ----CBA
// 8 => GFEDCBA
// 9 => GF-DCBA

// Arduino digital pins used to light up
// corresponding segments on the LED display
#define A 3
#define B 2
#define C 6
#define D 8
#define E 7
#define F_SEG 4
#define G 5

// Pushbuttons connected to pins 9 and 10
#define BTN1 9
#define BTN2 10

// Pins driving common anodes
#define CA1 13
#define CA2 12

// Pins for A B C D E F G, in sequence
const int segs[7] = { A, B, C, D, E, F_SEG, G };

// Segments that make each number
const byte numbers[10] = { 0b1000000, 0b1111001, 0b0100100, 0b0110000, 0b0011001, 0b0010010,
0b0000010, 0b1111000, 0b0000000, 0b0010000 };

int digit1 = 0;
int digit2 = 0;

void setup() {
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F_SEG, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(BTN1, INPUT);
  pinMode(BTN2, INPUT);
  pinMode(CA1, OUTPUT);
  pinMode(CA2, OUTPUT);
}

void loop() {

  // check button1
  int val1 = digitalRead(BTN1);
  if (val1 == HIGH) {
    digit1++;
    digit1 %= 10;
    delay(10);
  }

  // check button2
  int val2 = digitalRead(BTN2);
  if (val2 == HIGH) {
    digit2++;
    digit2 %= 10;
    delay(10);
  }

  // display number
  unsigned long startTime = millis();
  for (unsigned long elapsed=0; elapsed < 600; elapsed = millis() - startTime) {
    lightDigit1(numbers[digit1]);
    delay(5);
    lightDigit2(numbers[digit2]);
    delay(5);
  }
}

void lightDigit1(byte number) {
  digitalWrite(CA1, LOW);
  digitalWrite(CA2, HIGH);
  lightSegments(number);
}

void lightDigit2(byte number) {
  digitalWrite(CA1, HIGH);
  digitalWrite(CA2, LOW);
  lightSegments(number);
}

void lightSegments(byte number) {
  for (int i = 0; i < 7; i++) {
    int bit = bitRead(number, i);
    digitalWrite(segs[i], bit);
  }
}

Here’s a video of the Arduino 2 digit 7 segment display with buttons in action.

{ 65 comments… add one }
  • Eric

    Can I ask why you are looping for 0.6 seconds (600 ms) on the “Display Number” part? And doesn’t that mean that you will keep getting “stuck” in this loop for 0.6 seconds each time, and that you will miss the button presses (unless they are holding it long enough for you to come out of the loop)?

    • Yes, but I’ve found that keeping the current number lit for that amount of time allows us to better see what number is being displayed. The code is simple enough for beginners to understand, even if it comes at some performance cost.

  • computer

    Hi natalia!!Immediatly question??
    Design a logic circuit (?) that can convert the (five-bit) output of the full adder into appropriate values
    for seven segment decoders in order to show the resulting sum on display devices as decimal values.
    Examples:
    + if A=1111 (15) and B=0101 (5) then the values on the displays should be (15+5=20) Display1=2
    and Display0=0.
    + if A=1010 (10) and B=1000 (8) then the values on the displays should be (10+8=18) Display1=1
    and Display0=8.
    + if A=0010 (2) and B=0111 (7) then the values on the displays should be (2+7=9) Display1=0 and
    Display0=9.
    1

    • Hi,
      unfortunately I don’t do these circuits… yet. I’m only a beginner in electronics, and so far I’m most comfortable doing microcontroller programming… sorry.

  • ronald

    hi! this helps a lot for a beginner like ,can i ask how to modify the code having only one button to control the count one at a time up to 99 count?

    thanks very much!
    best regards,
    ronald

    • Hi Ronald,

      to have the system count up using just one button you will keep the units digit to be the modulus (%) of the current count by 10, and the tenths digit to be the integer division (/) of the current count by 10. Something like:

      // check button
      int val = digitalRead(BTN);
      if (val == HIGH) {
      count++;
      unitsDigit = count % 10;
      tensDigit = count / 10;
      delay(10);
      }

      • Adrian

        Hi, my example

        #define A 6
        #define B 5
        #define C 4
        #define D 3
        #define E 2
        #define FS 1
        #define G 0
        #define BTN 7
        #define CA1 13
        #define CA2 12

        const int segs[7] = { A, B, C, D, E, FS, G };
        const byte numbers[10] = { 0b1000000, 0b1111001, 0b0100100, 0b0110000, 0b0011001, 0b0010010,
        0b0000010, 0b1111000, 0b0000000, 0b0010000 };

        int digit1 = 0;
        int digit2 = 0;

        void setup() {
        pinMode(CA1, OUTPUT);
        pinMode(CA2, OUTPUT);
        pinMode(BTN, INPUT);
        pinMode(A, OUTPUT);
        pinMode(B, OUTPUT);
        pinMode(C, OUTPUT);
        pinMode(D, OUTPUT);
        pinMode(E, OUTPUT);
        pinMode(FS, OUTPUT);
        pinMode(G, OUTPUT);
        }
        void loop() {

        int val1 = digitalRead(BTN);
        if (val1 == HIGH) {
        digit1++;
        digit1 %= 10;
        delay(100);
        }

        int val9 = digitalRead(BTN);
        if (val9 == HIGH){
        int val2 = bitRead(digit1, 3);
        if (val2 == 0) {
        int val3 = bitRead(digit1, 2);
        if (val3 == 0) {
        int val4 = bitRead(digit1, 1);
        if (val4 == 0) {
        int val5 = bitRead(digit1, 0);
        if (val5 == 0) {

        digit2++;
        digit2 %= 10;
        delay(100);
        }
        }
        }
        }
        }
        unsigned long startTime = millis();
        for (unsigned long elapsed=0; elapsed < 600; elapsed = millis() – startTime) {
        lightDigit1(numbers[digit1]);
        delay(100);
        lightDigit2(numbers[digit2]);
        delay(100);
        }
        }

        void lightDigit1(byte number) {
        digitalWrite(CA1, LOW);
        digitalWrite(CA2, HIGH);
        lightSegments(number);
        }

        void lightDigit2(byte number) {
        digitalWrite(CA1, HIGH);
        digitalWrite(CA2, LOW);
        lightSegments(number);
        }

        void lightSegments(byte number) {
        for (int i = 0; i < 7; i++) {
        int bit = bitRead(number, i);
        digitalWrite(segs[i], bit);
        }
        }

        • Rehan Tariq Abbasi .

          problem#1:
          when i press push button it counts 1to7 and jump to 10.
          problem#2:
          i want if i press button one time it will show both digits.
          thanku

  • ronald

    thanks Natalia, i will try your suggestions ….will post result after..

    br,
    ronald

  • ronald

    wow it works like a charm ……
    thank you very much Natalie! your the best…

    br,
    ronald

  • ronald

    hi Natalie, I found out that after the count reach 99 ,it displays some number and none number characters ..how to modify this code so that when count reach 99 it reset to 00 and count again when button is press…

    br,
    ronald

    • Hi Ronald,

      the easiest way to do this is to instead of using the variable count itself as the number to display, use “count % 100”, which will yeld 0 when count is 100, 1 when count is 101, and so on. Kinda like you did using the modulo operation on the tens unit display.

      So you can create another variable, say “display”, which will be “display = count % 100;”, and then instead of using count you use display. (as in “unitsDigit = display % 10;”) does that make sense? let me know how that works.

  • ronald

    hi there,
    i was able to experiment and add some to your given code and it works to my expectation …
    int val = digitalRead(BTN);
    if (val == HIGH) {
    if (count<99) count++;
    else count = 00;
    unitsDigit = count % 10;
    tenthsDigit = count / 10;
    delay(10);

    will try your suggestions if same result will came out.. I'am still on a learning stage
    thanks a lot..
    btw,
    if you could suggest how to include alarm or buzzer (a ding dong i guest ^_^) once button press….

    • Hi Ronald,

      check https://www.tinkerhobby.com/phase-0-of-the-ipon-project/ for an example of how I used sound output in response to an input signal from a switch and a force sensor. I played a more elaborate sound, but you can output only a buzzing sound if you want (by not varying the frequency like I did).

    • Shen

      Hi Natalia I am new in coding in arduino i would like to ask how can i make this 3 digits.. thanks

      • Hi Shen, the code will be very similar, except you will reset your counter to all zeroes again once it reaches 999. You will use count/100 for your hundreds digit, and where you were using count in your code above for the tens and units digits, you will instead use the remainder of the integer division by 100, i.e modulus (%). So your tens digit will be (count%100)/10 and your units digit will be (count%100)%10.

  • dobri

    there are two transistors that i am wondering what are for and are they realy enable and if they don`t can you give a circuit that doesnt need transistors. I am only a beginer.

  • patrick

    hey can you help me on creating a 2nd button program that will allow the digit to subtract from the present value. thank you 😀

    • patrick

      i manage to make the code sorry to disturb.thank you 😀

      • Sorry not to have replied, Patrick. For some reason Wordpress is not sending me comment notifications… I am glad you figured it out! Hey, commenting here is definitely not disturbing me!

      • Aaron

        Patrick, would you mind posting the change you made to do this?

      • spenc

        would you mind sharing your code for countdown button?

      • Waqas

        patrick can you please share the code of countdown button

  • Darren

    I have looked for a long time to find a project like this. This is exactly what I need. I want to drive 2 ‘sets’ of 7-segment displays. 2 small ones like in your example, and 2 larger ones. I want each small digit to match the large digit. Ie, if the small digit shows a ‘1’ , then the large digit should show a ‘1’.

    3 buttons, one button for each digit, one to clear them both back to ‘0’.

    I will use an Arduino Uno and hopefully figure out how to power it all.

    THANK YOU for posting your stuff!!

  • Renze Santos

    Hi there..!
    Can you make a 3 digit 7 segment display? my project is to connect the arduino to a coin slot that can read 3 different coins. But the thing is, I have to make another display except for that in the coin slot that will show that the inserted coins are added correctly.., like in a vending machine.. Please help..! thanks in advance…. ^_^

    • Hi Renze,

      yes, you can. If you have a 3-digit 7-segment display, such as this one here (3-digit 7-segment display from Jameco) you will control them in the same way, and you will need a third transistor to switch control to the third digit.

      I apologize for the delay, I was traveling and sometimes miss comment notifications.

      Take care,
      Natalia.

  • dominic toretto

    is this for 8051 micro controller?

    • Hi Dominic,

      no, this is for the Arduino.

      • dominic toretto

        thanks for the reply.
        if possible can you please give me c language program coding for 8051 interfacing with 2 digits 7 segment display?
        pleaseeeeeeee 🙂

        • Sorry Dominic…

          I don’t know how to work with the 8051, and I’m not even currently home that I could try to whip up something on it to see how it works… did you search blogs that write about working with the 8051? That might be your best bet.

          Good luck,
          Natalia.

          • dominic toretto

            its alright and thanks a lot 🙂
            im already searching on internet but didnt find anything yet, i have to submit my assignment in few days. lets hope for the best.

            thank you and have a nice day 🙂

  • ed

    hi natalia,, i tried to verify it but its not working.. ^_^ ,, hope you can help me how to work it will. ty

  • Aaron

    Natalia, is it possible to add code to reset to “00” if both buttons are pushed at the same time?

    • Hi Aaron,

      yes, you would just have to add that test for both those pins being high at the same time. Except it is just a bit tricky because we will most likely not be pressing the two buttons at EXACTLY the same time. So what you could do is have it be a press and hold, if both pins are high, say, for a full second, then your sketch will certainly catch that. If it were just a quick press it could likely happen that we actually pressed the two buttons a few milliseconds apart.

  • Shen

    Hi Natalia

    Great work, I’ve been trying to make it increment using only one button and Ronald’s

    if (val == HIGH) {
    if (count<99) count++;
    else count = 00;
    unitsDigit = count % 10;
    tenthsDigit = count / 10;
    delay(10);

    works pretty well on 2 Digits but I'm trying to make it 3 digits any idea how it will work? thanks for your generosity.

    • Hi Shen, the code will be very similar, except you will reset your counter to all zeroes again once it reaches 999. You will use count/100 for your hundreds digit, and where you were using count in your code above for the tens and units digits, you will instead use the remainder of the integer division by 100, i.e modulus (%). So your tens digit will be (count%100)/10 and your units digit will be (count%100)%10.

  • Vivek

    Hi Natalia,

    Thanks. the code works perfectly. Have been trying to add a reverse counter, ones the digits have been finalized (no success). The reverse counter, after reaching value
    “zero zero” in 7-segment should trigger an event like making a pin high, or execute another function.
    The event or function has to run only ones until reset on arduino is pressed.

    Please help.

    • Hi Vivek,

      so what you are doing is keeping track of the current count and starting to decrement the digit to display once it reaches 99? See Shen’s comment above, you will have a test for count == 99. Then to trigger your event on 0 0, in the decrement loop you will have a test for count == 0, and either call your function or set an Arduino pin, if you so desire.

      • Vivek

        Thanks for the reply Natalia. I am a novice to arduino sketches.
        Learning from shared codes from experts like you. Seen Shen’s comments.
        The sketch you have written takes input from two buttons BTN1 and BTN2.
        It can display any integer between “00” to “99”. This is exactly what I was looking for…

        Additionally please refer to a sketch at this URL:- https://www.hobbytronics.co.uk/arduino-countdown-timer

        This counter also works perfectly, I was able to modify this code to work for 2 digits. However the starting value of “30” (in example) is hard coded in sketch and assigned to variable “start_num”.

        What I am trying to do is as follows:-

        Instead of hard coded, preassigned value, I want to assign the number from your code, using two buttons. Additionally there will be another button, lets say “start_button” . Once a number/integer is input with first two buttons, the 7 segment display will continue to show the same. (Existing sketch)

        Next: now if the third button (“start_button”) is pressed, the same number/integer should start to decrement by one per second and stop at “zero””zero”.

        Here I will then trigger an action, say switch off a photo exposure lamp, or switch off the microwave oven or a relay etc.

        I am stuck up at the reverse countdown timer to be initiated at push of third button. In my case, the countdown code starts executing as soon as I switch ON the arduino. I do not get a chance to input initial number to start with, from the first two buttons.

  • kuya

    what part in the program should i change, without changing the circuit, to make it count from 0 to 99 with one button and the other button will count backwards.??
    i tried to understand the program and other comments but ended up getting confused, especially when i ended in the part in which it says that the variables wasnt declared……
    please help….

    thank you.

  • Renz

    Hi Natalia,

    can i ask whats the use of ?
    unitsDigit = count % 10;
    tenthsDigit = count / 10;

    it doesn’t work in my arduino..
    and should i use int count = 0 to use the count var?

    Thank you so much..

  • Renz

    Hi Natalia,

    can i ask for an advise ? is this right ?
    int count = 0;
    int unitsDigit = 0;
    int tensDigit = 0;
    // check button1
    int val = digitalRead(BTN2);
    if (val == HIGH) {
    count++;
    unitsDigit = count % 10;
    tensDigit = count / 10;
    delay(10);
    }

    // check button2

    // display number
    unsigned long startTime = millis();
    for (unsigned long elapsed=0; elapsed < 600; elapsed = millis() – startTime) {
    lightDigit1(numbers[digit1]);
    delay(5);
    lightDigit2(numbers[digit2]);
    delay(5);
    }
    }

    void lightDigit1(byte number) {
    digitalWrite(CA1, LOW);
    digitalWrite(CA2, HIGH);
    lightSegments(number);
    }

    void lightDigit2(byte number) {
    digitalWrite(CA1, HIGH);
    digitalWrite(CA2, LOW);
    lightSegments(number);
    }

    void lightSegments(byte number) {
    for (int i = 0; i < 7; i++) {
    int bit = bitRead(number, i);
    digitalWrite(segs[i], bit);
    }
    }

  • Yannee Vee Sumagaysay

    We would like to do this as a class project but in our place the only available 7-segment has 18 pins. Can you please help us on this. thanks

  • Zack

    Hi, can i ask what does this code mean?

    // display number
    unsigned long startTime = millis();
    for (unsigned long elapsed=0; elapsed < 600; elapsed = millis() – startTime)
    {
    lightDigit1(numbers[digit1]);
    delay(5);
    lightDigit2(numbers[digit2]);
    delay(5);
    }

    Are my comments correct for the below statements?
    void lightDigit1(byte number)
    {
    digitalWrite(CA1, LOW); //set the LED off
    digitalWrite(CA2, HIGH); //set the LED on
    lightSegments(number);
    }

    void lightDigit2(byte number) {
    digitalWrite(CA1, HIGH); //set the LED on
    digitalWrite(CA2, LOW); //set the LED off
    lightSegments(number);
    }

  • JustForFun

    This is a very useful tutorial! Thank you for taking the time to make it Natalia! I am using a 2 digit 7 segment display and an arduino nano to create an ammo counter on a rifle. I have a vibration sensor that will be the button that counts down as the gun shoots to tell me how many more shots I have left. I may have to use an accelerometer instead if the vibration sensor gets glitchy(the vibration sensor’s output gets stuck on HIGH sometimes). Anyways similar to what Vivik asked about I would like to be able to set the initial value on the display using 2 buttons. An add and subtract button. Then a third button or input like my sensor to make it count down from the initial value. Since not every rifle has the same ammo capacity I would like to be able to set the max number of shots without reprogramming the arduino at a computer. I don’t know if this is possible on an arduino, so I wanted to know your thoughts.

  • ISLAM,MD.RASEDUL

    I am baginer of Ardunio..
    I am student of American International University*-Bangladesh of EEE Department.
    How to write a code and make it [automatic light controlling with visitor counting with 7 segment display,PIR sensor,LDR based on Ardunio UNO]
    I don’t know if this is possible on an arduino, so I wanted to know your thoughts.

  • Adrian

    Beautyfull program, i am very new to this, i understand what coding works , but i dont know how to write code, i have a project for school, can u please point me out so i can build this project but with a button that if pressed for an amount of time ( eg. 3 secs ) , it will descrease the number from screen by 1 . and a speaker that beeps when it reaches zero? .. thank you very much , i am trying to find info , but nothing …

  • A

    Update! 3 buttons. One for increment from 0 to 99, one to decrement from 99 to 0 and one for reset to 0. Enjoy!

    #define A 6
    #define B 5
    #define C 4
    #define D 3
    #define E 2
    #define FS 1
    #define G 0
    #define BTN 7
    #define BTN1 8
    #define BTN2 9
    #define CA1 13
    #define CA2 12

    const int segs[7] = { A, B, C, D, E, FS, G };

    const byte numbers[10] = { 0b1000000, 0b1111001, 0b0100100, 0b0110000, 0b0011001, 0b0010010,
    0b0000010, 0b1111000, 0b0000000, 0b0010000 };

    int digit1 = 0;
    int digit2 = 0;

    void setup() {
    pinMode(CA1, OUTPUT);
    pinMode(CA2, OUTPUT);
    pinMode(BTN, INPUT);
    pinMode(BTN1, INPUT);
    pinMode(BTN2, INPUT);
    pinMode(A, OUTPUT);
    pinMode(B, OUTPUT);
    pinMode(C, OUTPUT);
    pinMode(D, OUTPUT);
    pinMode(E, OUTPUT);
    pinMode(FS, OUTPUT);
    pinMode(G, OUTPUT);
    }

    void loop() {
    int val0 = digitalRead(BTN);
    if (val0 == HIGH){
    digit1 = 0;
    digit2 = 0;
    delay(100);
    }

    int val1 = digitalRead(BTN1);
    if (val1 == HIGH) {
    digit1++;
    if (digit1 == 10) digit1 = 0;
    delay(100);
    }

    if (val1 == HIGH) {
    if (digit1 == 0) digit2++;
    if (digit2 == 10) digit2 = 0;
    delay(100);
    }

    int val2 = digitalRead(BTN2);
    if (val2 == HIGH){
    digit1–;
    if (digit1 == -1) digit1 = 9;
    delay(100);
    }

    if (val2 == HIGH) {
    if (digit1 == 9) digit2–;
    if (digit2 == -1) digit2 = 9;
    delay(100);
    }

    unsigned long startTime = millis();
    for (unsigned long elapsed=0; elapsed < 400; elapsed = millis() – startTime) {
    lightDigit1(numbers[digit1]);
    delay(100);
    lightDigit2(numbers[digit2]);
    delay(100);
    }
    }

    void lightDigit1(byte number) {
    digitalWrite(CA1, LOW);
    digitalWrite(CA2, HIGH);
    lightSegments(number);
    }

    void lightDigit2(byte number) {
    digitalWrite(CA1, HIGH);
    digitalWrite(CA2, LOW);
    lightSegments(number);
    }

    void lightSegments(byte number) {
    for (int i = 0; i < 7; i++) {
    int bit = bitRead(number, i);
    digitalWrite(segs[i], bit);
    }
    }

    • m

      I used your code but there is an error at this line
      ////for (unsigned long elapsed=0; elapsed < 400; elapsed = millis() – startTime) {////
      and at digit2 just keep blinking number 0 and 9 .

      • Adrian

        A good display on this scheme can not be obtained
        Use this scheme for your project, one button for increment at 0 to 99, and one button for decrement at 99 to 0
        https://static.md/196d2540f4800f65e7be55a6fac18b40.png
        and this code
        //
        #include “LedControl.h”

        LedControl lc=LedControl(12,10,11);

        #define BTN 0
        #define BTN1 1
        #define BTN2 2

        int digit1 = 0;
        int digit2 = 0;

        unsigned long delaytime=250;

        void setup() {
        lc.shutdown(0,false);
        lc.setIntensity(0,15);
        lc.clearDisplay(0);
        pinMode(BTN, INPUT);
        pinMode(BTN1, INPUT);
        pinMode(BTN2, INPUT);
        }

        void number() {

        int val0 = digitalRead(BTN);
        if (val0 == HIGH){
        digit1 = 0;
        digit2 = 0;
        delay(100);
        }
        int val1 = digitalRead(BTN1);
        if (val1 == HIGH) {
        digit1++;
        if (digit1 == 10) digit1 = 0;
        delay(100);
        }

        if (val1 == HIGH) {
        if (digit1 == 0) digit2++;
        if (digit2 == 10) digit2 = 0;
        delay(100);
        }

        int val2 = digitalRead(BTN2);
        if (val2 == HIGH){
        digit1–;
        if (digit1 == -1) digit1 = 9;
        delay(100);
        }

        if (val2 == HIGH) {
        if (digit1 == 9) digit2–;
        if (digit2 == -1) digit2 = 9;
        delay(100);
        }
        }

        void scrollDigits() {
        lc.setDigit(0,1,digit1,false);
        lc.setDigit(0,0,digit2,false);
        delay(delaytime);
        }

        void loop() {
        number();
        scrollDigits();
        }
        //
        Don’t forget use LedControl library
        https://github.com/wayoda/LedControl.git
        Good luck!
        P.S. Sorry for my english!

  • isha

    im trying to do count up and count down using 1 7segment display with 2 buttons. one for counting up and the other for counting down. and when it reach 9, a red LED will light up. Can help?

  • Dhei

    Hello, thanks for the program.

    But, can you please show the simulation with proteus or else too?
    Cause I have problem to connect them.

    Sorry and thank you

  • Chandeepa

    Hi… I need a big help.
    I have a 2 digit common cathode 7 segment.
    please anyone can send me the code to common cathode 7 segment display?
    I want to make 2 digit UP & DOWN counter circuit using 2 PUSH BUTTON.
    Please can you help me.
    Because I’m a beginner for ARDUINO..
    please anyone help me..
    This project very important to me..

  • // http://www.TinkerHobby.com
    // Natalia Fargasch Norman

    // Modified by Adrian Birnaz
    // On 24 MAY 2018

    // Dual seven-segment LED Display with two buttons
    // Common Cathode digit 1 pin 8
    // Common Cathode digit 2 pin 9
    // | | | | | -> pins and segments they control
    // ——— ———
    // | A | | A |
    // F| |B F| |B
    // |—G—| |—G—|
    // E| |C E| |C
    // | D | | D |
    // ——— ———
    // Arduino digital pins used to light up
    // corresponding segments on the LED display
    #define A 7
    #define B 6
    #define C 5
    #define D 4
    #define E 3
    #define F_SEG 2
    #define G 1
    // Pins driving common cathodes
    #define CC1 8
    #define CC2 9
    // Pushbuttons connected to pins 9 and 10
    #define BTN1 13
    #define BTN2 11
    #define AMOUNT_NUM 2
    #define REFRESH_IND_RATE 5
    #define REFRESH_BTN_RATE 200
    unsigned long startTime;
    unsigned long elapsed;
    unsigned long elapsed_btn;
    int value;
    volatile unsigned char buf[AMOUNT_NUM];
    // Segments that make each number
    unsigned char number[] =
    {
    (1 << A) | (1 << B) | (1 << C) | (1 << D) | (1 << E) | (1 << F_SEG) | (0 << G), // 0
    (0 << A) | (1 << B) | (1 << C) | (0 << D) | (0 << E) | (0 << F_SEG) | (0 << G), // 1
    (1 << A) | (1 << B) | (0 << C) | (1 << D) | (1 << E) | (0 << F_SEG) | (1 << G), // 2
    (1 << A) | (1 << B) | (1 << C) | (1 << D) | (0 << E) | (0 << F_SEG) | (1 << G), // 3
    (0 << A) | (1 << B) | (1 << C) | (0 << D) | (0 << E) | (1 << F_SEG) | (1 << G), // 4
    (1 << A) | (0 << B) | (1 << C) | (1 << D) | (0 << E) | (1 << F_SEG) | (1 << G), // 5
    (1 << A) | (0 << B) | (1 << C) | (1 << D) | (1 << E) | (1 << F_SEG) | (1 << G), // 6
    (1 << A) | (1 << B) | (1 << C) | (0 << D) | (0 << E) | (0 << F_SEG) | (0 << G), // 7
    (1 << A) | (1 << B) | (1 << C) | (1 << D) | (1 << E) | (1 << F_SEG) | (1 << G), // 8
    (1 << A) | (1 << B) | (1 << C) | (1 << D) | (0 << E) | (1 << F_SEG) | (1 < REFRESH_IND_RATE)
    {
    IND_Update();
    elapsed = startTime;
    }
    if (startTime – elapsed_btn > REFRESH_BTN_RATE)
    {
    BTN_Check();
    elapsed_btn = startTime;
    }
    }

    void BTN_Check()
    {
    // check button1
    int val1 = digitalRead(BTN1);
    if (val1 == LOW)
    {
    if (value != 99)
    {
    value = value + 1;
    }
    }
    // check button2
    int val2 = digitalRead(BTN2);
    if (val2 == LOW)
    {
    if (value != 0)
    {
    value = value – 1;
    }
    }
    }

    void IND_Output(unsigned int value)
    {
    unsigned char tmp;
    for (unsigned char i = 0; i < AMOUNT_NUM; i++)
    {
    tmp = value % 10;
    buf[i] = number[tmp];
    value = value/10;
    }
    }

    void IND_Update(void)
    {
    static unsigned char count = 0;
    PORTD = 0;
    digitalWrite(CC1, LOW);
    digitalWrite(CC2, LOW);
    switch (count)
    {
    case 0:
    digitalWrite(CC1, HIGH);
    break;
    case 1:
    digitalWrite(CC2, HIGH);
    break;
    }
    PORTD = buf[count];
    count++;
    if (count == AMOUNT_NUM)
    {
    count = 0;
    }
    }

  • Link to the full project.

  • Karanbir

    I observe that to set a number to 876 we would need 876 button press instances. A tedious task. Is there a way to select digits( Units/Tens/Hundreds) and then set with a switch?

  • paul

    I am a newby when it comes to arduino programming. What you have here is really close to what i am trying to accomplish. With this setup i want to be able to add and subtract from 0 to 30 using 2 switches.

  • WENCESLAUS NIC DONNY

    Why the lightdigit cannot declare in this scope ? i need the answer asap . thankyou in advance

Leave a Comment