Introduction: First Time Using Keypad

About: Artificial Intelligence Developer

This instructable shows you how to get one of these 4 x 4 matrix keypad to work with your arduino. There are 8 pins on this. It works by 4 pins being connected to the rows and 4 pins to the columns. There are a few uses for this keypad: a door lock, controlling lights, password for a laptop etc.

In my free time I love to play with my arduinos, testing and building little toys and projects. I bought this keypad from an app called geek on the app store. It is really cheap for little things like this and they work quite well but for excellent quality products find a proper score.

Disclaimer: I'm not responsible if anyone injures themselves after building a project identical or similar to this instructable.

Arduino is a company that produces their own programmable boards and their own software IDE, the IDE is free and can be used by public or companies. However due to it being free to keep their development going please donate them money or buy their computer programmable boards.

Step 1: Collect the Items to Use

So simply collect these items to use for testing the keypad. The items are:

  1. Laptop with arduino IDE on it;
  2. Arduino, (any, I used nano);
  3. USB cable to (USB-B for arduino uno. USB micro for nano etc);
  4. Bread board if using arduino nano, ardunino uno already has the female pins on it;
  5. Male to male bread board / arduino wires;
  6. Keypad

The last image shows which digital I/O the pins are to connect to the arduino.

Step 2: Starting With Software

Firstly, if you don't have it download and install the arduino IDE. If this is the first time using the IDE you may have to install drivers depending on the board you are using. You can download the IDE from https://www.arduino.cc/en/Main/Software

One other thing to download for this project to work is the library. Libraries make every model easier to build. It means there is less coding to do. It works as a base to all projects you make. So the library you need is the keypad library. Once downloaded you need to put the file into your arduino libraries folder. You can download the library from http://playground.arduino.cc/Code/Keypad it's about half way down.

Inside of the examples in the library there is a hellopad.ino file. You can either copy the code I'll paste next or you can go on this and edit it by skipping to step 4.

Step 3: The Code

So this code is very simple since the library takes away all the complicated parts.

#include <Keypad.h> //import the library from the download

const byte ROWS = 4; //four rows

const byte COLS = 4; //three columns

char keys[ROWS][COLS] = { //the layout of the keypad

{'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 );

void setup(){

Serial.begin(9600);

}

void loop(){

char key = keypad.getKey();

if (key){

Serial.println(key);

}

}

Step 4: The Edit of the Hellopad.ino

This is the easiest way to write the code. From this you can also learn what parts change, why they change and the effect of the change.

Changes from

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

To

const byte ROWS = 4; //four rows

const byte COLS = 4; //three columns

char keys[ROWS][COLS] = { //the layout of the keypad

{'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

Step 5: Testing

The little video just shows me testing by pressing some of the buttons. It works very well.

To view results open serial monitor, located at the menu bar, tools > serial monitor.

Any errors?

Check:

  1. Board;
  2. Port;
  3. Wiring

If there are any questions don't hesitate to ask.