Raspberry Pi - Keypad - LCD

This tutorial instructs you how to use Raspberry Pi to display the input from keypad on LCD display.

Hardware Preparation

1×Raspberry Pi 4 Model B
1×LCD I2C
1×Keypad 3x4 and 4x4 Kit
1×Breadboard
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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. We appreciate your support.

Overview of Keypad and LCD

If you are unfamiliar with keypad and LCD (including pinout, functionality, programming, etc.), the following tutorials can help:

Wiring Diagram

The wiring diagram between Raspberry Pi and Keypad LCD

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Code

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 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
pip install lcddriver
  • Create a Python script file keypad_lcd.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-lcd import RPi.GPIO as GPIO from lcddriver import lcd from time import sleep # GPIO pins for the keypad rows and columns ROW_PINS = [17, 27, 22, 24] COL_PINS = [25, 8, 7] # I2C address 0x27, 16 columns, 2 rows LCD = lcd() # Mapping of keys on the keypad keys = [ ['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'], ['*', '0', '#'] ] def setup_gpio(): GPIO.setmode(GPIO.BCM) for row_pin in ROW_PINS: GPIO.setup(row_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) for col_pin in COL_PINS: GPIO.setup(col_pin, GPIO.OUT) def read_key(): for col_num, col_pin in enumerate(COL_PINS): GPIO.output(col_pin, GPIO.LOW) for row_num, row_pin in enumerate(ROW_PINS): if GPIO.input(row_pin) == GPIO.LOW: return keys[row_num][col_num] GPIO.output(col_pin, GPIO.HIGH) return None def display_key(key): LCD.lcd_clear() LCD.lcd_display_string(f"Key Pressed:", 1) LCD.lcd_display_string(f"{key}", 2) try: setup_gpio() while True: key = read_key() if key is not None: display_key(key) sleep(0.1) # Adjust the sleep duration as needed except KeyboardInterrupt: pass finally: GPIO.cleanup() LCD.lcd_clear()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 keypad_lcd.py

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

  • Press certain keys on the keypad
  • Check out the outcome on the LCD display

If the LCD display is not showing anything, check out Troubleshooting on LCD I2C . for assistance.

Code Explanation

Check out the line-by-line explanation contained in the comments of the source code!

Video Tutorial