# 2-Digit 7-Segment Display Counter with Arduino | Part 1

#### * 2-Digit 7-segment display counter is a multi-part post. Here are links to all parts:

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

This month’s Arduino project is to build two 2-digit 7-segment display circuits and sketches, one that counts up and one that counts up using mini push buttons. The next posts will explain the circuits and the Arduino sketches. (Here’s a simpler, 1-digit 7-segment display using Arduino).

## Sketch for the 2-digit 7-segment display counter (without buttons):

```// www.TinkerHobby.com
// Natalia Fargasch Norman
// Dual seven-segment LED Display
// 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

// 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 };

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(CA1, OUTPUT);
pinMode(CA2, OUTPUT);
}

void loop() {
for (int digit1=0; digit1 < 10; digit1++) {
for (int digit2=0; digit2 < 10; digit2++) {
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++) {
digitalWrite(segs[i], bit);
}
}```

Here’s a video of the 2-digit 7-segment display counter in action.

• Tom Dolan

??

Seems to me that this line:
const int segs[7] = { 3, 2, 6, 8, 7, 4, 5 };

should be:
const char segs[7] = { A,B,C,D,E,F,G };

..works this way for me…

thanks for the code and the design!

• Thanks for catching that, Tom! I’ll fix the code listing. What’s the point of doing the #define’s if I’m not gonna use them, right? 🙂

• sam

hi it does not work

sketch_jan25a:45: error: ‘F’ was not declared in this scope
sketch_jan25a.cpp: In function ‘void setup()’:
sketch_jan25a:57: error: ‘F’ was not declared in this scope

• Sam,

I haven’t modified my sketches to be compatible with Arduino release 1.0. According to the release notes here: https://arduino.cc/en/Main/ReleaseNotes a now built-in function F() can be used to store strings in flash memory rather than RAM.

Therefore on the line that says:

#define F 4

just replace “F” with another identifier (for instance FF) and replace every occurrence of “F” on the sketch by that new identifier.

That should do the trick. At some point I should go over the sketches on this blog and make these changes to avoid confusion.

• lasita

Hi

Thanks for the code and is it possible to show how to connect the 2 PNP transistors to common anode?

Regards

• lasita

thanks 🙂

• Dobri

Hi i am novice in the curcuit building and i am Bulgarian so excuse my English, but are these transistors really enable and if they are why do you use them ?

• afiq

Hi..
I want to know why when i compile the coding, it says F was not declared in this scope? I’m not good in C or C++ ..hehe

• Read my comment above, in reply to Sam who has asked the same question as you.

• katrina

will it still work without the resistors and transistors?f

• Hi Katrina,

check my post https://www.tinkerhobby.com/arduino-2-digit-7-segment-display-counter-circuit for an explanation behind the components used.

Basically you need the resistors to limit the current in the LEDs. The value of the resistors will depend on the specs of the display used, so check the datasheet for your display.

The 2 transistors were used to select which of the 2 digits is the output of the digitalWrite function. There are other options instead of the transistors, the link I pasted above mentions a few of them. But you need some way to route the signal to the pins of the proper display.

Let me know if this makes it clearer after you read the other post linked to above. I chose this particular setup because it is simple and required no additional chips.

Good luck!
Natalia.

• John Paul

I’m sorry, but I didn’t get what you mean by what you replied in Sam’s post about the error: ‘F’ was not declared in this scope. I’m just a newbie in arduino

• Hi John,

it means that in the old versions of the Arduino compiler we could use the identifier “F” in the define directive. Starting with version 1.0, Arduino defines a built-in function “F()” that allows you to store strings in flash memory.

From the Arduino release notes (https://arduino.cc/en/Main/ReleaseNotes):

“Support has been added for printing strings stored in flash (program memory) rather than RAM. Wrap double-quoted strings in F() to indicate that they should be stored in flash, e.g. Serial.print(F(“hello world”)).”

So sketches can no longer use “F” as an identifier, as it is now a reserved name. In other words: name “F” something else and the sketch will compile. For instance

#define F_segment 4

Let me know if this clears it for you.

• John Paul De Leon

Yeah, I get it but when I’m going to test it out in my Arduino Uno, still doesn’t count. I follow everything there, is there something wrong in my work?

• Hi John,

there certainly is something wrong if the system does not behave as you expect. It could be a myriad of things, so it is hard for someone who is not in front of the circuit and sketch to know what it could be.

A couple of tips:

The way I tested my circuit/sketch was connecting only one segment at a time, and making sure that that one segment was working. Repeating for each individual segment. In my case I didn’t even know what kind of display I had, since it came in a grab bag. The only way to find out was to start sending it signals and observe what happened.

And you do that individual testing for each part, and don’t add anything new until that piece is working.

It is also important to know that depending on whether your display is common anode or common cathode, that will determine the type of transistor you use (NPN for common cathode and PNP for common anode displays).

Sometimes the best thing to do is to take it apart… and start over. With a bare breadboard and a blank sketch…

Good luck to you!

• Cesar Perez

Please I need the circuit diagram 2 digits if they had one with buttons

• Sheila

Hi Natalia 🙂

I search on many websites, blogs and forums for one tutorial with common anode 2 digits 7 segment display with switches buttons and finally I found !! 😀

But I see the title says “Sketch for counting up without buttons:” 🙁

Where is the example with two buttons?

Thanks and regards 🙂

• Hi Sheila,

I did the posts for this small projects in 5 parts, and have yet to include links to all the parts…

So here is the full series, and the modifications to add the two buttons can be found on the last link, part 5:

Hope this helps!

• Sheila

Hi again Natalia 🙂

After I put the question, searching on your blog, finally I found the article that it explain 🙂 Tx for all 🙂

But I have one problem…Numbers are blinking all time and it´s displays rare (They aren´t numbers..)

I’ve tried twice and same problem (I have 2 units of 2 digits 7 segment displays, and both do the same..Wires are like pic…)

The only difference between yours and mine is the PNP (I don´t have..Common anode of each digit are on pin 12 and pin 13..)

You know what could be the problem ?

Regards 🙂
(Buttons, I haven´t put yet..)

• Sheila

Hi Natalia, I´m working in the code and I’m modifying to see if it works on my digits..I have 2 questions:

Number 0 for example: => -KEDCBA => 0b1000000
Should not be 0b0111111 ? Or common anode inverts the values…? (This value should be to common cathode? )

What is the relation between segments and pins on arduino ?
#define A 3
Why isn´t connected to pin 2 ?
#define G 5
Why isn´t connected to pin 8 ?
I don´t see the relation 🙁

Sorry, but I don´t understand these little things…

Regards 🙂

• Hi Sheila, you can choose the pins you want your components connected to. Just make sure your sketch reflects the pins you used. Yes, you are right, zeroes were used to cause segments to lit because I used a common anode display.

You have to follow what the datasheet for your part specifies, and in order to debug, I would connect just ONE segment at a time (one segment, not even one whole digit) and tinker with until you understand the behavior of your system. A datasheet helps because it will tell you what each pin is, and also how they are numbered.

Any success yet?

• Jeanne

Hi Natalia!

I just wanted to ask if I’m gonna use a common cathode 7-segment display,
will there be no changes other than the pin configuration?

• Hi Jeanne, also pay attention that the transistors will be different for common cathode or common anode displays. As for the pins, you also have to follow the datasheet of your display, as the configuration may be different than the one I used. Good luck!

• peter

hi
thanks for codes but i use arduino uno and it make a error
sketch_jan21d:46: error: ‘f’ was not declared in this scope
sketch_jan21d.ino: In function ‘void setup()’:
sketch_jan21d:58: error: ‘f’ was not declared in this scope

• Mark

how to count by tens….

ex. 10,20,30,40

• Alan

what’s the difference if i use a common cathode one?

• JustForFun

You replace this line with this:

const byte numbers[10] = { 0b0111111, 0b0000110, 0b1011011, 0b1001111, 0b1100110, 0b1101101, 0b1111101, 0b0000111, 0b1111111, 0b1101111 };

Basically you replace all zeros with ones and ones with zeros on this line if you are using a common cathode display. I ran into this issue since I have a common cathode display. Oddly enough I used the same PNP transitors recommended for the common anode display and my circuit still works.

plz I need coding and circuit connection diagram for 2 digit 7 segment timing display by arduino for my project help me

Hellow, i am following the instructions , and i was wondering, is it hard to add a button that after a set amount of time it’s pressed, it will deduct 1 from numbers?… example : hold it 3 seconds and from the number 99 it becomes 98 ?

• Ishan

Hi i test your project. thanks for sharing. and i have a trouble. normally no display any digits at the segment. and when i plug out the 2n3906 ‘s emitter pin 1 then display the segments in like a shadow. (number 1 show like E) like negative.

• Maleeha

What does these lines means:

const int segs[7] = {A,B,C,E,F_seg,G}
&
const byte numbers[10] = (ob10000000,……..,}

• Dalton

THANKS! Any chance you could help me with 1 small issue? It seems the buttons don’t always work, meaning if I press it quickly, it doesn’t change the display sometimes. Is there something in the code that can be changed to cause it to read the button state faster? thanks again…nice work!

• Jason Le

Thank you so much for this project . I made it.

I work correctly that I wanted.

However, there are many differences of position of A,B,C,D,E,F,G with that yours. But after all, I configure them again and it run very well.

• Jason Le

Thank you so much for this project . I made it.

I work correctly that I wanted.

However, there are many differences of position of A,B,C,D,E,F,G with that yours. But after all, I configure them again and it run very well.

Probably, I think you should add more 2 resistors to pin 13, 12 to avoid damaged the LED :).

• Akhrorjon

Is it possible to right a code by using assembly?

• idin

Thanks, it’s work. But I do not use that resistor. I just use 220ohm resistor after CA1 & CA2. I hope it’s safe.

• Bryan

Hi, Natalia
My daughter is working on a project for school (3rd grade). We thought a really cool added bonus to her display would be to build a 7 segment, 2 digit readout that must contain a constant running, random number generator. We would like it to pick a random number from 1-14, display it for a couple seconds, then move on to the next….
Is it possible to alter your sketch a little to make this doable, or do I need to look for a different direction? Obviously, my experience is minimal. I read a little about random numbers and pseudo codes, but it’s mostly theroy to me right now.
I know I have the ability to wire 14 individual leds to uno board and have 1 -14 randomly flash, buts not as impressive as actual numbers.

Thanks Bryan

• NITIN MISHRA

i can’t understand this 2 lines

“”unsigned long startTime = millis();
for (unsigned long elapsed=0; elapsed < 600; elapsed = millis() – startTime)""

• al2318

Is it possible for me to use assembly language only?

• Abdul Hafeez

Hi Natalia
I m a hobbist and thanks for sharing details of circuit. Its really helpful.