To detect button presses using a push button and display a message ("TOUCHED") on an LCD (I2C interface) whenever the button is pressed.
- Arduino board (e.g., Arduino Uno)
- 16x2 LCD with I2C interface
- Push button
- Resistors (if required)
- Jumper wires
- Breadboard
-
LCD (I2C Interface):
- VCC: Connect to Arduino 5V.
- GND: Connect to Arduino GND.
- SDA: Connect to Arduino A4 (Uno).
- SCL: Connect to Arduino A5 (Uno).
-
Push Button:
- One terminal: Connect to Arduino pin 7.
- Other terminal: Connect to GND.
- Enable internal pull-up by configuring pin 7 as
INPUT_PULLUP
.
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2); // Initialize the LCD with I2C address 0x3F, 16 columns, 2 rows
const int BUTTON_PIN = 7; // Pin connected to the push button
int lastState = LOW; // Stores the last state of the button
int currentState;
void setup() {
Serial.begin(9600); // Initialize serial communication for debugging
pinMode(BUTTON_PIN, INPUT_PULLUP); // Set the button pin as input with an internal pull-up resistor
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the LCD backlight
}
void loop() {
currentState = digitalRead(BUTTON_PIN); // Read the current state of the button
// Check if the button is pressed (transition from not pressed to pressed)
if (lastState == HIGH && currentState == LOW) {
Serial.println("Button pressed");
lcd.clear(); // Clear the LCD display
lcd.setCursor(0, 0); // Set the cursor to the first row, first column
lcd.print("TOUCHED"); // Display "TOUCHED" on the LCD
delay(500); // Keep the message for 500ms
lcd.clear(); // Clear the LCD display
}
lastState = currentState; // Update the last state
}
-
Button Detection:
- The push button is connected to pin 7 with an internal pull-up resistor enabled (
INPUT_PULLUP
). - When the button is pressed, the state changes from
HIGH
toLOW
, and this transition is detected in the code.
- The push button is connected to pin 7 with an internal pull-up resistor enabled (
-
LCD Interaction:
- The LCD (16x2) is initialized using the I2C library with address
0x3F
. - Upon button press, the message "TOUCHED" is displayed briefly before clearing the screen.
- The LCD (16x2) is initialized using the I2C library with address
-
Serial Monitoring:
- Each button press is logged in the Serial Monitor with the message "Button pressed" for debugging purposes.
-
Setup the Circuit:
- Connect the components as described in the Circuit Diagram section.
-
Upload the Code:
- Open the Arduino IDE.
- Copy the code into the IDE.
- Upload the code to the Arduino board.
-
Test the Setup:
- Power the Arduino board.
- Press the push button and observe the LCD displaying "TOUCHED" briefly.
- Open the Serial Monitor at a baud rate of 9600 to see the button press messages.
- When the button is pressed, it triggers a state change from
HIGH
toLOW
. - This triggers a message "TOUCHED" to be displayed on the LCD.
- The message is shown for 500ms and then cleared to be ready for the next button press.
-
No Response on Button Press:
- Verify the button connections.
- Ensure the correct pin number is defined for the button (
BUTTON_PIN
). - Check the internal pull-up resistor setting (
INPUT_PULLUP
).
-
No Display on LCD:
- Confirm the I2C address of the LCD (
0x3F
in this case). - Ensure proper connections to SDA and SCL pins.
- Confirm the I2C address of the LCD (
-
Serial Monitor Not Showing Output:
- Verify the baud rate of the Serial Monitor matches the code (
9600
). - Check the USB connection between the Arduino and your computer.
- Verify the baud rate of the Serial Monitor matches the code (
- If your LCD I2C address is different, scan for the correct address using an I2C scanner sketch.
- Adjust the delay times as needed for better user experience.