Connecting a keypad to an Arduino microcontroller is highly useful for various projects that require user input. A keypad provides a simple and effective way for users to interact with electronic systems by entering data, commands, or controlling functions.
Objective
The aim of this experiment is to help the user understand the operation of a keypad and how to interface it with a microcontroller. This experiment will allow the user to:
- Read user inputs from the keypad.
- Process commands based on the keypad inputs.
- Display the entered data or control devices via the microcontroller.
Component knowledge
What is a Keypad? A keypad is an electronic input device with a grid of buttons (usually arranged in rows and columns) that lets users enter data or commands. When you connect it to a microcontroller, you can use it to enter PIN codes, control devices, or interact with a menu system. The most common type of keypad used in electronics is a 4×4 or 4×3 matrix keypad.
Figure: 3X4 and 4X4 Keypad
- Pin Identification of a Keypad
A matrix keypad typically has 7 to 8 pins depending on whether it’s a 4×3 or 4×4 keypad.
- 4×3 Keypad: This has 4 rows and 3 columns, resulting in 7 pins.
- 4×4 Keypad: This has 4 rows and 4 columns, resulting in 8 pins.
This shows how you arrange the pins:
- Rows (R1, R2, R3, R4): These pins correspond to the horizontal rows of the keypad.
- Columns (C1, C2, C3, [C4]): These pins correspond to the vertical columns of the keypad.
Pressing a button connects a specific row and column, letting the microcontroller detect which key you pressed.
- Pin Mapping
For a 4×4 keypad, you’ll have the following pinout:
- Pin 1: Row 1 (R1)
- Pin 2: Row 2 (R2)
- Pin 3: Row 3 (R3)
- Pin 4: Row 4 (R4)
- Pin 5: Column 1 (C1)
- Pin 6: Column 2 (C2)
- Pin 7: Column 3 (C3)
- Pin 8: Column 4 (C4)
For a 4×3 keypad, you would omit Pin 8 (C4).
- Proper Way to Test a Keypad
- Check Continuity with a Multimeter:
- Set the multimeter to continuity mode.
- Identify row and column pins by referring to the keypad’s pinout.
- Press a key on the keypad.
- Place the multimeter probes on the corresponding row and column pins. When you press the button, the multimeter beeps to indicate continuity between the row and column pins if the key functions properly.
- Testing with a Microcontroller:
- Connect the keypad to a microcontroller (e.g., an Arduino). Connect the row and column pins to the digital input/output pins of the microcontroller.
- Use a keypad library for Arduino.
- Upload a simple sketch to the microcontroller that reads the keypad input. When you press a key, the microcontroller detects the specific key by identifying the connected row and column.
- Manual Testing:
Press each button and check whether the serial monitor detects the corresponding signal (when using Arduino) or the multimeter shows continuity for each press. This will help confirm that the keypad is working and can properly interface with the microcontroller.
Materials to use
Circuit diagram
Use pin 9-2 on control board to connect 4×4 keypad
Figure: Schematic diagram
Figure: Components used to make connection on the board
Arduino Code
Code knowledge
Include Keypad Library
This line imports the Keypad library, which simplifies the interaction between the Arduino and the keypad. This library provides functions to read key presses and map them to specific values.
Here, we define the number of rows and columns for the 4×4 keypad. This determines the grid layout of the keypad.
This section creates a 2D array that maps the physical keypad buttons to specific characters (‘1’, ‘2’, ‘3’, etc.).
Each row and column represents the buttons on the keypad and their corresponding character.
These arrays specify which Arduino pins connect to the keypad’s rows and columns.
rowPins[] holds the pin numbers connected to the rows, and colPins[] holds the pin numbers connected to the columns.
This line initializes a Keypad object named keypad using the makeKeymap() function, which converts the keys[][] array into a usable format for the Keypad library.
The parameters define the key mapping and the pin arrays that connect the keypad to the Arduino.
In the setup() function, the Serial.begin(9600) line initializes the serial communication at a baud rate of 9600. This will allow us to print the keypad input to the Serial Monitor.
Here in the loop() function continuously checks for key presses using keypad.getKey(). If you press a key, the program stores the character in the variable key. The condition if (key != NO_KEY) ensures the program prints the key value only when an actual key is pressed, preventing unnecessary outputs.
| Key Concepts: | |
| keypad.getKey(): This function checks if any key on the keypad is pressed. If a key is pressed, it returns the corresponding character, otherwise, it returns NO_KEY.Serial.println(key): This prints the detected key press to the Serial Monitor. | |
Results
We expect the output to give the following result.
Serial Monitor:
- The program prints the detected key to the serial monitor whenever you press a key. It doesn’t display any key until you press a button.
- Pressing a key displays its corresponding character on the serial monitor. For example:
- If you press the 1 key, the serial monitor will show: 1
- If you press the A key, the serial monitor will show: A
Example Serial Monitor Output: When you press keys in the following order: 1, 2, A, *, D, the serial monitor will display:
1
2
A
*
D