Dial-A-Song: Part 2

For part two of the Dial-A-Song project, I’m going to go through the hookup of the phone’s keypad so that it can act as an input to the Raspberry Pi.

The first thing to do was remove the short ribbon cable that came on the keypad and replace it with something a little longer so I could more easily hook it up to the Pi. Here’s the old ribbon cable:

Dial Pad and Tone Generator

And now with the new, more colorful, cable:

Keypad Wiring

On the other end, I attached a 2x5 IDC female header (I didn’t have a 2x4, so there’s a couple wasted pins). This is what will eventually connect to the Pi interface board I’ll create, but for now I mocked up a breakout board (the green perf-board) so I could connect it to a breadboard along with Adafruit’s great “Cobbler” kit.

The keypad’s internal wiring is setup as shown below (which the red wire being pin 1 and increasing to the left, from the image above):

5 6 7
4|1|2|3|
3|4|5|6|
2|7|8|9|
1|*|0|#|

Most matrix keypads are very similar so, the keypad pins should be connected to the Raspberry Pi GPIO as follows:

Pad | Pi
________
1 - 25
2 - 24
3 - 23
4 - 18
5 - 22
6 - 17
7 - 4

Now for the software. Originally, I was going to use a library designed for specifically this purpose. It did exactly what it said it would with the exception that it uses polling to detect the keypads. This works, but required a significant amount of the CPU resources to get any decent response time. Often around 80%. This was not acceptable.

So, I decided to try converting the library to use the interrupt capability of the RPi.GPIO library that comes pre-installed on Raspbian. It took a little fiddling because the Pi GPIO pins are really susceptible to noise making button debouncing a real issue. But in the end, it works quite well:

It’s super simple to use as you can see from the code below. Instantiate the class with a callback function and that function gets called when there’s a keypress on the pad. Just note that the callback will be running on the context of a different thread from the main thread. While it uses interrupts, RPi.GPIO cheats a little bit and the interrupts are actually running on background threads and so will the callback. Not usually a big problem but something to be aware of.