How to Make a Number Increase Every Time You Press a Button on an LCD Screen with Arduino?

If you’re looking to learn how to use an Arduino to increase a number every time you press a button, this project is a perfect way to dive into both basic electronics and programming. In this tutorial, we’ll show you step-by-step how to set up a simple circuit with a button and an LCD screen. Every time the button is pressed, the number displayed on the LCD will increase by one.

    What You’ll Need:

    • Arduino Uno (or any compatible Arduino board)
    • 16×2 LCD with I2C module
    • Push button
    • Breadboard and jumper wires

    Circuit Setup:

    LCD Wiring (I2C):

    • VCC of the I2C module to 5V on the Arduino.
    • GND of the I2C module to GND on the Arduino.
    • SDA to A4 on the Arduino (for Uno boards).
    • SCL to A5 on the Arduino (for Uno boards).

    Button Wiring:

    • One leg of the button goes to GND.
    • The other leg goes to digital pin 7 on the Arduino.

    Code Explanation:

    Once you have everything wired up, it’s time to write the code. We’ll use the Wire library and LiquidCrystal_I2C library to communicate with the I2C LCD.

    #include <Wire.h>                // Include the Wire library for I2C
    #include <LiquidCrystal_I2C.h>    // Include the LiquidCrystal_I2C library
    
    // Pin Definitions
    const int buttonPin = 7;  // Pin connected to the button
    int buttonState = 0;      // Current state of the button
    int lastButtonState = 0;  // Previous state of the button
    int count = 0;            // Variable to store the current count
    
    // Initialize the LCD with I2C address (0x27 is common, check your module)
    LiquidCrystal_I2C lcd(0x27, 16, 2);  // 16 columns, 2 rows
    
    void setup() {
      // Initialize the LCD
      lcd.begin(16, 2);  
      // Ensure the backlight is on
      lcd.backlight();
      lcd.setCursor(0, 0);
      lcd.print("Press the button");
      
      // Set button pin as input
      pinMode(buttonPin, INPUT_PULLUP);
    }
    
    void loop() {
      // Read the state of the button
      buttonState = digitalRead(buttonPin);
      
      // Check if the button state has changed 
      if (buttonState == LOW && lastButtonState == HIGH) {
        // Short delay 
        delay(200);
    
        // Increment the count
        count++;
    
        // Clear the LCD and update the count
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Count: ");
        lcd.print(count);
      }
    
      // Save the current button state to detect changes in the next loop
      lastButtonState = buttonState;
    }
    

    How the Code Works:

    1. Libraries:
      • Wire.h: This library is used for I2C communication between the Arduino and the LCD.
      • LiquidCrystal_I2C.h: This library is used to control the LCD with I2C communication.
    2. Button State:
      • The code reads the state of the button connected to pin 7. If the button is pressed, the program detects a state change from LOW to HIGH.
    3. Debouncing:
      • Debouncing is a technique used to avoid multiple button presses being counted in quick succession. After detecting a button press, we introduce a short delay(50) to prevent the button from being read multiple times for one press.
    4. Count Incrementation:
      • Each time the button is pressed, the count variable is incremented, and the LCD is updated with the new count value.
    5. LCD Updates:
      • The lcd.clear() function is used to clear the screen each time the button is pressed, ensuring that the new count is displayed without old text overlapping.
      • lcd.setCursor(0, 0) positions the cursor to the first row, and lcd.print("Count: ") prints the label, followed by the updated count.

    Testing the Project:

    1. Upload the Code: Connect your Arduino to the computer and upload the code via the Arduino IDE.
    2. Press the Button: After uploading the code, press the button. The number displayed on the LCD should increment by 1 each time you press the button.
    3. Check the LCD: The LCD should display something like, Count: 1 After pressing the button again, it will show, Count: 2 And so on.

    Troubleshooting:

    • LCD not displaying anything:
      • Ensure that the I2C address is correct. The default I2C address for many LCDs is 0x27, but it can sometimes be 0x3F or another address. You can scan for the correct I2C address using an I2C scanner script.
      • Double-check the wiring of the SDA and SCL pins.
      • Make sure the contrast on the LCD is set properly (you can adjust this using a small potentiometer on your I2C module).
    • Button not working:
      • Verify that the button is correctly wired, with one leg to GND and the other leg to pin 7.

    Conclusion:

    In this tutorial, you’ve learned how to use an I2C LCD with an Arduino to display a number that increments each time you press a button. This is a great foundational project that introduces basic I2C communication, button input handling, and LCD control. Once you’re comfortable with this setup, you can modify it for more advanced projects, such as creating a counter, timer, or even a game interface.

    Enjoy experimenting with your new setup, and happy coding!