Keypad

Sometimes your application requires a keypad to enter some user data. A good example of this is the signal generator. Many Arduino programs use either a 3×4 or 4×4 keypad like show below. Typically the keypad consists of 12 or 16 keys and 7 or 8 connections instead of 12 or 16 respectively. The connections are scanned by the program and if one keys is pressed the row and column connections in the matrix are connected. This way the specific key can be deduced and acted upon.

Example of a 4×4 matrix keypad.


Installing the keypad library

It might be that the keypad library is not installed in your Arduino IDE. If this the case you will get a compiler warning when you try to compile the keypad example below. If so then from the Arduino IDE select Menu | Sketch | Include Library | Manage Libraries  … Then type “keypad” next to the Topic ALL to narrow the search result and select the Keypad library by Mark Stanley and Alexander Brevig as there are many keypad libraries available.


Keypad in loop()

Below is an example of a keypad that echoes the pressed key on the USB port.

If you haven’t heard about the Arduino yield() function you may even consider including the part of the code in the loop() in the yield() function so the keypad will be scanned more often.


Keypad with “enter”

Instead of just echoing the keys pressed why not wait until an “enter key” has been pressed? On a 4×4 keypads the “enter key” is often the pound sign or number key “#”. The below example collect the incoming keys pressed and when the #-key is pressed the buffer is printed on the USB port. Furthermore, when the “password”is entered a confirmation message is printed. Instead of printing a message the correct sequence could be used to perform some action in the program.