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:
- Include Libraries:
- We include
Wire.h
for I2C communication andLiquidCrystal_I2C.h
for controlling the LCD over I2C.
- We include
- Initialize the LCD:
- We create an LCD object using
LiquidCrystal_I2C lcd(0x27, 16, 2);
, where0x27
is the I2C address of the LCD, and16, 2
represents the number of columns and rows on the screen.
- We create an LCD object using
- Set Button Pin:
- The button is connected to pin
2
, which we define as an input usingpinMode(buttonPin, INPUT);
.
- The button is connected to pin
- 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 usingdigitalRead(buttonPin);
. - If the button is pressed (when
buttonState == HIGH
), it will display “Button Pressed”. If the button is not pressed (whenbuttonState == LOW
), it will show “Button Released”.
- The LCD is initialized in the
- Debouncing:
- A small delay (
delay(200)
) is used to reduce flickering by allowing the button state to stabilize between checks.
- A small delay (
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.