How to Display Button Press Status on an LCD using Arduino?

In this tutorial, we’ll learn how to show the status of a button (pressed or not pressed) on an LCD screen connected to an Arduino board. The LCD will use an I2C (Inter-Integrated Circuit) interface, which simplifies the wiring and programming. We will write a small program that updates the LCD to display whether the button is currently pressed or not.

Components Needed:

  • Arduino board (e.g., Arduino Uno)
  • LCD display with I2C module (1602 or 2004 LCD)
  • Push-button switch
  • 10kΩ resistor (for the pull-down resistor)
  • Jumper wires
  • Breadboard

Wiring Connection:

Before diving into the code, let’s make sure the components are connected correctly.

  • LCD I2C Wiring:
    • SDA (Data) on the LCD connects to the A4 pin on Arduino (Uno).
    • SCL (Clock) on the LCD connects to the A5 pin on Arduino (Uno).
    • VCC connects to 5V on Arduino.
    • GND connects to GND on Arduino.
  • Button Wiring:
    • One leg of the push button connects to pin 2 on the Arduino.
    • The other leg connects to GND.
    • A 10kΩ resistor is used between pin 2 and 5V to ensure a stable high state when the button is not pressed.

Arduino Code:

Here is a simple sketch that shows how to read the state of a button and display whether it’s pressed or not on an I2C LCD.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Initialize the LCD object with I2C address 0x27 and size 16x2
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Define the button pin
const int buttonPin = 2;

// Variable to store the button state
int buttonState = 0;

void setup() {
  // Initialize the button pin as an input
  pinMode(buttonPin, INPUT);
  
  // Initialize the LCD
  lcd.begin(16, 2);
  
  // Ensure the backlight is on
  lcd.backlight();
  
  // Display a welcome message
  lcd.setCursor(0, 0);
  lcd.print("Button Status:");
}

void loop() {
  // Read the state of the button
  buttonState = digitalRead(buttonPin);
  
  // Clear the second line and reset the cursor position
  lcd.setCursor(0, 1);
  
  // Check if the button is pressed or not and display accordingly
  if (buttonState == LOW) {
    lcd.print("Button Pressed ");
  } else {
    lcd.print("Button Released");
  }

  // Add a small delay to avoid flickering
  delay(200);
}

Code Breakdown:

  1. Include Libraries:
    • We include Wire.h for I2C communication and LiquidCrystal_I2C.h for controlling the LCD over I2C.
  2. Initialize the LCD:
    • We create an LCD object using LiquidCrystal_I2C lcd(0x27, 16, 2);, where 0x27 is the I2C address of the LCD, and 16, 2 represents the number of columns and rows on the screen.
  3. Set Button Pin:
    • The button is connected to pin 2, which we define as an input using pinMode(buttonPin, INPUT);.
  4. Display Information:
    • The LCD is initialized in the setup() function. We display the text “Button Status:” to notify the user of the current status.
    • In the loop() function, we constantly check the state of the button using digitalRead(buttonPin);.
    • If the button is pressed (when buttonState == HIGH), it will display “Button Pressed”. If the button is not pressed (when buttonState == LOW), it will show “Button Released”.
  5. Debouncing:
    • A small delay (delay(200)) is used to reduce flickering by allowing the button state to stabilize between checks.

Testing the Circuit:

Once the circuit is connected and the code is uploaded to your Arduino, the LCD should display “Button Status” in the top row. When the button is not pressed, the bottom row should show “Button Released.” When you press the button, it should change to “Button Pressed.”

Troubleshooting:

  • LCD not showing any text:
    • Ensure the LCD is connected correctly with proper SDA and SCL lines. You can try running an I2C scanner sketch to confirm the LCD’s I2C address.
  • Button not responding:
    • Double-check the button connections. Ensure the 10kΩ resistor is placed between pin 2 and 5V. The button must pull the pin low when not pressed.

Conclusion:

In this tutorial, we learned how to display the status of a button on an LCD with an I2C interface using Arduino. The I2C communication simplifies the wiring, and the code is simple to adapt for other projects. With this setup, you can easily monitor the status of various inputs or sensors and display them on the LCD screen.