You want to interface a keypad with your Raspberry Pi.
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] |
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 |