Raspberry Pi - Keypad 1x4

In this tutorial, we will learn how to use a keypad 1x4 with an Raspberry Pi. In detaile, we will learn:

Raspberry Pi Keypad 1x4

Hardware Preparation

1×Raspberry Pi 4 Model B
1×Keypad 1x4
1×Jumper Wires
1×(Optional) Screw Terminal Block Shield for Raspberry Pi
1×(Optional) USB-C Power Cable with On/Off Switch for Raspberry Pi 4B
1×(Optional) Plastic Case and Cooling Fan for Raspberry Pi 4B
1×(Optional) HDMI Touch Screen Monitor for Raspberry Pi

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Keypad 1x4

A 1x4 keypad is composed of four membrane buttons arranged in a single row. It is commonly used for user input in projects such as passcode entry, menu navigation, or control interfaces.

Pinout

The 1x4 keypad has 5 pins, which do not correspond directly to the key labels in order. Specifically:

  • Pin 1: connects to key 2
  • Pin 2: connects to key 1
  • Pin 3: connects to key 4
  • Pin 4: connects to key 3
  • Pin 5: is a common pin that connects to all keys
Keypad 1x4 Pinout
image source: diyables.io

Wiring Diagram

The wiring diagram between Raspberry Pi and Keypad 1x4

This image is created using Fritzing. Click to enlarge image

To simplify and organize your wiring setup, we recommend using a Screw Terminal Block Shield for Raspberry Pi. This shield ensures more secure and manageable connections, as shown below:

Raspberry Pi Screw Terminal Block Shield

Raspberry Pi Code

Each key on the 1x4 keypad functions as a button. This means we can use the digitalRead() function to check the status of each key. However, in practice, like with any button, we must handle the issue of bouncing, where a single press might be mistakenly detected as multiple presses. To avoid this, we need to debounce each key. This task becomes challenging when trying to debounce four keys without blocking other parts of the code. Fortunately, the ezButton library simplifies this process.

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect the Arduino Nano to the 1x4 keypad.
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Create a Python script file keypad_1x4.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-keypad-1x4 """ import RPi.GPIO as GPIO import time # Define GPIO pins for the keypad buttons PIN_KEY_1 = 24 # The Raspberry Pi pin GPIO24 connected to the key 1 PIN_KEY_2 = 23 # The Raspberry Pi pin GPIO23 connected to the key 2 PIN_KEY_3 = 8 # The Raspberry Pi pin GPIO8 connected to the key 3 PIN_KEY_4 = 25 # The Raspberry Pi pin GPIO25 connected to the key 4 KEY_PINS = [PIN_KEY_1, PIN_KEY_2, PIN_KEY_3, PIN_KEY_4] DEBOUNCE_TIME = 0.1 # 100 milliseconds # Setup GPIO mode GPIO.setmode(GPIO.BCM) # Setup GPIO pins as inputs with pull-up resistors for pin in KEY_PINS: GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Initialize previous states for each button previous_states = [GPIO.input(pin) for pin in KEY_PINS] # Function to read the state of the keypad buttons with debouncing def get_key_pressed(): pressed_key = 0 for i, pin in enumerate(KEY_PINS): current_state = GPIO.input(pin) if previous_states[i] == GPIO.HIGH and current_state == GPIO.LOW: # Detect state change from HIGH to LOW time.sleep(DEBOUNCE_TIME) # Wait for debounce time if GPIO.input(pin) == GPIO.LOW: # Ensure button is still pressed pressed_key = i + 1 break return pressed_key try: while True: key = get_key_pressed() if key: print(f"The key {key} is pressed") # Update the previous states previous_states = [GPIO.input(pin) for pin in KEY_PINS] time.sleep(0.1) # Delay for a short period to avoid excessive CPU usage except KeyboardInterrupt: # Cleanup GPIO settings before exiting GPIO.cleanup() print("Program terminated")
  • Save the file and run the Python script by executing the following command in the terminal:
python3 keypad_1x4.py
  • Press each key on the 1x4 keypad one by one
  • Check out the result on Terminal.
PuTTY - Raspberry Pi
The key 1 is pressed The key 2 is pressed The key 3 is pressed The key 4 is pressed

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!