Keypad

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.

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.

Code for Keypad

#Include Library

A code library for the keypad has to be included in your Photon app:

  1. In Particle Build, click on the bookmark icon to open the Libraries sidebar.

  2. Type "keypad" into the search field. Select the result called: KEYPAD_PARTICLE

  3. Click the button to "Include in Project"

  4. Select the name of your Photon app, and then click the "Confirm" button

  5. Particle Build will automatically insert an #include statement at the top of your app code

// This #include statement was automatically added by the Particle IDE.
#include <Keypad_Particle.h>

This code library contains special functions that allow you to interact with the keypad.

Global Variables

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).

// global variables to set up keypad buttons
// must use single quotes for key characters
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = { D3, D2, D1, D0 };
byte colPins[COLS] = { D6, D5, D4 };

// create Keypad object variable called "keypad"
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

You will probably also want to declare variables for a secret PIN code or commands that can be entered to control the device.

// change to any secret PIN of any length
// must use single quotes for key characters
const int PIN_LENGTH = 4;
char secretPIN[PIN_LENGTH] = {'8','3','2','4'}; 

int correctCount = 0;
boolean correctPIN = false;

NOTE: Be sure to use single quotes when listing key characters (not double quotes).

setup( ) function

There isn't any code that you need to include in the setup() function for the keypad.

Read Key Press

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.

void loop() {
    char key = keypad.getKey();
    if (key) {
        tone(speakerPin, 3000, 25); // play sound as feedback when any key pressed
        if (key == '#') {
            // add code to do something if this key is pressed
        }
        delay(100);
    }
}

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.

void loop() {
    char key = keypad.getKey();
    if (key) {
        tone(speakerPin, 3000, 25); // play sound as feedback when any key pressed
        if (key == '1') {
            // add code to do something if this key is pressed
        } else if (key == '2') {
            // add code to do something else if this key is pressed
        } else if (key == '3') {
            // add code to do something else if this key is pressed
        } else {        
            // optional: add code to do something else if any other key pressed
        }
        delay(100);
    }
}

NOTE: Be sure to use single quotes when listing key characters (not double quotes).

Detect If Secret PIN Code Entered

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.

void loop() {
    checkPIN();
    if (correctPIN) {
        // do something if secret PIN code entered correctly
    }
}

// custom function to detect if secret PIN code entered
void checkPIN() {
    char key = keypad.getKey();
    // if key pressed, then check which key
    if (key) {
        tone(speakerPin, 3000, 25); // play sound as feedback when any key pressed
        // if key matches next key in secret PIN, add one to correct count
        if (key == secretPIN[correctCount]) {
            correctCount++; 
        } else {
            // else wrong key was entered (reset correct count)
            correctCount = 0; 
        }
        delay(100);
    }
    // check to see if complete secret PIN entered correctly
    if (correctCount == PIN_LENGTH) {
        correctPIN = true;
    }
}

Last updated