Question 11.8: You want to interface a keypad with your Raspberry Pi....

You want to interface a keypad with your Raspberry Pi.

Step-by-Step
The 'Blue Check Mark' means that this solution was answered by an expert.
Learn more on how do we answer questions.

Keypads are arranged in rows and columns, with a push switch on the intersection of each row or column. To find out which key is pressed, you first connect all the row and column connections to Raspberry Pi GPIO pins. So, for a 4 × 3 keypad, you will need four + three pins. By scanning each column in turn (setting it to output high) and reading the value of each of the row inputs, you can determine which (if any) key is pressed.

Note that keypads show considerable variation in their pinouts.

To make this recipe, you will need:

• Breadboard and jumper wires (see “Prototyping Equipment” on page 380)

• 4 × 3 keypad (see “Miscellaneous” on page 382)

• Seven male header pins (see “Miscellaneous” on page 382)

Figure 11-11 shows the wiring diagram for the project using the SparkFun keypad listed in “Miscellaneous” on page 382. The keypad is supplied without header pins, which must be soldered onto the keypad.

Open an editor (nano or IDLE) and paste in the following code. As with all the program examples in this book, you can also download the program from the Code section of the Raspberry Pi Cookbook website, where it is called keypad.py.

Before you run the program, make sure that the row and column pins are correct for the keypad that you are using, and if necessary change the values in the variables rows and cols. If you do not do this, then it is possible that pressing a key could short one GPIO output to another, where one is high and the other is low. This would likely damage your Raspberry Pi.

The row and columns defined here are correct for the SparkFun keypad listed in Appendix A. The first row is connected to GPIO pin 17, the second to 25, and so on. The wiring of the row and column to the keypad connector is illustrated in Figure 11-12.

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)

rows = [17, 25, 24, 23]
cols = [27, 18, 22]
keys = [
[‘1’, ‘2’, ‘3’],
[‘4’, ‘5’, ‘6’],
[‘7’, ‘8’, ‘9’],
[‘*’, ‘0’, ‘#’]]
for row_pin in rows:
GPIO.setup(row_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
for col_pin in cols:
GPIO.setup(col_pin, GPIO.OUT)
def get_key():
key = 0
for col_num, col_pin in enumerate(cols):
GPIO.output(col_pin, 1)
for row_num, row_pin in enumerate(rows):
if GPIO.input(row_pin):
key = keys[row_num][col_num]
GPIO.output(col_pin, 0)
return key
while True:
key = get_key()
if key :
print(key)
time.sleep(0.3)

You must run the program with superuser privileges, as it accesses the GPIO. You can see the trace from the program pressing each key in turn.

pi@raspberrypi ~ $ sudo python keypad.py
1
2
3
4
5
6
7
8
9
*
0
#

Discussion

The keys variable contains a map of the key name for each row and column position. You can customize this for your keypad.

Because there are quite a lot of pins to initialize as inputs or outputs, both the row and column pins are initialized in loops.

All the real action takes place in the get_key function. This enables each column in turn, by setting it to high. An inner loop then tests each of the rows in turn. If one of the rows is high, then the key name corresponding to that row and column is looked up in the keys array. If no keypress is detected, then the default value of key (0) is returned.

The main while loop just gets the key value and prints it. The sleep command just slows down the output.

See Also

An alternative to adding a keyboard is simply to use a USB keypad.

Table A-3. Prototyping equipment
Description Suppliers
M-M jumper wires SparkFun: PRT-08431, Adafruit: 759
M-F jumper wires SparkFun: PRT-09140, Adafruit: 825
F-F jumper wires SparkFun: PRT-08430, Adafruit: 794
Half-sized breadboard SparkFun: PRT-09567 Adafruit: 64
Pi Cobbler Adafruit: 1105
Table A-9. Miscellaneous
1200mAh LiPo battery Adafruit: 258
5V relay SparkFun: COM-00100
5V panel meter SparkFun: TOL-10285
Servo motor SparkFun: ROB-09065, Adafruit: 1449
5V 1A power supply Adafruit: 276
Low power 6V DC motor Adafruit: 711
0.1 inch header pins SparkFun: PRT-00116, Adafruit: 392
5 V 5-pin unipolar stepper motor Adafruit: 858
12 V, 4-pin bipolar stepper motor Adafruit: 324
Magician chassis with gearmotors SparkFun: ROB-10825
Tactile push switch SparkFun: COM-00097, Adafruit: 504
Miniature slide switch SparkFun: COM-09609, Adafruit: 805
Rotary encoder Adafruit: 377
4×3 keypad SparkFun: COM-08653
Piezo buzzer SparkFun: COM-07950, Adafruit: 160
11.11
11.12

Related Answered Questions

Question: 11.13

Verified Answer:

Use an RTC (real-time clock) module. A very common...
Question: 11.2

Verified Answer:

Record the last state of the button and invert tha...
Question: 11.1

Verified Answer:

Connect a switch to a GPIO pin and use the RPi.GPI...
Question: 11.7

Verified Answer:

Use a rotary (quadrature encoder) connected to two...
Question: 11.9

Verified Answer:

Use a PIR (passive infrared) motion detector modul...
Question: 11.12

Verified Answer:

The solution to this is very similar to that of us...
Question: 11.11

Verified Answer:

There are at least two ways to solve this problem....
Question: 11.10

Verified Answer:

A 3.3V serial GPS module can be connected directly...