Arduino Push Button Counter Code LCD Circuit

A counter is a device that counts the number of times when a particular event occurs.

Here we count the number of times the push switch has been pressed. The Arduino detects a transition of input from a LOW state to the HIGH state during switch press; that is the value of counting variable increments for a positive edge triggering.

In the circuit, the push switch is connected to a digital pin of the Arduino; here at pin 9. When the push switch has pressed the LED ON for half a seconds and then OFF, it is provided just for an indication that the switch press has been detected or the value has been incremented by one.

The counter is designed for a positive edge trigger, hence it only increments the count variable on a positive edge irrespective of how long the switch is held ON. In order to do that we have added a variable called “prestate”. The counter value increments only when two conditions are satisfied, that is the input state is high and the value of prestate is 0. Once the switch is pressed, along with incrementing and flashing the LED the value of prestate also set to 1; as you can see in the code area inside the if condition.

As long as the switch is held ON the prestate remains 1 and it sets to 0 only when the switch is released. So the code allows us to increment the variable only for positive edges of the input. This logic is similar to a toggle switch which ON and OFF the output for alternative switch ON of the same input; usually a toggle switch responds for a positive edge trigger.

Arduino counter code

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  9;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int count_value =0;
int prestate =0;
void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, then the buttonState is HIGH:
  if (buttonState == HIGH && prestate == 0) {
    count_value++;
    Serial.println(count_value);
    // turn LED on
    digitalWrite(ledPin, HIGH);
    delay(100);
    // turn LED off
    digitalWrite(ledPin, LOW);

    prestate = 1;
  } else if(buttonState == LOW) {
    prestate = 0;
  }
}

If we use code as given below, without a variable to detect the previous state then the counter value keeps incrementing as long as the switch is held ON.

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    count_value++;
    delay(100);
  }
}

Push button up down counter Arduino

The arrangement is similar to above, the only difference is an additional input switch and a few lines of code to add the decrement function to the counter. Here, one switch press increments the value whereas the seconds switch decrements the value.

Here in the code, when we press the first switch the first “if” condition becomes true as the input at pin2 is high and the prestate is 0. Once the switch is held on the variable prestate is 1 and stops further counting. Even if we press the second switch it doesn’t decrement the variable as the prestate is still 1 and its value doesn’t change to 0 unless both switches are released.

So in order to increment or decrement both the inputs must be at a low state before applying the high state at any of the inputs.

Code for increment and decrement counter

onst int buttonPin1 = 2, buttonPin2 = 3;  // the number of the pushbutton pins
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int button1_State = 0, button2_State = 0;  // variable for reading the pushbuttons status
int count_value =0;
int prestate =0;
void setup() {
  // initialize the LED pin as an output
  //LED flashes during both increment and decrement of counter
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  Serial.begin(9600);
}

void loop() {
  // read the state of the pushbutton
  button1_State = digitalRead(buttonPin1);
  button2_State = digitalRead(buttonPin2);
  // counter increment if the pushbutton 1 is pressed.
  if (button1_State == HIGH && prestate == 0) {
    count_value++;
      Serial.println(count_value);
    // turn LED on
    digitalWrite(ledPin, HIGH);
    delay(200);
     // turn LED off
    digitalWrite(ledPin, LOW);
    prestate = 1;
  }
// counter decrement if the pushbutton 2 is pressed.
  else if (button2_State == HIGH && prestate == 0) {
    count_value--;
      Serial.println(count_value);
    // turn LED on
    digitalWrite(ledPin, HIGH);
    delay(100);
     // turn LED off
    digitalWrite(ledPin, LOW);
    prestate = 1;
  } 
 else if(button1_State == LOW && button2_State == LOW) {
    prestate = 0;
  }
}

Up Down Counter with LCD display

In the above codes, the counter value was displayed on the serial monitor, the same can displayed on an LCD display.

The below code is for an LCD interface with the Arduino using an I2C module, refer to the Arduino LCD interface to use an LCD display with or without an I2C adapter.

#include <LiquidCrystal_I2C.h>
const int buttonPin1 = 2, buttonPin2 = 3;    
int button1_State = 0, button2_State = 0;
int count_value =0;
int prestate =0;
LiquidCrystal_I2C lcd(0x27,16,2);
void setup() {
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(4,0);
  lcd.print("Counter");
  lcd.setCursor(2,1);
  lcd.print(count_value);
}

void loop() {
  // read the state of the pushbutton value:
  button1_State = digitalRead(buttonPin1);
  button2_State = digitalRead(buttonPin2);
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (button1_State == HIGH && prestate == 0) {
    count_value++;  
  lcd.setCursor(0,1);
  lcd.print("> ");
  lcd.print(count_value);
  lcd.print(" ");
    prestate = 1;
  }
//decrement
  else if (button2_State == HIGH && prestate == 0) {
    count_value--;
  lcd.setCursor(0,1);
  lcd.print("< ");
  lcd.print(count_value);
  lcd.print(" ");
   prestate = 1;
  } 
 else if(button1_State == LOW && button2_State == LOW) {
    prestate = 0;
  }
}
Previous
Previous

Timer switch with Relay and LCD Arduino code

Next
Next

Voltage Sensor with Arduino – Measure up to 25V using Arduino