≡ Menu

Controlling a Seven-Segment Display Using Arduino Part 3

Seven segment display with Arduino

* 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; this post
Part 4: Sketch #3, bit manipulation

The second sketch cycles through the numbers from 0 to 9, but only increments the display counter each time a button is pressed. Note that this code includes simple debouncing by introducing a short delay when the Arduino detects that the button has been pressed.

Sketch #2:

// 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 => ABCDEF
// 1 => BC
// 2 => ABDEG
// 3 => ABCDG
// 4 => BCFG
// 5 => ACDFG
// 6 => ACDEFG
// 7 => ABC
// 8 => ABCDEFG
// 9 => ABCDFG

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

// Common anode;
// on when pin is low
// and off when pin is high
#define ON LOW
#define OFF HIGH

int count = 0; // current display count
int val = 0;   // digital input from button

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);
  zero();
}

void loop() {
  val = digitalRead(BUTTON);
  if (val == HIGH) {
    count++;
    delay(200);
    switch (count) {
      case 0:
        zero();
        break;
      case 1:
        one();
        break;
      case 2:
        two();
        break;
      case 3:
        three();
        break;
      case 4:
        four();
        break;
      case 5:
        five();
        break;
      case 6:
        six();
        break;
      case 7:
        seven();
        break;
      case 8:
        eight();
        break;
      case 9: {
        nine();
        count = -1;
        break;
      }
    }
  }
}

// 0 => ABCDEF
void zero() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(F_SEG, ON);
  digitalWrite(G, OFF);
}

// 1 => BC
void one() {
  digitalWrite(A, OFF);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, OFF);
  digitalWrite(E, OFF);
  digitalWrite(F_SEG, OFF);
  digitalWrite(G, OFF);
}

// 2 => ABDEG
void two() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, OFF);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(F_SEG, OFF);
  digitalWrite(G, ON);
}

// 3 => ABCDG
void three() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, OFF);
  digitalWrite(F_SEG, OFF);
  digitalWrite(G, ON);
}

// 4 => BCFG
void four() {
  digitalWrite(A, OFF);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, OFF);
  digitalWrite(E, OFF);
  digitalWrite(F_SEG, ON);
  digitalWrite(G, ON);
}

// 5 => ACDFG
void five() {
  digitalWrite(A, ON);
  digitalWrite(B, OFF);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, OFF);
  digitalWrite(F_SEG, ON);
  digitalWrite(G, ON);
}

// 6 => ACDEFG
void six() {
  digitalWrite(A, ON);
  digitalWrite(B, OFF);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(F_SEG, ON);
  digitalWrite(G, ON);
}

// 7 => ABC
void seven() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, OFF);
  digitalWrite(E, OFF);
  digitalWrite(F_SEG, OFF);
  digitalWrite(G, OFF);
}

// 8 => ABCDEFG
void eight() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, ON);
  digitalWrite(F_SEG, ON);
  digitalWrite(G, ON);
}

// 9 => ABCDFG
void nine() {
  digitalWrite(A, ON);
  digitalWrite(B, ON);
  digitalWrite(C, ON);
  digitalWrite(D, ON);
  digitalWrite(E, OFF);
  digitalWrite(F_SEG, ON);
  digitalWrite(G, ON);
}
{ 15 comments… add one }
  • edgar

    Hola quería preguntar si hay otra manera más fácil de utilizar los pines para que se escriban de forma conjunta como escribir un byte en el PORTB de PIC por ejemplo output_B(BYTE); esto se me hace necesario para utilizar dos display con la técnica de multiplexacion.

  • Edgard,
    Usted puedes hacer asi:
    PORTD = B10101000; // sets digital pins 7,5,3 HIGH
    Más exemplos, aceda:
    https://www.arduino.cc/en/Reference/PortManipulation
    Abraços,
    Renato

  • Adrian

    Its this correct
    // Common Anode pins 3 and 8

    And where can i connect the button? from pin 5V to ground and then to the pin 9…

    I don’t get it

    • Adrian,
      common anode pins 3 and 8 is correct for the display I used. Yours might be different, so if your setup is not working, check the datasheet for the part you are using.
      As for the button you will connect one lead to ground, and the other to pin 9 on the Arduino and to 5V using a 10K resistor.

      • Adrian

        I got it, you’r right my display setup it’s a little bit different, i make it the project today and it works. But i just have a little issue, when i press de button sometimes count and sometimes don’t i think its a issue with the debouncing. Wath do you think.
        Thanks Nat. And sorry for my grammar, greeting from Mexico.

        • I’m glad it works now, Adrian. The simplest way to work around bouncing is to add a short delay when you read from the button, so play around with the amount of time and see what works for you.

  • 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

  • darli

    I try it but it no compiled, it keep saying that ” F was not declared on this scope”.
    Could you help me with?
    thanks

  • Thanks for the tutorial Natalie. I have also written a short tutorial on five different methods to control a 7Seg LED with an Arduino if anyone is interested
    https://www.pedroduino.com/7SegLEDcircuits.php
    Pedro.

  • keren hovav

    hello , I need your help, maybe you could help me !
    I am doing a project and I want to display a tempeture that I am reading from a sensore ( lm35) . unfortunately I dont know how to control each digit . can you help me and solve my problems by telling me how can I display for instance the number 32 ?
    I allready did the “math” I just need to know if I can control reach digit alone ?

    thank you very much !

    *my code by far:*

    int pinA = 2;
    int pinB = 4;
    int pinC = 7;
    int pinD = 8;
    int pinE = 10;
    int pinF = 12;
    int pinG = 13;

    int pinNumber1 = 9;
    int pinNumber2 = 1;

    int number1=0;
    int middleNumber=0;
    int number2=0;

    int pinTempurure=A0;
    int homeTempeture=0;
    void setup() {
    Serial.begin (9600);
    pinMode (pinA , OUTPUT);
    pinMode (pinB , OUTPUT);
    pinMode (pinC , OUTPUT);
    pinMode (pinD , OUTPUT);
    pinMode (pinE , OUTPUT);
    pinMode (pinF , OUTPUT);
    pinMode (pinG , OUTPUT);
    pinMode (pinNumber1 , OUTPUT);
    pinMode (pinNumber2 , OUTPUT);
    pinMode (pinTempurure , INPUT);
    }

    void loop() {
    homeTempeture=analogRead(pinTempurure);
    int mv = (homeTempeture/1024.0)*5000;
    int cel = mv/10;
    number1=cel*0.1;
    middleNumber= cel/10;
    number2=cel-(middleNumber*10);

    Serial.print (“the tempeture”);
    Serial.println (cel);
    Serial.print (“first digit”);
    Serial.println (number1);
    Serial.print (“second digit”);
    Serial.println (number2);

Leave a Comment