Keypad Tutorial


let’s talk about keypads, and most importantly matrix keypads.  These are extremely easy to set up and you only need a pin for each row and each column.  So if you have a 4×4 keypad (shown below) you need 4+4 totaling 8 pins.  8 pins for 16 buttons?  Seems like a good deal to me!

For this one I’m going to show you how to set up the key pad and code so that when the end user has to put in a ‘password’ .  If the password is right: a green light comes one.  If the password is wrong: a red light blinks 4 times.

Now these LEDs can be pretty much anything.  You could use this as the basic code to open and close a door, turn on a light, or even turn off an alarm


1X Arduino Compatible Board

1X 4×4 Key Pad

1X Male – Male jumper cables


2X 220Ω Resistors




#include <Keypad.h>

#define greenLED 11
#define redLED 10

const byte ROWS = 4; 
const byte COLS = 4; 
char keys[ROWS][COLS] = {

String passWord ="1234"; //manually define password
String passInput;

byte rowPins[ROWS] = {2,3,4,5}; //connect to row pinouts 
byte colPins[COLS] = {6,7,8,9}; //connect to column pinouts

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){

void loop(){
  char key = keypad.getKey();//gets the character pressed

  if (key != NO_KEY){
    if (key=='#'){//checks to see if "enter" is pressed
      if(passInput==passWord){ //is the password entered the same as the one defined?
        Serial.println("congratulations you have unlocked the safe");//yes
        Serial.println("Sorry... Wrong Password!");

        for (int i=0;i<4;i++){ //blink light 4 times
       passInput="";//clear password string
    }else{//if "enter" is not pressed add the key char to the nd of the string


Leave a Reply

Your email address will not be published. Required fields are marked *