This was inspired by this Count to Pi project.
To use the 3x4 keypad, we will make use of the Arduino keypad library. In order to use that library, we must know which pins are used, which is accomplished with a multimeter and a bit of patience. Once this is determined, we put the details of our findings in the code, and everything will run smoothly.
The CodeThe code can be found here: https://github.com/mbwatson/pigame.
You will need to download and install the Arduino Keypad library from http://playground.arduino.cc/Code/Keypad. All that remains to be done it to configure the code to work with your matrix keypad. Actually this is the bulk of the remaining work, and it's not hard--just a little tedious perhaps.
Identify Keypad PinsYou can find out how to use your multimeter to determine whihc pins your keypad uses by following the instructions here: http://playground.arduino.cc/Main/KeypadTutorial.
My keypad has nine pins, and only makes use of the middle seven. Calling the leftmost pin Pin 1 and the rightmost pin Pin 9, my keypad uses Pin 2 through Pin 8. After following the instructions on the aforementioned page, I had a diagram like the following, allowing me to visualize how this thing works.
Keypad
======
1 2 3 _____ Pin 3
4 5 6 _____ Pin 8
7 8 9 _____ Pin 7
* 0 # _____ Pin 5
| | |
| | |______ Pin 4
| |________ Pin 2
|__________ Pin 6
Now we use some jumper wires to connect the used pins on the keypad to the desired pins on the Huzzah Feather. I used consecutive pins on the Huzzah, except I had to skip pin 15 and pin 0, as they are only for output. The diagram below details the complete mapping from the keypad to the GPIO pins on the Huzzah Feather.
Keypad GPIO
====== ======
1 2 3 _____ Pin 3 _______ Pin 12
4 5 6 _____ Pin 8 _______ Pin 4
7 8 9 _____ Pin 7 _______ Pin 5
* 0 # _____ Pin 5 _______ Pin 16
| | |
| | |______ Pin 4 _______ Pin 13
| |________ Pin 2 _______ Pin 14
|__________ Pin 6 _______ Pin 2
(I should note that the keypad in the schematic is not the keypad I used, but the process of determining the pins is the same no matter what keypad you use--also, I could only find a Fritzing part file for the one in the schematic.)
Configure Keypad LibraryThe following section of code tells the program how to interpret button presses on the keypad.
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = { 12, 4, 5, 16 };
byte colPins[COLS] = { 13, 14, 2 };
Basically this snippet says that a closure between, say, GPIO pins 4 and 2 indicates that 6 was pressed on the keypad. The array defined the characters that the getkey()
method will return. In this case, the character '6
' will be returned. Pretty neat.
With stacking headers wiring would be easier, but I don't have those around. Just be sure to connect the 3V and GND pins for power and the SDA and SCL pins (4 and 5, respectively) for data. Conveniently, these are the only pins required. Moreover, the data pins can be shared, so we connect the together with the keypad on the same pins. So nice.
Build the EnclosureI still need to figure out an enclosure for this thing. Any help on this front is welcome and would certainly be appreciated.
Comments