
Connect a Raspberry Pi Keypad – Code Lock
For the Arduino and Raspberry Pi there are some keypad matrixes that can be connected and read relatively easily. Similar to the numpad on a keyboard, these keypads have 3×4 or 4×4 keys. These modules can be used, for example, as code locks or for the construction of a small pocket calculator. With help of a few small changes, even a T9 keyboard can be created, with which texts can be typed like on older mobile phones.
In this tutorial, I will show the connection of such a keypad matrix and how to easily read them with the GPIOs. In order to use fewer GPIOs, it is also possible to connect it via the I2C Port Expander MCP23017.
Required Hardware Parts
As I already mentioned, there are several different models of these “membrane switches”, which mainly differ in size and labelling. In this tutorial, I used a 3×4 keyboard, but there is no problem adjusting it for a 4×4 (or larger) keypad.
The following models are available:
You should also have male-female jumper cables on hand.
It does not matter which Raspberry Pi model you use as long as you have at least 7 or 8 unused GPIOs.
Connection and Function of the Raspberry Pi Keypad
Basically all free and controllable GPIOs can be chosen for it. Depending on whether you have a 3×4 or 4×4 matrix membrane 7 or 8 pins will be occupied. Best suited for it are male-female jumper cables, which fit into the terminals of the keypad. The basic structure of such a keyboard matrix is as follows:
The first 4 pins from the left are here for the lines, the other 3 or 4 connections for one column. As soon as a key is pressed, two lines are connected. To find out and, if so, which key was pressed, a signal is sent in each case by the first four lines. The remaining pins are “tapped”.
If e.g. only key 6 was pressed, the lines “row 2” and “column 3” would be connected. So if we send a stream to the GPIO for line 2, only a high signal will be received at the GPIO for column 3. All other GPIOs receive no signal. So by sending a signal through all the rows and then checking all the columns, we can tell if a key was pressed and of course which one.
Well, as already mentioned, it does not matter which 7 or 8 GPIOs you use, but this must be adjusted later in the code if necessary. In this example, I use a 3×4 keypad, so I only have 7 GPIOs to allocate. From left to right the occupancy is as follows:
3×4 Keypad | Raspberry Pi |
---|---|
Pin 1 | GPIO 7 (Pin 26) |
Pin 2 | GPIO 8 (Pin 24) |
Pin 3 | GPIO 11 (Pin 23) |
Pin 4 | GPIO 25 (Pin 22) |
Pin 5 | GPIO 9 (Pin 21) |
Pin 6 | GPIO 10 (Pin 19) |
Pin 7 | GPIO 15 (Pin 10) |
Schematically it looks like this:
Code for steering the keypad
To be able to use the small keyboard now, we load ourselves a small library of GitHub. First we create a folder.
mkdir keypad-rpi && cd keypad-rpi
Then we download the file:
wget -O keypad.py https://raw.githubusercontent.com/rainierez/MatrixKeypad_Python/master/matrix_keypad/RPi_GPIO.py
If you are using a 4×4 keyboard or GPIOs different than me, you will need to adjust the GPIO numbers in the code accordingly (sudo nano keypad.py
).
Now we can use that class. To clarify the functionality, I have created a small example. To copy this, create a new file
sudo nano example.py
with the following code:
import time
import RPi.GPIO as GPIO
from keypad import keypad
GPIO.setwarnings(False)
if __name__ == ‘__main__’:
# Initialize
kp = keypad(columnCount = 3)
# waiting for a keypress
digit = None
while digit == None:
digit = kp.getKey()
# Print result
print digit
time.sleep(0.5)
###### 4 Digit wait ######
seq = []
for i in range(4):
digit = None
while digit == None:
digit = kp.getKey()
seq.append(digit)
time.sleep(0.4)
# Check digit code
print(seq)
if seq == [1, 2, 3, ‘#’]:
print “Code accepted”
|
You can save and quit the file with CTRL + O, CTRL + X.
If we execute the file now, the inputs can be selected.
sudo python example.py
In the following video you can see how the reading of the numbers can look like: