Keypad
Sumit Narayan Saurov
February 28, 2026
LIST OF FIGURES
7.1 4×4 Keypad Pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
7.2 Keypad Connection with Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . 3
i
LIST OF TABLES
7.1 Keypad Connection with Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . 3
7.2 Keypad Characters with ASCII Values . . . . . . . . . . . . . . . . . . . . . . . 4
7.3 Keypad Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
ii
CODE LISTINGS
7.1 Basic Keypad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
7.2 Numeric Keypad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
7.3 Alphabetic Keypad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
7.4 Simple Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
7.5 Fixed Length Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
7.6 Variable Length Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
iii
Chapter
7 Keypad
A membrane keypad is a low-profile human–machine interface device widely used in embedded
systems and electronic instruments for user input. It consists of multiple flexible layers - typ-
ically a top graphic overlay, spacer, and conductive circuit layer - laminated together to form
a compact, sealed structure. When a key is pressed, the top layer flexes and makes contact
with the conductive traces below, completing an electrical circuit that can be detected by a
microcontroller. Due to their thin form factor, low cost, light weight, and resistance to dust and
moisture, membrane keypads are commonly found in applications such as calculators, microwave
ovens, access control panels, and educational development boards. The sealed construction also
improves durability in harsh or frequently used environments compared to mechanical switches.
4×4 Membrane Keypad
A 4×4 membrane keypad is one of the most widely used configurations, offering 16 keys arranged
in four rows and four columns. The keys are internally connected in a matrix format, which
requires only 8 microcontroller I/O pins (4 row lines and 4 column lines) instead of 16 individual
connections (Figure 7.1), making the interface highly efficient and hardware-friendly. When a key
is pressed, it electrically connects a specific row and column, and the microcontroller identifies the
key using a row–column scanning technique by sequentially driving rows and monitoring column
states. To ensure stable logic levels during scanning, internal or external pull-up resistors are
commonly used. These keypads typically operate at standard logic voltages such as 5V, ensuring
compatibility with popular platforms like Arduino microcontrollers. The keys are usually labeled
with numeric digits (0–9), arithmetic symbols, and function keys such as A–D or *, #, making the
4×4 membrane keypad well suited for applications like password-based access control systems,
menu navigation, digital calculators, and other simple embedded user interfaces.
1
CHAPTER 7. KEYPAD 2
Internal Structure
Inside a 4×4 membrane keypad, the switches are
arranged in a matrix of 4 rows and 4 columns, form-
ing 16 switch points at their intersections. Each
key is located at the crossing of one specific row
line and one specific column line. Internally, all
keys in the same row share a common horizontal
conductor, and all keys in the same column share a
common vertical conductor. When a key is pressed,
it electrically connects its corresponding row and
column, creating a unique row–column pair. The
controller identifies the pressed key by driving rows
or columns one at a time and reading the others, a
method known as matrix scanning. This structure
allows 16 keys to be read using only 8 signal lines Figure 7.1: 4×4 Keypad Pins
instead of 16 individual connections.
Keypad Scanning Procedure
In row–column scanning, the microcontroller does not check all keys at once; instead, it activates
one row at a time and checks which column becomes connected. First, all column pins are
configured as inputs, usually with pull-up resistors enabled, and all row pins are configured as
outputs, with all rows initially kept at logic HIGH. The controller then sets the first row to
logic LOW while keeping the other rows HIGH; if any key in that row is pressed, it electrically
connects that LOW row to its corresponding column, causing that column input to read LOW.
By checking which column went LOW, the controller knows exactly which key in that active
row was pressed, and the combination of the active row number and detected column number
uniquely identifies the key. After reading the columns, the controller sets the first row back to
HIGH and pulls the second row LOW, repeating the same checking process for all rows one by
one. This scanning is done continuously in a loop and very quickly, typically many times per
second, so the user experiences it as instant detection; if no column changes during a particular
row scan, it means no key in that row is pressed. Only one row is activated at a time to
avoid confusion between multiple keys, and software usually adds a small debouncing delay of a
few milliseconds to prevent false multiple detections caused by mechanical vibration of the key
contacts.
Keypad Connection with Arduino
When connecting a keypad to Arduino, digital pins 0 and 1 are kept unused because they are
reserved for serial communication (RX and TX) with the computer, so that serial communication
through the USB port works properly for uploading programs and displaying keypad output on
the Serial Monitor. Typically, the four row wires of the keypad are connected to digital pins 9,
8, 7, and 6, while the four column wires are connected to digital pins 5, 4, 3, and 2 (Table 7.1
& Figure 7.2), though any available digital pins can be used.
Arduino Sumit Narayan Saurov EEE
CHAPTER 7. KEYPAD 3
Table 7.1: Keypad Connection with Arduino
Rows Arduino Columns Arduino
R1 9 C1 5
R2 8 C2 4
R3 7 C3 3
R4 6 C4 2
Figure 7.2: Keypad Connection with Arduino
Arduino Sumit Narayan Saurov EEE
CHAPTER 7. KEYPAD 4
ASCII Encoding
American Standard Code for Information Interchange (ASCII) is a character encoding system
used to represent text in computers and digital devices by assigning a unique numeric value to
letters, digits, punctuation marks, and control characters. Since computers operate using binary
(0s and 1s) and cannot directly understand letters or symbols, numbers are used to represent
characters so that textual information can be stored, processed, and transmitted electronically.
Standard ASCII uses 7 bits to represent 128 different characters. By providing a standardized
numeric code for each character, ASCII enabled different machines, computers and communica-
tion systems to communicate consistently.
Table 7.2: Keypad Characters with ASCII Values
Char ASCII Char ASCII Char ASCII Char ASCII
# 35 2 50 6 54 A 65
* 42 3 51 7 55 B 66
0 48 4 52 8 56 C 67
1 49 5 53 9 57 D 68
NULL Character
The null character (\0) is a special character which represents an empty value or the absence of
data. It has an ASCII value of 0 and does not produce any visible output when printed. The null
character indicates that no valid input or character is present, such as when a function returns
no data. In the Keypad library, it is represented by NO_KEY, which means no key is pressed.
Methods
In the Keypad library, there are two main methods available to detect key presses. The getKey()
method is non-blocking and simply checks if a key has been pressed, returning the key value or
NO_KEY (\0) if nothing is pressed. On the other hand, waitForKey() is blocking and pauses the
program until a key is pressed, ensuring that input is received before the program continues.
Table 7.3: Keypad Methods
Method Type Function
getKey() Non-blocking Returns pressed key or NULL if no key is pressed
waitForKey() Blocking Waits untill a key is pressed and returns the key
Arduino Sumit Narayan Saurov EEE
CHAPTER 7. KEYPAD 5
Basic Operation
This program demonstrates the basic operation of a matrix keypad interfaced with an Arduino.
The code continuously scans the keypad to detect key presses and displays the corresponding
character on the serial monitor.
Code 7.1 Basic Keypad /
/*
This is the most simple and introductory code for a 4*4 Keypad.
In this code we will print the pressed key in the serial monitor.
*/
// Custom Library Inclusion
#include <Keypad.h>
// Specifying no. of rows & columns of keypad
const int ROW_NUM = 4; // four rows
const int COLUMN_NUM = 4; // four columns
// Character Map
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Keypad connection with Arduino Pin
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; // Rows (R1,R2,R3,R4) connection to Arduino
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; // Columns (C1,C2,C3,C4) connection to Arduino
// Declaring a object under Keypad Class library
Keypad mykeypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
void setup() {
[Link](9600); // Serial monitor to print characters
}
void loop() {
char key = [Link](); // getkey() method to get the key
// Checking whether any key has been pressed to avoid Printing Null Characters
// ----- key ** key == true ** key != false ** key != 0 ** key != '\0' ----- //
if (key != NO_KEY) {
[Link](key);
}
}
Arduino Sumit Narayan Saurov EEE
CHAPTER 7. KEYPAD 6
Numeric Keypad
This program demonstrates a modified keypad interface where only numeric inputs (0–9) are
accepted and displayed, while all other keys such as A, B, C, D, * and # are ignored. This
selective input approach is useful in applications where only numerical data is required, such as
password entry or number-based user input. This implementation shows how input validation
can be performed in a Arduino keypad system to restrict unwanted key presses. The program
continuously checks the pressed key and verifies whether it falls within the acceptable numeric
range before displaying or storing it. This method enhances system security, reduces input errors,
and simplifies further data processing by ensuring that the collected input follows a predefined
format. Such controlled input handling is commonly used in embedded systems that require
accurate and restricted data entry, and it demonstrates an important technique for designing
robust and user-friendly keypad interfaces.
Code 7.2 Numeric Keypad /
/*
Here we would create a simple Number Pad. That means it would only take input when a
user presses any number [0 - 9]. If user presses any letter ('A',B','C','D') or any
special character ('*','#') it would simply ignore those key inputs.
*/
#include <Keypad.h>
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //four columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad mykeypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
void setup() {
[Link](9600);
}
void loop() {
char key = [Link]();
// Checking whether any number key has been pressed (Excluding letters)
if (key >= '0' && key <= '9') {
[Link](key);
}
}
Arduino Sumit Narayan Saurov EEE
CHAPTER 7. KEYPAD 7
Alphabetic Keypad
This program also demonstrates a modified keypad interface where alphabetic inputs (A–D)
and special characters such as * and # are accepted and processed, while only numeric keys
(0–9) are ignored. This approach is useful in applications where only letter-based input is
required, such as mode selection or command entry. This implementation also illustrates how
input filtering can be applied in a Arduino keypad system to ensure that only relevant key
presses are recognized. The program continuously monitors key presses and checks whether the
detected input belongs to the allowed set of characters before processing or displaying it. This
controlled input handling improves system efficiency, reduces errors, and simplifies program logic
by eliminating unnecessary data validation at later stages. Such techniques are widely used in
embedded systems that require structured command input, demonstrating an important method
for designing reliable and application-specific keypad interfaces.
Code 7.3 Alphabetic Keypad /
/*
Here we would create a simple Number Pad. That means it would only take input when a
user presses any letter ('A',B','C','D') or any special character ('*','#'). If user
presses any number ['0' - '9'] it would simply ignore those key inputs.
*/
#include <Keypad.h>
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //four columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
void setup() {
[Link](9600);
}
void loop() {
char key = [Link]();
// Checking whether any letter key has been pressed (Excluding Numbers)
if (key != NO_KEY && (key < '0' || key > '9')) {
[Link](key);
}
}
Arduino Sumit Narayan Saurov EEE
CHAPTER 7. KEYPAD 8
Simple Calculator Design
This program implements a simple keypad-based calculator that performs arithmetic operations
on two single-digit numbers, including addition (+), subtraction (–), multiplication (*), division
(/), power (∧), and remainder (%). The keypad mapping is modified so that specific keys
represent these operations: pressing ‘A’ produces ‘+’, pressing ‘B’ produces ‘–’, pressing ‘C’
produces ‘*’, pressing ‘D’ produces ‘/’, pressing ‘*’ produces ‘∧’ and pressing ‘#’ produces ‘%’.
This demonstrates how the keypad layout can be customized to perform specific functions, along
with basic input processing and arithmetic computation using a Arduino keypad interface.
A ⇒ Prints ‘+’ and performs the addition operation.
B ⇒ Prints ‘−’ and performs the subtraction operation.
C ⇒ Prints ‘*’ and performs the multiplication operation.
D ⇒ Prints ‘/’ and performs the division operation.
* ⇒ Prints ‘∧’ and performs the power operation.
# ⇒ Prints ‘%’ and performs the remainder operation.
Code 7.4 Simple Calculator /
/*
This code shows how to construct a simple calculator which takes two single-digit
input from keypad and performs various mathematical operation between them.
*/
#include <Keypad.h>
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //four columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', '+'}, // A (Addition)
{'4', '5', '6', '-'}, // B (Subtraction)
{'7', '8', '9', '*'}, // C (Multiply)
{'^', '0', '%', '/'} // * (Power --> ^) + # (Remainder --> %) + D (Division --> /)
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad myKeypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
void setup() {
[Link](9600);
Arduino Sumit Narayan Saurov EEE
CHAPTER 7. KEYPAD 9
void loop() {
// Taking First Input
[Link]("First Number: ");
int n1 = [Link]() - '0'; // ASCII code of '0' is 48 (0x30)
[Link](n1);
// Operation Input
[Link]("Operation: ");
char op = [Link](); // Mathematical Operator
[Link](op);
// Taking Second Input
[Link]("Second Number: ");
int n2 = [Link]() - '0';
[Link](n2);
// Calculation Result
int result = calculator(n1, op, n2); // Calling Custom Function
[Link]("Result = ");
[Link](result);
[Link]("-----------------------"); // Creating a space between calculations
// Custom Function for Mathematical Calculation
int calculator (int n1, char op, int n2)
{
int result = 0; // Variable for storing calculation result
if (op == '+')
result = n1 + n2;
else if (op == '-')
result = n1 - n2;
else if (op == '*')
result = n1 * n2;
else if (op == '/')
result = n1 / n2;
else if (op == '%')
result = n1 % n2;
else if (op == '^')
result = round(pow(n1, n2));
else
[Link]("Invalid Operation ");
return result;
}
Arduino Sumit Narayan Saurov EEE
CHAPTER 7. KEYPAD 10
Multiple Digit Input
When interfacing a 4×4 keypad with an Arduino Uno, multiple digit input enables the system
to receive a sequence of key presses rather than a single character. Instead of processing each
key independently, the microcontroller collects and stores successive inputs to form a complete
numeric value or command. This feature is essential in embedded applications such as password
authentication, electronic locks, calculator inputs, security systems, and menu-driven interfaces,
where accurate and organized data entry is required for reliable system operation.
To handle such input efficiently, two main approaches are commonly used:
■ Fixed Length Digit Input
■ Variable Length Digit Input
In the fixed length approach, the number of digits to be entered is predetermined by the program,
and the system processes the input only after receiving the required number of keys. In contrast,
variable length input allows users to enter digits freely and uses a specific termination key
to indicate the end of the sequence. The choice between these methods depends on system
requirements, flexibility, and application design.
Fixed Length Digit Input
Fixed length digit input refers to a method where the system is programmed to accept a specific
number of key presses, such as a 3-digit or 4-digit code. The microcontroller reads each key
press sequentially and stores the input in a buffer or variable until the required number of digits
is reached. After receiving the complete sequence, the system performs further actions such
as verification, comparison with a stored value, or execution of a command. This approach is
simple to implement and ensures consistent input size, reducing the need for additional checks or
termination conditions. It is widely used in applications like door security systems, PIN-based
authentication, and access control mechanisms, where the input format is fixed and known in
advance.
The fixed-digit method which requires a predetermined number of key presses before an operation
is executed demonstrated in Code 7.5.
Variable Length Digit Input
Variable length digit input allows the user to enter an arbitrary number of digits without a
predefined limit. In this method, the system continuously monitors keypad presses and stores
each input until a designated terminating key, such as ‘#’, is pressed to signal the completion of
data entry. Once the termination key is detected, the stored sequence is processed by the pro-
gram. This technique provides greater flexibility compared to fixed length input, as it supports
different input sizes and user-defined data lengths. It is commonly used in applications such as
numeric data entry, menu selection systems, and command-based interfaces, where the amount
of input may vary depending on user requirements.
The variable digit method which uses a special terminating key to gather inputs of flexible
length, allowing numbers of any size demonstrated in Code 7.6.
Arduino Sumit Narayan Saurov EEE
CHAPTER 7. KEYPAD 11
Code 7.5 Fixed Length Number /
/*
This code shows how to take a fixed digit input from a keypad
and then multiplies the given input by 2 and show the result.
First, it prompts the user to specify the input length
and then takes that exact number of digits from user.
*/
#include <Keypad.h>
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //four columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad mykeypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
void setup() {
[Link](9600);
}
void loop() {
// Taking no. of digits as Input (Value must be Non-Zero)
[Link]("Number of Digits = ");
int n = [Link]() - '0'; // keypad value input
[Link](n);
// Taking Multi Digit Input
[Link]("Input Number: ");
int keyval = 0; // Keypad value input
int value = 0; // variable for storing input (accumulator)
for (int i = 1; i <= n; i ++)
{
keyval = [Link]() - '0';
[Link](keyval); // Printing Digits one by one
value = value * 10 + keyval; // Multi Digit Conversion
}
[Link](); // Printing new line to indicate end of input
[Link]("Result = ");
[Link](value * 2);
}
Arduino Sumit Narayan Saurov EEE
CHAPTER 7. KEYPAD 12
Code 7.6 Variable Length Number /
/*
This code shows how to take a Integer input from user using String Class object.
After taking the input it shows the result which is the user input multiplied by 2.
It checks input validity and user has to press "#" to denote the end of the input.
*/
#include <Keypad.h>
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //four columns
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad mykeypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
void setup() {
[Link](9600);
}
void loop() {
char keynum = 0; // Pressed key
int i = 0; // String position indicator
String str = ""; // String Input (Initialized as an Empty String)
int num = 0; // Converted Integer Input Number
[Link]("Input: ");
while (keynum != '#') // Denotes End of Input String
{
keynum = [Link](); // Fetching new keypress until '#' is pressed
if (keynum >= '0' && keynum <= '9') // Checking Valid key Input
{
str += keynum;
[Link](str[i]); // To print individual keystrokes
i++;
}
}
num = [Link](); // String to INT conversion
[Link](); // Printing New Line
[Link]("Answer: ");
[Link](num * 2); // User input multiplied by 2
}
Arduino Sumit Narayan Saurov EEE