Using Numeric Keypads With Arduino

About: pmdway.com - everything for the maker and electronics enthusiast, with free delivery worldwide

Numeric keypads can provide a simple end-user alternative for various interfaces for your projects. Or if you need a lot of buttons, they can save you a lot of time with regards to construction. We’ll run through connecting them, using the Arduino library and then finish with a useful example sketch.

Step 1: Getting Started

No matter where you get your keypads from, make sure you can get the data sheet – as this will make life easier when wiring them up. For example - the image in this step.

The data sheet is important as it will tell you which pins or connectors on the keypad are for the rows and columns. If you don’t have the data sheet – you will need to manually determine which contacts are for the rows and columns.

This can be done using the continuity function of a multimeter (the buzzer). Start by placing one probe on pin 1, the other probe on pin 2, and press the keys one by one. Make a note of when a button completes the circuit, then move onto the next pin. Soon you will know which is which. For example, on the example keypad pins 1 and 5 are for button “1″, 2 and 5 for “4″, etc…

At this point please download and install the keypad Arduino library. Now we’ll demonstrate how to use both keypads in simple examples.

Step 2: Using a 12 Digit Keypad

We’ll use the small black keypad, an Arduino Uno-compatible and an LCD with an I2C interface for display purposes. If you don’t have an LCD you could always send the text to the serial monitor instead.

Wire up your LCD then connect the keypad to the Arduino in the following manner: Keypad row 1 to Arduino digital 5Keypad row 2 to Arduino digital 4Keypad row 3 to Arduino digital 3Keypad row 4 to Arduino digital 2Keypad column 1 to Arduino digital 8Keypad column 2 to Arduino digital 7Keypad column 3 to Arduino digital 6 If your keypad is different to ours, 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 enter the digital pin numbers connected to the rows and columns of the keypad respectively.

Furthermore, the array keys stores the values displayed in the LCD when a particular button is pressed. You can see we’ve matched it with the physical keypad used, however you can change it to whatever you need. But for now, enter and upload the following sketch once you’re satisfied with the row/pin number allocations:

/* Numeric keypad and I2C LCD
http://tronixstuff.com Uses Keypad library for Arduino http://www.arduino.cc/playground/Code/Keypad by Mark Stanley, Alexander Brevig */

#include "Keypad.h" #include "Wire.h" // for I2C LCD #include "LiquidCrystal_I2C.h" // for I2C bus LCD module // http://www.dfrobot.com/wiki/index.php/I2C/TWI_LCD1602_Module_(SKU:_DFR0063) LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display

// 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, 4, 3, 2}; //connect to the row pinouts of the keypad byte colPins[COLS] = { 8, 7, 6}; // connect to the column pinouts of the keypad

int count=0;

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

void setup() { lcd.init(); // initialize the lcd lcd.backlight(); // turn on LCD backlight }

void loop() { char key = keypad.getKey(); if (key != NO_KEY) { lcd.print(key); count++; if (count==17) { lcd.clear(); count=0; } } }

And the results of the sketch are shown in the video. So now you can see how the button presses can be translated into data for use in a sketch. We’ll now repeat this demonstration with the larger keypad.

Step 3: Using a 16 Digit Keypad

We’ll use the larger 4×4 keypad, an Arduino Uno-compatible and for a change the I2C LCD from Akafugu for display purposes. Again, if you don’t have an LCD you could always send the text to the serial monitor instead. Wire up the LCD and then connect the keypad to the Arduino in the following manner:

  • Keypad row 1 (pin eight) to Arduino digital 5
  • Keypad row 2 (pin 1) to Arduino digital 4
  • Keypad row 3 (pin 2) to Arduino digital 3
  • Keypad row 4 (pin 4) to Arduino digital 2
  • Keypad column 1 (pin 3) to Arduino digital 9
  • Keypad column 2 (pin 5) to Arduino digital 8
  • Keypad column 3 (pin 6) to Arduino digital 7
  • Keypad column 4 (pin 7) to Arduino digital 6

Now for the sketch – take note how we have accommodated for the larger numeric keypad: the extra column in the array char keys[]the extra pin in the array colPins[]and the byte COLS = 4.

/* Numeric keypad and I2C LCD
http://tronixstuff.com Uses Keypad library for Arduino http://www.arduino.cc/playground/Code/Keypad by Mark Stanley, Alexander Brevig */

#include "Keypad.h" #include "Wire.h" // for I2C LCD #include "TWILiquidCrystal.h" // http://store.akafugu.jp/products/26 LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const byte ROWS = 4; //four rows const byte COLS = 4; //four columns char keys[ROWS][COLS] = {{'1','2','3','A'}, {'4','5','6','B'}, {'7','8','9','C'}, {'*','0','#','D'}}; byte rowPins[ROWS] = { 5, 4, 3, 2}; //connect to the row pinouts of the keypad byte colPins[COLS] = { 9, 8, 7, 6}; //connect to the column pinouts of the keypad int count=0;

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

void setup() { Serial.begin(9600); lcd.begin(16, 2); lcd.print("Keypad test!"); delay(1000); lcd.clear(); }

void loop() { char key = keypad.getKey(); if (key != NO_KEY) { lcd.print(key); Serial.print(key); count++; if (count==17) { lcd.clear(); count=0; } } }

And again you can see the results of the sketch above in the video.

Now for an example project, one which is probably the most requested use of the numeric keypad…

Step 4: Example Project – PIN Access System

The most-requested use for a numeric keypad seems to be a “PIN” style application, where the Arduino is instructed to do something based on a correct number being entered into the keypad. The following sketch uses the hardware described for the previous sketch and implements a six-digit PIN entry system.

The actions to take place can be inserted in the functions correctPIN() and incorrectPIN(). And the PIN is set in the array char PIN[6]. With a little extra work you could create your own PIN-change function as well.

// PIN switch with 16-digit numeric keypad
// http://tronixstuff.com #include "Keypad.h" #include #include LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const byte ROWS = 4; //four rows const byte COLS = 4; //four columns char keys[ROWS][COLS] = { { '1','2','3','A' } , { '4','5','6','B' } , { '7','8','9','C' } , { '*','0','#','D' } }; byte rowPins[ROWS] = { 5, 4, 3, 2}; //connect to the row pinouts of the keypad byte colPins[COLS] = { 9, 8, 7, 6}; //connect to the column pinouts of the keypad

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

char PIN[6]={ '1','2','A','D','5','6'}; // our secret (!) number char attempt[6]={ '0','0','0','0','0','0'}; // used for comparison int z=0;

void setup() { Serial.begin(9600); lcd.begin(16, 2); lcd.print("PIN Lock "); delay(1000); lcd.clear(); lcd.print(" Enter PIN..."); }

void correctPIN() // do this if correct PIN entered { lcd.print("* Correct PIN *"); delay(1000); lcd.clear(); lcd.print(" Enter PIN..."); }

void incorrectPIN() // do this if incorrect PIN entered { lcd.print(" * Try again *"); delay(1000); lcd.clear(); lcd.print(" Enter PIN..."); }

void checkPIN() { int correct=0; int i; for ( i = 0; i < 6 ; i++ ) {

if (attempt[i]==PIN[i]) { correct++; } } if (correct==6) { correctPIN(); } else { incorrectPIN(); }

for (int zz=0; zz<6; zz++) { attempt[zz]='0'; } }

void readKeypad() { char key = keypad.getKey(); if (key != NO_KEY) { attempt[z]=key; z++; switch(key) { case '*': z=0; break; case '#': z=0; delay(100); // for extra debounce lcd.clear(); checkPIN(); break; } } }

void loop() { readKeypad(); }

The project is demonstrated in the video.

So now you have the ability to use twelve and sixteen-button keypads with your Arduino systems. I’m sure you will come up with something useful and interesting using the keypads in the near future.

This post is brought to you by pmdway.com – everything for makers and electronics enthusiasts, with free delivery worldwide.

Share

    Recommendations

    • Sew Tough Challenge

      Sew Tough Challenge
    • Stone Concrete and Cement Contest

      Stone Concrete and Cement Contest
    • Classroom Science Contest

      Classroom Science Contest

    Discussions