Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- To use a 4-Key Membrane Switch Keypad with an Arduino or an ESP32, you will need to connect each button to a digital input pin on the microcontroller. Here's an example of how to read the keypad input and print the key press to the Serial Monitor:
- Hardware Requirements:
- 4-Key Membrane Switch Keypad
- Arduino or ESP32 development board
- Jumper wires
- Connection:
- Connect the 4 pins of the membrane keypad to 4 digital input pins on the Arduino or ESP32. In this example, we'll use pins 2, 3, 4, and 5.
- This code sets up the digital input pins with internal pull-up resistors, reads the state of each button, and prints the button number to the Serial Monitor when a button is pressed. The code also includes a 200ms debounce delay to avoid false triggers or multiple triggers caused by mechanical vibrations in the switch.
- You can customize the code to perform different actions depending on the button pressed. For example, you could control LEDs, motors, or other components in response to the button inputs.
- */
- const int buttonPins[] = {2, 3, 4, 5};
- const int numButtons = sizeof(buttonPins) / sizeof(buttonPins[0]);
- void setup() {
- for (int i = 0; i < numButtons; i++) {
- pinMode(buttonPins[i], INPUT_PULLUP);
- }
- Serial.begin(115200);
- }
- void loop() {
- for (int i = 0; i < numButtons; i++) {
- int buttonState = digitalRead(buttonPins[i]);
- if (buttonState == LOW) {
- Serial.print("Button ");
- Serial.print(i + 1);
- Serial.println(" pressed");
- delay(200); // Debounce delay
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement