Raspberry Pi Pico - Keypad 4x4

This guide will show you how to use a Raspberry Pi Pico with a 4x4 keypad. We will go through the following steps:

Raspberry Pi Pico 4x4 Keypad

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×Keypad 4x4
1×Jumper Wires
1×Breadboard
1×(Optional) Screw Terminal Expansion Board for Raspberry Pi Pico

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 4x4 Keypad

The keypad includes 16 soft buttons organized in rows and columns, forming a grid. Each button is referred to as a key.

Pinout

A 4x4 keypad uses 8 pins, split into two types: rows and columns.

  • 4 pins connect to the rows (R1, R2, R3, R4).
  • 4 pins connect to the columns (C1, C2, C3, C4).
4x4 Keypad Pinout

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico Keypad 4x4

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Pico Code

from DIYables_MicroPython_Keypad import Keypad import time NUM_ROWS = 4 NUM_COLS = 4 # Constants for GPIO pins ROW_PINS = [13, 12, 11, 10] # The Raspberry Pi Pico pin (GP1) connected row pins COLUMN_PINS = [9, 8, 7, 6] # The Raspberry Pi Pico pin (GP1) connected column pins # Keymap corresponds to the layout of the keypad 4x4 KEYMAP = ['1', '2', '3', 'A', '4', '5', '6', 'B', '7', '8', '9', 'C', '*', '0', '#', 'D'] # Initialize the keypad keypad = Keypad(KEYMAP, ROW_PINS, COLUMN_PINS, NUM_ROWS, NUM_COLS) keypad.set_debounce_time(400) # 400ms, addjust it if it detects twice for single press print("Keypad 4x4 example") # Main loop to check for key presses while True: key = keypad.get_key() if key: print("Key pressed: ", key)

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Connect the 4x4 keypad to the Raspberry Pi Pico as shown in the diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Navigate to the Tools Manage packages on the Thonny IDE.
  • Search “DIYables-MicroPython-Keypad”, then find the Keypad library created by DIYables.
  • Click on DIYables-MicroPython-Keypad, then click Install button to install Keypad library.
Raspberry Pi Pico Keypad library
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Press some keys on the 4x4 keypad.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Keypad 4x4 example Key pressed: 1 Key pressed: 2 Key pressed: 3 Key pressed: 4 Key pressed: A Key pressed: B Key pressed: C Key pressed: * Key pressed: #
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

If it does not work, please check your wiring carefully. It is very easy to confise when connecting Raspberry Pico to the 4x4 keypad.

If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonnys's Shell.

Keypad and Password

A keypad is often used to type a password. Here, we use two special keys:

  • A key to begin or re-enter the password, like the "*" key.
  • A key to complete the password entry, like the "#" key.

The password will consist of other keys, except for the two special keys. When you press a key:

  • If the key is neither "*" nor "#", include it in the password being typed.
  • If the key is "#", verify if the typed password is correct. Then, reset the password.
  • If the key is "*", reset the password.

Keypad - Password Code

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-keypad-4x4 """ from DIYables_MicroPython_Keypad import Keypad import time NUM_ROWS = 4 NUM_COLS = 4 # Constants for GPIO pins ROW_PINS = [13, 12, 11, 10] # The Raspberry Pi Pico pin (GP1) connected row pins COLUMN_PINS = [9, 8, 7, 6] # The Raspberry Pi Pico pin (GP1) connected column pins # Keymap corresponds to the layout of the keypad 4x4 KEYMAP = ['1', '2', '3', 'A', '4', '5', '6', 'B', '7', '8', '9', 'C', '*', '0', '#', 'D'] # Initialize the keypad keypad = Keypad(KEYMAP, ROW_PINS, COLUMN_PINS, NUM_ROWS, NUM_COLS) keypad.set_debounce_time(400) # 400ms, addjust it if it detects twice for single press # Define the correct password correct_password = "1234A" input_password = "" print("Keypad 4x4 password") # Main loop while True: key = keypad.get_key() if key: print(key) if key == '*': input_password = "" # Clear input password elif key == '#': if input_password == correct_password: print("Password is correct") # DO YOUR WORK HERE else: print("Password is incorrect, try again") input_password = "" # Clear input password else: input_password += key # Append new character to input password string
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico
  • Type the keys "123" and then hit "#".
  • Type the keys "1234A" and then hit "#".
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot MPY: soft reboot Keypad 4x4 password 1 2 3 # Password is incorrect, try again 1 2 3 4 A # Password is correct
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

Video Tutorial

※ 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!