Arduino counter: 7-segment display + a button

Task:

Create an Arduino circuit + write a program, using 2 components (LED, button, light sensor, servo etc.)

Components:

I decided to explore how to use a digit 7-segment digit so that it could count the number of times I press the button.
  • Arduino UNO + cable
  • 14 jumper wires (male-male)
  • Common cathode 7-segment display
  • Button
  • 2 resistors, 220 ohm
  • Breadboard

A piece of theory:

How does the display work? Well, seven segment displays are just 7 LEDs, connected together. They are called segments, and are arranged in the shape of an “8”. Most 7-segment displays actually have 8 segments, with a dot on the right side of the digit that serves as a decimal point. Each segment is named with a letter A to G, and DP for the decimal point: Single digit seven segment displays typically have 10 pins. Two pins connect to ground, and the other 8 connect to each of the segments. Here is a pin diagram of the one that I used:

Assembling:

Connect the pins described below:
  1. Arduino Pin 2 to Pin 9.
  2. Arduino Pin 3 to Pin 10.
  3. Arduino Pin 4 to Pin 4.
  4. Arduino Pin 5 to Pin 2.
  5. Arduino Pin 6 to Pin 1.
  6. Arduino Pin 8 to Pin 7.
  7. Arduino Pin 9 to Pin 6.
  8. GND to Pin 3 and Pin 8 each connected with 220-ohm resistors.

Code:

const int a = 8; //For displaying segment "a"
const int b = 9; //For displaying segment "b"
const int c = 4; //For displaying segment "c"
const int d = 5; //For displaying segment "d"
const int e = 6; //For displaying segment "e"
const int f = 2; //For displaying segment "f"
const int g = 3; //For displaying segment "g"

bool bPress = false;
const int buttonPin = 10;

// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button

void setup() {
// put your setup code here, to run once:
pinMode(a, OUTPUT); //A
pinMode(b, OUTPUT); //B
pinMode(c, OUTPUT); //C
pinMode(d, OUTPUT); //D
pinMode(e, OUTPUT); //E
pinMode(f, OUTPUT); //F
pinMode(g, OUTPUT); //G

pinMode( buttonPin , INPUT_PULLUP );
Serial.begin(9600);
displayDigit(buttonPushCounter);
}

void loop() {

buttonState = digitalRead(buttonPin);

// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == LOW) {
// if the current state is HIGH then the button went from off to on:
bPress = true;
buttonPushCounter++;
if( buttonPushCounter > 9) buttonPushCounter =0 ;
Serial.println("on");

} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;

if( bPress ){
turnOff();
displayDigit(buttonPushCounter);
}

}

void displayDigit(int digit)
{
//Conditions for displaying segment a
if(digit!=1 && digit != 4)
digitalWrite(a,HIGH);

//Conditions for displaying segment b
if(digit != 5 && digit != 6)
digitalWrite(b,HIGH);

//Conditions for displaying segment c
if(digit !=2)
digitalWrite(c,HIGH);

//Conditions for displaying segment d
if(digit != 1 && digit !=4 && digit !=7)
digitalWrite(d,HIGH);

//Conditions for displaying segment e
if(digit == 2 || digit ==6 || digit == 8 || digit==0)
digitalWrite(e,HIGH);

//Conditions for displaying segment f
if(digit != 1 && digit !=2 && digit!=3 && digit !=7)
digitalWrite(f,HIGH);
if (digit!=0 && digit!=1 && digit !=7)
digitalWrite(g,HIGH);

}
void turnOff()
{
digitalWrite(a,LOW);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
}







Results:

In the end, I got a circuit, where the display shows numbers from 0 to 9, which represent how many times the button was pressed.

Tags: , , ,

Comments (2)

  • Kamilia Iusupova

    |

    Too big featured image.

    Reply

  • Kamilia Iusupova

    |

    External access

    Reply

Leave a comment

address: 20 Myasnitskaya ulitsa
(metro stations ‘Lubyanka’ and ‘Kitay-Gorod’)
Moscow 101000 Russia

phone: +7(495)772-95-90 *15026

email: city@hse.ru