Arduino Keypad Tutorial
This webpage tutorial will guide you through connecting and using a keypad with your Arduino board. Keypads allow for user input, opening up a world of possibilities for your Arduino projects.
Keypad Types
There are two popular types of keypads for DIY projects:
- Keypad 3×4 (12 keys):
- It has 4 row pins (R1, R2, R3, R4) and 3 column pins (C1, C2, C3).
- Total pins: 7 (4 rows + 3 columns).
- Keypad 4×4 (16 keys):
- It has 4 row pins (R1, R2, R3, R4) and 4 column pins (C1, C2, C3, C4).
- Total pins: 8 (4 rows + 4 columns).
Components
Arduino Uno
Keypad 4x4
Cables
Connection diagram
Keypad library
The code!
#include <Keypad.h> 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); void setup(){ Serial.begin(9600); } void loop(){ char customKey = customKeypad.getKey(); if (customKey){ Serial.println(customKey); } }
Breakdown:
- Include Library: Add
#include <Keypad.h>
at the beginning of your Arduino sketch to utilize the Keypad library. - Define Keypad Layout: Create a character array to define the keypad layout, matching characters to their physical keys.
- Define Rows and Columns: Set variables for the number of rows and columns on your keypad.
- Initialize Keypad Object: Use the
Keypad
constructor to create a new keypad object, specifying the row pins, column pins, number of rows, and number of columns. - Read Key Presses: Inside your loop, use the
getKey()
function of the keypad object to detect and read any pressed keys.
Keypad with Relay Example
Arduino Uno
Breadboard
Keypad 4x4
Relay
Cables
Connection diagram
The code!
#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; }