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

by Natalia

2-digit 7-segment displayThis month’s Arduino project is to build two 2-digit 7-segment LED 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).

Materials:

Sketch for counting up without buttons:

// www.TheElectronicsHobbyist.com/blog
// 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 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, 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, 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++) {
    int bit = bitRead(number, i);
    digitalWrite(segs[i], bit);
  }
}

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

You might also enjoy:

  1. Arduino 2-Digit 7-Segment Display with Buttons | Part 4
  2. Controlling a Seven-Segment Display Using Arduino Part 4
  3. Arduino 2-Digit 7-Segment Display Counter: Sketch | Part 3
  4. Controlling a Seven-Segment Display Using Arduino Part 3

{ 4 comments… read them below or add one }

Tom Dolan September 1, 2011 at 6:21 pm

??

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!

Reply

Natalia September 1, 2011 at 7:15 pm

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? :-)

Reply

sam January 25, 2012 at 6:23 pm

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

Reply

Natalia January 25, 2012 at 10:16 pm

Sam,

I haven’t modified my sketches to be compatible with Arduino release 1.0. According to the release notes here: http://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.

Reply

Leave a Comment

{ 1 trackback }

Previous post:

Next post: