4×4 Keypad Details with Arduino
Keypads are a great way to let users interact with your project. You can use them to navigate menus, enter passwords, and control games and robots.
n this tutorial, I’ll show you how to setup a keypad on the Arduino. First I’ll explain how the Arduino detects key presses, then I’ll show you how to find the pinout of any keypad. As a simple example, I’ll show you how to print out the key presses on the serial monitor and an LCD. Finally, I’ll show you how to activate a 5V relay when a password is entered correctly.
I’ll be using a 4X4 matrix membrane keypad in this article, but there’s also code and wiring diagrams for 3X4 matrix keypads as well. I like membrane style keypads because they’re thin and they also have adhesive backing so you can stick them to most flat surfaces. You can also get telephone style keypads that have thicker buttons if you like that style better. Even salvaged keypads from old telephones will work with the Arduino.
HOW KEYPADS WORK
The buttons on a keypad are arranged in rows and columns.
1. 3X4 keypad has 4 rows and 3 columns.
2. 4X4 keypad has 4 rows and 4 columns.
Beneath each key is a membrane switch. Each switch in a row is connected to the other switches in the row by a conductive trace underneath the pad. Each switch in a column is connected the same way – one side of the switch is connected to all of the other switches in that column by a conductive trace. Each row and column is brought out to a single pin, for a total of 8 pins on a 4X4 keypad:
Pressing a button closes the switch between a column and a row trace, allowing current to flow between a column pin and a row pin.
The schematic for a 4X4 keypad shows how the rows and columns are connected:
The Arduino detects which button is pressed by detecting the row and column pin that’s connected to the button.
This happens in four steps:
1. First, when no buttons are pressed, all of the column pins are held HIGH, and all of the row pins are held LOW:
2. When a button is pressed, the column pin is pulled LOW since the current from the HIGH column flows to the LOW row pin.
3. The Arduino now knows which column the button is in, so now it just needs to find the row the button is in. It does this by switching each one of the row pins HIGH, and at the same time reading all of the column pins to detect which column pin returns to HIGH.
4. When the column pin goes HIGH again, the Arduino has found the row pin that is connected to the button:
From the diagram above, you can see that the combination of row 2 and column 2 could only mean that the number 5 button was pressed.
Applications:
1. Security systems.
2. Vending machines.
3. Industrial machines.
4. Engineering systems.
5. Measuring instruments.
6. Data entry for Embedded Systems.
7. Hobby projects.
8. Basically any where INPUT device is needed.
CONNECT THE KEYPAD TO THE ARDUINO USE A PASSWORD TO ACTIVATE A RELAY
One of the most useful applications of a keypad is to use it for keyed entry. You can set up a password and have the Arduino activate a relay or some other module if the password is correct. The following code will activate a 5V relay when the password is entered correctly, and download I2C LCD Library.
ARDUINO CODE FOR 4X4 KEYPAD
#include <Wire.h> #include <LiquidCrystal_I2C.h> #include <Keypad.h> #define Password_Length 8 int signalPin = 12; char Data[Password_Length]; char Master[Password_Length] = "123A456"; byte data_count = 0, master_count = 0; bool Pass_is_good; char customKey; const byte ROWS = 4; const byte COLS = 4; char hexaKeys[ROWS][COLS] = { {'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'} }; byte rowPins[ROWS] = {9, 8, 7, 6}; byte colPins[COLS] = {5, 4, 3, 2}; Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); LiquidCrystal_I2C lcd(0x21, 16, 2); void setup(){ lcd.init(); lcd.backlight(); pinMode(signalPin, OUTPUT); } void loop() { lcd.setCursor(0,0); lcd.print("Enter Password:"); customKey = customKeypad.getKey(); if (customKey){ Data[data_count] = customKey; lcd.setCursor(data_count,1); lcd.print(Data[data_count]); data_count++; } if(data_count == Password_Length-1) { lcd.clear(); if(!strcmp(Data, Master)){ lcd.print("Correct"); digitalWrite(signalPin, HIGH); delay(5000); digitalWrite(signalPin, LOW); } else{ lcd.print("Incorrect"); delay(1000); } lcd.clear(); clearData(); } } void clearData() { while(data_count !=0) { Data[data_count--] = 0; } return; }