≡ Menu

Controlling a Seven-Segment Display Using Arduino Part 4

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

Part 1: Overview
Part 2: Bill of Materials and sketch #1
Part 3: Sketch #2, using a pushbutton
Part 4: Sketch #3, bit manipulation; this post

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.TinkerHobby.com
// 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_SEG 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_SEG, 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.

{ 19 comments… add one }
  • Emily

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

  • 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:
    https://www.tinkerhobby.com/arduino-2-digit-7-segment-display-counter/

  • Lipzy

    Hello
    Will can give me a hand?
    I want to change this schedule. Make each of the numbers, do the same time that shows on the display.
    Example:
    Display value 5, the procedure is repeated five times controlled by a censor. When account is activated. for the coming five times.
    Use the button to set the desired value and another button will confirm the value and start the procedure. Repeated until the desired times.
    Thank you

    • Sorry Lipzy, I’m not sure I understand your question… but if you want the input value to determine how many times an event should occur, you could read how many times the button was pressed and store that value in a variable. Later on you would use that variable as your for loop control counter. But I’m not sure of what you want to do. Feel free to clarify and I’ll try again to help! Good luck!

  • Lipzy

    Yes that is exactly what he intended.
    Does it help me?
    I apologize for my English, no big deal!
    Thank you.

    • Awesome. No need to apologize on the English… I understand! I am from Brazil and English is not my first language either. 🙂

  • Lipzy

    Então podemos falar Português. 🙂
    Assim é melhor! Isto para nos! Será que podemos falar por MSN?
    Obrigado

  • benjamin

    sketch_nov09a:52: error: ‘F’ was not declared in this scope Arduino program finds this error ?

    • Hi Benjamin,

      I haven’t modified my sketches to be compatible with Arduino release 1.0 and beyond. 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. So the #define cannot use that same symbol.

      Therefore on the line that says:

      #define F 4

      just replace “F” with another identifier (for instance F_SEG) 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.

  • benjamin

    thank you very much, solved the problem!

  • Entei

    Olá, gostaria de saber se tinha como enviar uma imagem em boa resolução da protoboard e o arduino com uma visualização clara das portas e cabos. Desde já grato pelo material disponível.

    • Entei, procurei pelas imagens originais, mas infelizmente nao as encontrei… Sinto muito. 🙁 Voce ja usou o software Fritzing? (fritzing.org) Ele ajuda bastante na visualizacao do circuito.

      • Entei

        Hum, ainda não, nem tinha ouvido falar desse software ainda (até agora) vou testa-ló e ver no que consigo fazer =) desde já Grato novamente!

  • ogah

    hi,
    pls help me on this project.
    i want to use 2 buttons to control 2 seven segment display. the first button will count up any number between 00-99 and when the second button is pressed,it start counting down from that value to 00.
    thanks for anticipated repy

  • dg

    Hi. How can you increase the speed when the button is long pressed?

Leave a Comment