Controlling a Seven-Segment Display Using Arduino Part 4

Posted February 22nd, 2010 by Natalia and filed in Arduino, Project

The third and final Arduino sketch uses bits to represent each segment and is a reduced code version of the previous sketch (1,210 bytes for sketch #3 instead of 1,852 bytes for sketch #2). A ten element array holds a byte for each number 0-9 that specifies what segments should be lit (pin low). Bit 0 corresponds to segment A, bit 1 to segment B and so on. In order to display the number 1, segments B and C need to be lit, so that is represented by the value 0b1111001. Function “lightSegments” reads these bits in sequence and sets the corresponding segments accordingly.

Sketch #3:

// www.TheElectronicsHobbyist.com/blog
// Natalia Fargasch Norman
// Seven-segment LED Display
// Common Anode pins 3 and 8

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

// 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 2
#define B 3
#define C 4
#define D 5
#define E 6
#define F 7
#define G 8

// Pushbutton connected to pin 9
#define BUTTON 9

int count = 0; // current display count

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(BUTTON, INPUT);
  lightSegments(0b1000000);
}

void loop() {
  int val = digitalRead(BUTTON);
  if (val == HIGH) {
    count++;
    if (count == 10) count = 0;
    delay(200);
    lightSegments(numbers[count]);
  }
}

void lightSegments(byte number) {
  for (int i = 0; i < 7; i++) {
    int bit = bitRead(number, i);
    // segments connected to Arduino pins 2-8
    digitalWrite(i+2, bit);
  }
}

Watch a short video of this project in action.

You might also enjoy:

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

3 Responses to “Controlling a Seven-Segment Display Using Arduino Part 4”

  1. [...] was used on the sketch for the single-digit 7-segment project a couple of months ago. It updates digit1 with the remainder of the division of itself by 10, which [...]

  2. Natalia says:

    Argh, somehow your comment slipped out of my radar. Sorry Emily for not replying before. You may have already gotten your answer somewhere else, but you can also check my post from June 1st 2010, 2-digit 7-segment display counter:
    http://www.theelectronicshobbyist.com/blog/2010/06/arduino-2-digit-7-segment-display-counter/

  3. Emily says:

    Hey, how would you write the code if you wanted two 7 segments so you could count to 10?

Leave a Reply