• Poornima Mani

Interfacing 4*4 Keypad with a Raspberry Pi

Updated: Aug 20, 2020

This article will teach you How to Interface 4*4 Keypad with a Raspberry Pi. The process is very simple and involves a keypad and its working will be taught, starting from the list of components to the complete working and hardware implementation. So stay tuned.


Here, you can see all the necessary hardware and software components required to finish this project.

Hardware Components: -

1. Raspberry Pi: #RaspberryPi is a single-board microcomputer which utilizes the SoC, DDR RAM memory, ethernet port, USB host, and micro HDMI on it. It provides the opportunity to interact with the outside world and using its processing power in a compact board at a low cost. In this application, we have used the Python IDE to simplify it.

2. Keypad - 4*4 matrix is used to input values into the project. This particular component has a total of 8 terminals, driven out from the 16 buttons present in the module.

3. SD Card for Raspberry Pi: It is important that we use the right MicroSD for storing and installing the Pi in the system. It is always recommended to use a class-10 SDcard which is capable of storing all the information regarding the Raspberry Pi from installation to application.

4. Connecting Wires (Male-Female): As simple as it looks, these are used to connect ends without soldering. While male wired have protruding pins to plug into things, female jumpers are the ends that are used to plug into.

5. USB Cable for Raspberry Pi: This cord is used to temporarily connect the Raspberry Pi to the power supply.


In the keypad, we have four rows and four columns in the matrix. Every key in the keypad corresponds to a particular row and column. The pins could be connected as per your wish. Here we have taken it as follows.

ROWS at : 5, 16, 20, 21

COLUMNS at: 6, 13, 19, 26

Schematic -

Hardware Circuit -


Initially, the GPIOs and time are imported after which the values of rows and columns are assigned.

Later we set up the row connections as output and columns as input and pull them down from high to low.

The function 'readLine' is used to read the lines and characters and the output line is set high. This loop runs and connects with every line GPIO. The rows as shown is assigned, thereby checked while the loop is run and the sleep function is used to give the desired time delay.

import RPi.GPIO as GPIO
import time

L1 = 16
L2 = 20
L3 = 21
L4 = 5

C1 = 6
C2 = 13
C3 = 19
C4 = 26


GPIO.setup(L1, GPIO.OUT)
GPIO.setup(L2, GPIO.OUT)
GPIO.setup(L3, GPIO.OUT)
GPIO.setup(L4, GPIO.OUT)

GPIO.setup(C1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(C2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(C3, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(C4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

def readLine(line, characters):
    GPIO.output(line, GPIO.HIGH)
    if(GPIO.input(C1) == 1):
    if(GPIO.input(C2) == 1):
    if(GPIO.input(C3) == 1):
    if(GPIO.input(C4) == 1):
    GPIO.output(line, GPIO.LOW)

    while True:
        readLine(L1, ["1","2","3","A"])
        readLine(L2, ["4","5","6","B"])
        readLine(L3, ["7","8","9","C"])
        readLine(L4, ["*","0","#","D"])

except KeyboardInterrupt:
    print("\nProgram is stopped")

Download Code -

Keypad (Main) code

Note: Please convert the file from .txt format to .py format before uploading the code to Raspberry Pi.


The connections are made and ensured that the row and column connections are implemented properly. If a user pushes a button linked to the line that is pulled high at the moment, the corresponding column is pulled high too. By reading the line and column pair,

you will decide which key has been pressed. Since power is not necessary for a working keypad, it is connected to any 8 pins.

Scroll right to see all the output images -

To see the practical implementation of the above-explained tutorial, watch the complete video. Also, let us know in the comments section if you face any issue or learned something new after you try it yourself.

Video by: Sai Parimal

Learn Electronics. (June 19, 2020). "Interfacing a 4*4 Keypad with Raspberry Pi". https://www.youtube.com/watch?v=Xgc-otkF4uY.

See Also

Check these out:

  1. Interfacing Fingerprint Sensor with Raspberry Pi

  2. Interfacing HC05 with Raspberry Pi