In this project, we will go over how to integrate a keyboard with an UNO R3 board so that the UNO R3 can read the keys being pressed by a user.
Keypads are used in all types of devices, including cell phones, fax machines, microwaves, ovens, door locks, etc. They're practically everywhere. Tons of electronic devices use them for user input.
So knowing how to connect a keypad to a microcontroller such as an UNO R3 board is very valuable for building many different types of commercial products.
For this project, the type of keypad we will use is a matrix keypad. This is a keypad that follows an encoding scheme that allows it to have much less output pins than there are keys. For example, the matrix keypad we are using has 16 keys (0-9, A-D,*, #), yet only 8 output pins. With a linear keypad, there would have to be 17 output pins (one for each key and a ground pin) in order to work. The matrix encoding scheme allows for less output pins and thus much less connections that have to make for the keypad to work. In this way, they are more efficient than linear keypads, being that they have less wiring.
(1) x Elegoo Uno R3
(1) x Membrane switch module
(4) x M-M wires (Male to Male jumper wires)
When connecting the pins to the UNO R3 board, we connect them to the digital output pins, D9-D2. We connect the first pin of the keypad to D9, the second pin to D8, the third pin to D7, the fourth pin to D6, the fifth pin to D5, the sixth pin to D4, the seventh pin to D3, and the eighth pin to D2.
These are the connections in a table:
You can click the blue text link to download the program file to your local device, and double-click the file to open it after the download is complete. Please note: Before opening the file, ensure that you have installed the Arduino IDE development environment and completed the installation of relevant components such as the board support package and driver corresponding to the UNO board. If you have any questions about this operation process, you can refer to the "part 1" chapter of the document for detailed guidance.
With this code, once we press a key on the keypad, it should show up on the serial monitor of the Arduino software once the code is compiled and uploaded to the UNO R3 board.
Click the Serial Monitor button to turn on the serial monitor. The basics about the serial monitor are introduced in details in tutorial 4 in part 2.
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
Constant Declarations:const byte ROWS = 4 ;
Defines constants for the keypad’s dimensions (a 4x4 matrix keypad has 4 rows and 4 columns, totaling 8 pins).
Using const byte ensures these values are fixed (no accidental modification) and uses minimal memory.
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
hexaKeys[ROWS][COLS]: A 2D (two-dimensional) array that maps the physical buttons on the 4x4 keypad to their corresponding characters.
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
Keypad customKeypad: Creates a Keypad object named customKeypad (an instance of the Keypad class) to control the physical keypad.
void loop(){
char customKey = customKeypad.getKey();if (customKey){
Serial.println(customKey);
}
}
char customKey = customKeypad.getKey(): Checks if a keypad button is pressed. Returns the corresponding character (from hexaKeys) if a button is pressed; returns 0 (no value) if no button is pressed.