Advertisement
arduino with keypad

Arduino with Keypad Tutorial

A keypad is one of the most commonly used input devices in microprocessor applications. In a standard keypad wired as an X-Y switch matrix, normally-open switches connect a row to a column when pressed. If a keypad has 12 keys, it is wired as 3 columns by 4 rows. A 16 key pad would have 4 columns by 4 rows. Some time ago, I bought a couple of 3×4 membrane keypads from eBay. As usual it’s packed with zero documentation, thus it took couple of hours to get to work. Anyway the keypad is a perfect blend of art and technology with a price tag far below rubies!

keypad

This little article is designed to help you get started with your new microcontroller + keypad project so let’s start. Following figure shows the internal structure and pin notation of the 3×4 keypad used for the experiment. The drawing is the result of an obscene amount of research work because I really wanted to make an error-free guide for the project we are working on.

keypad: internal structure and pin notation

(keypad: internal structure and pin notation)

Here is the same information in a textual way; keypad columns C1-C2-C3 are routed to pins 3,1,5 and rows R1-R2-R3-R4 are routed to pins 2,7,6,4 located at the end of the 7-pin flexible cable. Much more clear now?

keypad: columns and rows switching

(keypad: columns and rows switching)

So now you can see how the button presses can be translated into electrical data for use with a microcontroller. We can now start the real experiment with as Arduino Uno by wiring up the keypad to the Arduino in the following/like manner:

  • Keypad pin 1 to Arduino digital 3 //C2
  • Keypad pin 2 to Arduino digital 5 //R1
  • Keypad pin 3 to Arduino digital 2 //C1
  • Keypad pin 4 to Arduino digital 8 //R4
  • Keypad pin 5 to Arduino digital 4 //C3
  • Keypad pin 6 to Arduino digital 7 //R3
  • Keypad pin 7 to Arduino digital 6 //R2

Since keypads are available from many retailers make sure you can get the data sheet as this will make the task easier when wiring them up. If your keypad is different, take note of the lines in the sketch from //keypad type definition, as you need to change the numbers in the arrays rowPins[ROWS] and colPins[COLS]. You should enter the digital pin numbers connected to the rows and columns of the keypad respectively. Example from Arduino playground:

#include "Keypad.h"
// keypad type definition
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {5, 6, 7, 8}; //connect to the row R1-R4 pinouts of the keypad
byte colPins[COLS] = {2, 3, 4}; //connect to the column C1-C3 pinouts of the keypad

Next is a project example, where the Arduino is instructed to do something based on a correct key (secret number) being entered into the keypad, probably the most demanded application of the keypad. For this, just wire up the hardware (arduino+keypad) as described earlier.

arduino keypad

Although the code is an entry-level one, it includes a secret key option that will need to be entered on the keypad. The Serial Monitor will tell whether the numerical-key inputted to the keypad is correct or not. To activate and deactivate the lock, the user must press * and then the secret key, followed by #. Okay, copy-paste, compile and upload this sketch (adaptation of a work by John Boxall) as usual:

#include "Keypad.h"
const byte ROWS = 4; // four rows
const byte COLS = 3; // three columns
char keys[ROWS][COLS] =
{
{'1','2','3' },
{'4','5','6' },
{'7','8','9' },
{'*','0','#' }
};
byte rowPins[ROWS] = {5, 6, 7, 8};
byte colPins[COLS] = {2, 3, 4};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char KEY[4] = {'1','2','3','4'}; // default secret key
char attempt[4] = {0,0,0,0};
int z=0;
void setup()
{
   Serial.begin(9600);
}
void correctKEY() // do this if the correct KEY is entered
{
   Serial.println(" KEY ACCEPTED...");
}
void incorrectKEY() // do this if an incorrect KEY is entered
{
   Serial.println("KEY REJECTED!");
}
void checkKEY()
{
   int correct=0;
   int i;
   for ( i = 0; i < 4 ; i++ )
   {
      if (attempt[i]==KEY[i])
      {
         correct++;
      }
   }
   if (correct==4)
   {
      correctKEY();
   }
   else
   {
      incorrectKEY();
   }
   for (int zz=0; zz<4; zz++) // clear previous key input
   {
      attempt[zz]=0;
   }
}
void readKeypad()
{
   char key = keypad.getKey();
   if (key != NO_KEY)
   {
      switch(key)
      {
      case '*':
         z=0;
         break;
      case '#':
         delay(100); // added debounce
        checkKEY();
         break;
      default:
         attempt[z]=key;
         z++;
      }
   }
}
void loop()
{
   readKeypad();
}

If your compiler raises an error “keypad does not name a type”, probably you have not installed the required Keypad Library correctly. If you still have the problem after downloading and installing the library, just drop me a line here in the comments.

Note that the good old 3×4 telephone keypad from your junk box can also be used for this project. Like most keypads, it is wired as an X-Y switch matrix, the normally-open switches connect a row (R) to a column (C) when pressed. Next figure shows the pin assignments of a generic 3×4 telephone keypad:

generic 3x4 telephone keypad

If you don’t know which pin represents which row or column, you have to fiddle the “rummy” keypad using your analog multimeter. It might take hours to get to work, be patient!

from author’s workbench

(from author’s workbench)

Note: At the middle of this experiment, I got another 3×4 keypad from an eBay seller with the “true” Arduino compatible pin-out (see figure shown below). If you are using this type, it would be better to change the arrays rowPins[ROWS] and colPins[COLS] in the sketch as indicated.

AKP-7

7 Comments

Join the conversation!

Error! Please fill all fields.
  • Tarvi

    Hi guys, I modified this script to fit 4×4 keybads. Usually it works just fine, but sometimes it seems to freeze/crash Arduino. It crashes in 2 ways:
    1. The keypad stops reacting
    2. The door magnet releases buzzer stays constantly on
    To stop this only Reset helps.

    My setup:
    Magnet is on PIN 12 through the relay module.
    Small buzzer is directly on PIN 11
    I have shortened the CAT5e cable between Arduino and keypad to ~2.5m

    Any idea what could cause this freeze of system?
    I am just wondering something in the script causing it? For example serial Print cannot handle too many quickly pressed buttons….

    The script:

    #include “Keypad.h”
    const byte ROWS = 4; // four rows
    const byte COLS = 4; // three columns
    char keys[ROWS][COLS] =
    {
    {‘1’, ‘2’, ‘3’, ‘A’},
    {‘4’, ‘5’, ‘6’, ‘B’},
    {‘7’, ‘8’, ‘9’, ‘C’},
    {‘*’, ‘0’, ‘#’, ‘D’}
    };
    byte rowPins[ROWS] = {9,8,7,6}; //Rows 0 to 3
    byte colPins[COLS] = {5,4,3,2}; //Columns 0 to 3
    Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
    char KEY[4] = {‘7′,’1′,’3′,’8’}; // default secret key
    char attempt[4] = {0,0,0,0};
    int z=0;

    int doorButton = 10;
    int doorBuzzer = 11;
    int doorMagnet = 12;

    void setup()
    {
    pinMode(doorButton, INPUT_PULLUP);
    pinMode(doorBuzzer, OUTPUT);
    pinMode(doorMagnet, OUTPUT);
    digitalWrite(doorMagnet, HIGH);
    digitalWrite(doorBuzzer, LOW);

    Serial.begin(9600);
    }
    void correctKEY() // do this if the correct KEY is entered
    {
    Serial.println(” KEY ACCEPTED…”);
    digitalWrite(doorMagnet, LOW);

    digitalWrite(doorBuzzer, HIGH); // Short beeps to signal door is opened
    delay(200);
    digitalWrite(doorBuzzer, LOW);
    delay(500);
    digitalWrite(doorBuzzer, HIGH);
    delay(200);
    digitalWrite(doorBuzzer, LOW);

    delay(5000);

    digitalWrite(doorMagnet, HIGH);
    }
    void incorrectKEY() // do this if an incorrect KEY is entered
    {
    Serial.println(“KEY REJECTED!”);
    digitalWrite(doorBuzzer, HIGH); // One long beep to signal door is opened
    delay(2000);
    digitalWrite(doorBuzzer, LOW);

    }
    void checkKEY()
    {
    int correct=0;
    int i;
    for ( i = 0; i < 4 ; i++ )
    {
    if (attempt[i]==KEY[i])
    {
    correct++;
    }
    }
    if (correct==4)
    {
    correctKEY();
    }
    else
    {
    incorrectKEY();
    }
    for (int zz=0; zz<4; zz++) // clear previous key input
    {
    attempt[zz]=0;
    }
    }
    void readKeypad()
    {
    char key = keypad.getKey();
    if (key != NO_KEY)
    {
    Serial.print(key);
    switch(key)
    {
    case '*':
    z=0;
    break;
    case '#':
    delay(100); // added debounce
    checkKEY();
    break;
    default:
    attempt[z]=key;
    z++;
    }
    }
    }
    void loop()
    {
    if (!digitalRead(doorButton)){ // To release the key from inside
    digitalWrite(doorMagnet, LOW);
    delay(3000);
    digitalWrite(doorMagnet, HIGH);
    }
    readKeypad();
    }

  • minakshi-c20gmail-com

    hey… i have 1 by 2 mtrix key pad having 2 buttons and 4 pins, without data sheet could u help me to identify the connection ..

  • eslam osama

    You could add a number of secret keys secret key for each particular job

  • George

    byte rowPins[ROWS] = {5, 6, 7, 8}; //connect to the row R1-R4 pinouts of the keypad

    shouldn’t that read

    byte rowPins[ROWS] = {2, 7, 6, 4}; //connect to the row R1-R4 pinouts of the keypad

    • tkhareendran

      This means you should connect keypad pins 2,7,6,4 to digital pins 5,6,7,8 of UNO in the same order for the keypad shown.

  • T.K.Hareendran

    Looking for the keypad library?

    Learn more about it & download from here: http://playground.arduino.cc/Code/Keypad

Looking for the latest from TI?