20. Chapter Matrix Keypad
We’ve already learned and used a push button switch before, now let us try to use a keypad, a device integrated with a number of Push Button Switches as Keys for the purposes of Input.
20.1. Project 20.1 Get Input Characters
First, try to understand how the keypad works and get the input characters.
20.1.1. Component List
Control board x1
|
USB cable x1
|
Keypad x1
|
Freenove Projects Board
|
||
20.1.2. Component Knowledge
20.1.2.1. 4x4 keypad
A Keypad Matrix is a device that integrates a number of keys in one package. As is shown below, a 4x4 Keypad Matrix integrates 16 keys (think of this as 16 Push Button Switches in one module):
Similar to the integration of an LED Matrix, the 4x4 Keypad Matrix has each row of keys connected with one pin and this is the same for the columns. Such efficient connections reduce the number of processor ports required. The internal circuit of the Keypad Matrix is shown below.
The method of usage is similar to the Matrix LED, by using a row or column scanning method to detect the state of each key’s position by column and row. Take column scanning method as an example, send low level to the first 1 column (Pin1), detect level state of row 5, 6, 7, 8 to judge whether the key A, B, C, D are pressed. Then send low level to column 2, 3, 4 in turn to detect whether other keys are pressed. Therefore, you can get the state of all of the keys.
20.1.3. Circuit
Use pin 9-2 on control board to connect 4x4 keypad.
Schematic diagram |
|
Hardware connection |
|
|
20.1.4. Sketch
20.1.4.1. Get_Input_Characters
Before writing code, we need to import the library needed.
Click “Add .ZIP Library…” and then find Keypad.zip in libraries folder (this folder is in the folder unzipped form the ZIP file we provided). This library can facilitate our operation of keypad.
Now write the code to obtain the keypad characters, and send them to the serial port.
1/*
2 Get_Input_Characters
3
4 modified 2021/7/1
5 by http://www.freenove.com
6*/
7
8#include <Keypad.h>
9
10// define the symbols on the buttons of the keypad
11char keys[4][4] = {
12 {'1', '2', '3', 'A'},
13 {'4', '5', '6', 'B'},
14 {'7', '8', '9', 'C'},
15 {'*', '0', '#', 'D'}
16};
17
18byte rowPins[4] = {2, 3, 4, 5}; // connect to the row pinouts of the keypad
19byte colPins[4] = {6, 7, 8, 9}; // connect to the column pinouts of the keypad
20
21// initialize an instance of class NewKeypad
22Keypad myKeypad = Keypad(makeKeymap(keys), rowPins, colPins, 4, 4);
23
24void setup() {
25 Serial.begin(9600); // Initialize the serial port and set the baud rate to 9600
26}
27
28void loop() {
29 // Get the character input
30 char keyPressed = myKeypad.getKey();
31 // If there is a character input, sent it to the serial port
32 if (keyPressed) {
33 Serial.println(keyPressed);
34 }
35}
In the code, we use a Keypad class provided by the Keypad library to operate the keypad. The following code is to instantiate a keypad object, and the last two parameters represent the number of the row and column of the keypad.
1Keypad myKeypad = Keypad(makeKeymap(keys), rowPins, colPins, 4, 4);
The two-dimensional arrays record the keypad characters, and these characters can be returned when you press the keyboard.
1// define the symbols on the buttons of the keypad
2char keys[4][4] = {
3 {'1', '2', '3', 'A'},
4 {'4', '5', '6', 'B'},
5 {'7', '8', '9', 'C'},
6 {'*', '0', '#', 'D'}
7};
These two arrays record the row and column’s connection pins of keypad.
1byte rowPins[4] = {2, 3, 4, 5}; // connect to the row pinouts of the keypad
2byte colPins[4] = {6, 7, 8, 9}; // connect to the column pinouts of the keypad
Send the input obtained from the keyboard to the computer via the serial port in function loop().
1void loop() {
2 // Get the character input
3 char keyPressed = myKeypad.getKey();
4 // If there is a character input, sent it to the serial port
5 if (keyPressed) {
6 Serial.println(keyPressed);
7 }
8}
Verify and upload the code, open the Serial Monitor and press the keypad, and then you will see the characters you press being printed out.






