Keypad
Last updated
Last updated
A 12-button keypad can be used as an input for codes or commands. It has buttons for ten digits (0
-9
) and two symbols (*
,#
).
The keypad has 9 pinouts, but only the middle 7 are used. (The outer pinout on each side is not used.)
The keypad works by wiring the 12 buttons into 3 columns and 4 rows (thus 7 pinout wires). Pressing a button sends a signal through two wires representing the column and row of the key that was pressed. For example, key “8” is in column 2 and row 3 (which correspond to pinout wires 1 and 6).
Connect wires to each of the 7 pinouts, and then connect the other ends of the wires into different numbered rows in a breadboard. Then use jumper wires and resistors to connect the rows to the Photon.
Keypad
Photon Pin
Wire 1
D5
Wire 2
D3
Wire 3
D6, plus V-USB (5V) using 10K resistor
Wire 4
D0
Wire 5
D4, plus V-USB (5V) using 10K resistor
Wire 6
D1, plus V-USB (5V) using 10K resistor
Wire 7
D2, plus V-USB (5V) using 10K resistor
NOTE: Keypad wires 3, 5, 6, and 7 each have two connections. They each connect to an I/O pin, and they each connect to 5V (V-USB) through a 10K resistor. Look at the example wiring diagram for the photocell as a visual reference, since it has similar wiring (except the resistor for the keypad will connect to 5V instead of GND).
FYI: Notice that this part does not connect to GND, which is unusual.
A code library for the keypad has to be included in your Photon app:
In Particle Build, click on the bookmark icon to open the Libraries sidebar.
Type "keypad" into the search field. Select the result called: KEYPAD_PARTICLE
Click the button to "Include in Project"
Select the name of your Photon app, and then click the "Confirm" button
Particle Build will automatically insert an #include
statement at the top of your app code
This code library contains special functions that allow you to interact with the keypad.
In the global variables, you will need to declare variables that will help set up the keypad buttons for the Keypad library functions. You will also need to declare a Keypad object variable that you will use to run the special functions in the library. The example below declares an object variable called "keypad" (but you could use a different variable name).
You will probably also want to declare variables for a secret PIN code or commands that can be entered to control the device.
NOTE: Be sure to use single quotes when listing key characters (not double quotes).
There isn't any code that you need to include in the setup()
function for the keypad.
You can read a key press by using a keypad.getKey()
statement that will identify which key is being pressed. (If no key is being pressed, the statement returns a value of false.)
If you want to check for a specific key, you can compare the value of key
to a specific keypad character.
RECOMMENDED: Connect the speaker to provide audio feedback whenever any key is pressed. Within the if (key)
statement, play a brief tone if a key is pressed.
Notice that delay(100);
was included at the end of the if(key)
statement. This is because it takes a person a fraction of a second to physically press and release a key. Including a small delay (0.1 seconds) before checking again for a key press will allow the code to detect each key press as a separate event (otherwise, it might detect it as multiple presses of the same key). You may need to fine-tune the delay value based on user testing, so the delay is not too fast and not too slow.
You can also use a series of if-else statements to compare the key pressed against multiple keys, in order to do something different based on which key is pressed.
NOTE: Be sure to use single quotes when listing key characters (not double quotes).
Here is an example of a custom function called checkPIN()
that will detect whether a secret PIN code has been correctly entered. You will need to add instructions to do something if the secret code is correctly entered.
RECOMMENDED: Connect the speaker to provide audio feedback whenever any key is pressed. Within the checkCode()
function, play a brief tone if any key is pressed.