
E029 How to use 4x4 Soft Keypad with Arduino
π’ Interfacing a 4x4 Matrix Keypad with Arduino
This tutorial demonstrates how to connect and use a 4x4 alphanumeric matrix keypad with an Arduino. This type of keypad includes 16 keys (0-9, A-D, *, #) and only uses 8 pins (4 rows + 4 columns), making it ideal for compact interfaces. With the help of the Keypad
library, you can easily detect which button is pressed and trigger specific actions accordingly.
The keypad is connected to Arduino pins 2 through 9, and each press is detected in real-time and printed to the Serial Monitor. This can be adapted to control devices, input passwords, or navigate menus in embedded systems.
π Wiring Explained
The 4x4 keypad consists of 4 row lines and 4 column lines:
Keypad Pin | Arduino Pin |
---|---|
Row 1 | Pin 2 |
Row 2 | Pin 3 |
Row 3 | Pin 4 |
Row 4 | Pin 5 |
Col 1 | Pin 6 |
Col 2 | Pin 7 |
Col 3 | Pin 8 |
Col 4 | Pin 9 |
This wiring ensures that each button press can be uniquely identified by scanning the row and column matrix.
π» Code Explanation
Here's the code used in the video:
cppCopyEdit#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {2, 3, 4, 5}; // Connect to the row pinouts
byte colPins[COLS] = {6, 7, 8, 9}; // Connect to the column pinouts
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);
}
if (key == '4') {
Serial.println("Key 4 is pressed");
}
}
Code Breakdown:
keys[][]
: Defines the layout of the keypad.rowPins[]
andcolPins[]
: Maps the physical keypad pins to the Arduino.keypad.getKey()
: Checks if a key is pressed. If so, it's printed to the serial monitor.The program also checks if
'4'
was pressed and performs an additional action (prints"Key 4 is pressed"
), which can be extended to other keys as needed.
π¬ Video Chapters
00:00 β Start
00:45 β Introduction
01:32 β Wiring Explained
02:00 β Code Explained and Demonstration
This setup allows for flexible use of each keyβfor example, initiating actions, navigating menus, or controlling relays and lights.
π¦ Downloads
The Arduino code and required library files are available as a ZIP archive below this article. Let me know if
Related Tutorials in This Series:
Keypad
Related Links
Downloads
-
Keypad Arduino Library 0.02 MB this is the library used with keypads.Download
Comments will be displayed here.