First thing we will do is explain how keypads work. Their simplicity might surprise you! Then we’ll walk through setting up a passcode on the Arduino that will print out numbers on the serial console as we type them on the keypad.  Finally, we’ll setup an LED that will turn green when successfully enter the passcode. If you want to expand on this you could replace the LED with a relay to make this project unlock a door. Let us know in the comments what you do with your setup!

### Parts List for this Project

If you’re just getting started or just want a simple list of parts, we’ve provided a simple shopping list for you. Some of these may be affiliate links. If you use them it costs you nothing. We may get a small commission that helps us keep making awesome content like this!

Before we go into the process of putting our project together, let’s quickly discover how the Keypads themselves work. As I mentioned previously, they are quite simplistic in design and operation.

On the underside of each key in a membrane keypad is a membrane switch (hence the name). In each row of the membrane is a horizontal trace that connects every membrane switch together. In every column is a vertical trace that also connects each switch together.  With four rows and four columns, this allows 16 keys to be read with only 8 pins.  When a key is pressed down it causes the trace on one row and one column to make electrical contact.

In the diagram below on the right, we’ve illustrated this concept with an electrical diagram. On the right, we’ve removed the backing from membrane switch so that you can see how the traces are physically laid out.  Other matrix keypads such as 3×4, 4×6, and 8×8 are all designed in this exact same manner. They will simply have more or less pins.

To determine which button is pressed the Arduino detects a connection at a certain row and column within the matrix. Let’s walk through the four operations that make this possible.

Step 1 – At idle, when none of the membrane keypad’s switches are being pressed, all of the rows are held low and all of the columns are held high.

Step 2 – When a button is pressed, the entire button’s row is pulled low, as electric contact is now being made.

Step 3 – Since the Arduino now knows which column the button is in, it needs to figure out which row the button is in. This process simply requires the Arduino to switch all of the rows from high to low and then read each columns. Which one returns high identifies which button was pressed.

Step 4 – Finally, the Arduino has its answer as both the row and column of the pressed button will be high.

## Connecting a Keypad to the Arduino

Most membrane keypads will use the same pinout.  The pinout will be R1, R2, R3, R4, C1, C2, C3, C4 where R is for row and C is for column. Refer to the image below. If your keypad does not have this pinout you can use a multi-meter in continuity mode to test each pin while pressing each button.

As we move forward in our How to Setup a Keypad on an Arduino tutorial, use the following diagram and chart to connect the keypad to your Arduino:

Keypad R1 –> Arduino PIN 9
Keypad R2 –> Arduino PIN 8
Keypad R3 –> Arduino PIN 7
Keypad R4 –> Arduino PIN 6
Keypad C1 –> Arduino PIN 5
Keypad C2 –> Arduino PIN 4
Keypad C3 –> Arduino PIN 3
Keypad C4 –> Arduino PIN 2

## Coding for an Arduino Keypad

Now that we have the hardware ready to go, it is time to move on to the software side of things. Let’s put together the basic code for the keypad.

Use the following code snippet to get the Keypad working.  Upload it to the Arduino Uno and then open the serial monitor.

```[code language = "cpp"]
// The Geek Pub Keypad to Serial Console

{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

byte rowPins[KEYPAD_ROWS] = {9, 8, 7, 6};
byte colPins[KEYPAD_COLS] = {5, 4, 3, 2};

void setup() {
Serial.begin(9600);
}

void loop() {

if (GPKey) {
Serial.println(GPKey);
}
}
[ / code]
```

If you did everything correctly, as you press keys the serial monitor should show the output.

## Controlling an LED with a Password

Now let’s add a little more code. This code will look for our password and if we get it correct, it will turn on a green LED for 5 seconds.  Otherwise, a red LED will stay lit indicating our keypad is locked.

```GPKey = GPKeypad.getKey();

if (GPKey) {
PW[myCounter] = GPKey;
Serial.print(PW[myCounter]);
myCounter++;
}

if (myCounter == Password_Length - 1) {

if (!strcmp(PW, MasterPW)) {
Serial.println("\nCorrect");
digitalWrite(GreenLEDPin, HIGH);
digitalWrite(RedLEDPin, LOW);
delay(5000);
digitalWrite(GreenLEDPin, LOW);
digitalWrite(RedLEDPin, HIGH);
}
else {
Serial.println("\nIncorrect");
delay(1000);
}
clearPW();
}
}
void clearPW() {
while (myCounter != 0) {
PW[myCounter--] = 0;
}
return;
}
[ / code]
```