How to Use Keypad with Arduino: Beginner’s Guide
What is Keypad?
A keypad is a user-input device with buttons arranged in a grid pattern. It’s commonly found in telephones, calculators, ATMs, and entry systems. A keypad is an ideal tool for user input in electronics projects, especially when numbers or commands need to be entered directly into a system.
Keypads come in various configurations, with the most common types being the 4×3 (12 buttons) and 4×4 (16 buttons). A 4×3 keypad consists of four rows and three columns, typically arranged like a phone keypad. A 4×4 keypad has four rows and four columns, adding additional buttons that are useful in projects requiring more inputs.
How the Keypad Works
Matrix Keypad Concept
A matrix keypad is arranged in a grid pattern, with each button connected at the intersection of a row and a column. Instead of connecting each button directly to the Arduino (which would require many pins), a matrix layout allows the Arduino to scan the rows and columns to detect which button was pressed. Pressing a button bridges a row and a column, creating a connection that the Arduino can detect by scanning the rows and columns in sequence.
A matrix keypad is more efficient for interfacing with a microcontroller like Arduino because it reduces the number of pins needed.
Pinout
A typical 4×4 keypad has 8 pins:
- 4 4-row pins: These correspond to the rows in the grid.
- 4 4-column pins: These correspond to the columns in the grid.
When wiring the keypad to the Arduino, these pins are connected to the digital I/O pins on the Arduino, allowing the microcontroller to detect which button is pressed by checking the intersections of rows and columns.
How to Connect the Keypad to Arduino
Wiring Diagram
Here’s how you can wire a 4×4 keypad to your Arduino:
- Connect the 4-row pins from the keypad to digital pins 2 through 5 on the Arduino.
- Connect the 4-column pins from the keypad to digital pins 6 through 9.
You can use the Keypad.h library, which simplifies interfacing the keypad with Arduino. This library provides predefined functions for reading keypresses and scanning the matrix layout.:
- Row Pins: R1 to pin 2, R2 to pin 3, R3 to pin 4, R4 to pin 5.
- Column Pins: C1 to pin 6, C2 to pin 7, C3 to pin 8, C4 to pin 9.
Arduino Code
You can use the Keypad.h library to read the button presses. Below is a sample code to read and display keypresses in the Arduino Serial Monitor.
#include <Keypad.h>
const byte ROWS = 4; // four rows
const byte COLS = 4; // four columns
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 of the keypad
byte colPins[COLS] = {6, 7, 8, 9}; // 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); // Print the pressed key to the serial monitor
}
}
Explanation of Code:
This code is for using a 4×4 keypad with an Arduino. It reads which key is pressed and displays it on the serial monitor. Here’s a breakdown of how it works:
- Libraries and Definitions:
- The Keypad library is included to handle keypad functions.
- ROWS and COLS define the keypad size (4 rows by 4 columns).
- Key Mapping:
- keys is a 2D array that matches each button on the keypad with a character. For example, the top left button is ‘1’, and the bottom right button is ‘D’.
- Pin Connections:
- rowPins and colPins define which Arduino pins connect to each row and column on the keypad.
- Keypad Setup:
- Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); set up the keypad using the layout and pin assignments.
- Serial Communication:
- Serial.begin(9600); initializes communication with the serial monitor at a speed of 9600 baud.
- Main Loop:
- char key = keypad.getKey(); checks if any key is pressed.
- If a key is pressed (if (key)), the key’s character is sent to the serial monitor with Serial.println(key);.
In short, when you press a key on the keypad, the code detects it and prints the corresponding character to the serial monitor.
Conclusion
Once you master the basics of using keypads, you can expand to more complex projects, such as access control systems that require a password to unlock a door or menu-driven interfaces where the keypad allows users to select different modes or settings. With a keypad and Arduino, the possibilities for interactive projects are limitless!