Chapter 22 Matrix Keypad
Earlier we learned about a single Push Button Switch. In this chapter, we will learn about Matrix Keyboards, which integrates a number of Push Button Switches as Keys for the purposes of Input.
Project 22.1 Matrix Keypad
In this project, we will attempt to get every key code on the Matrix Keypad to work.
Component List
ESP32-WROVER x1
|
GPIO Extension Board x1
|
Breadboard x1
|
|
4x4 Matrix Keypad x1
|
Jumper M/M x8 |
Component knowledge
4x4 Matrix 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:
Similar to the integration of a 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 usage is similar to the LED matrix, 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.
Circuit
Schematic diagram |
|
Hardware connection |
If you need any support, please feel free to contact us via: support@freenove.com
|
Code
This code is used to obtain all key codes of the 4x4 Matrix Keypad, when one of the keys is pressed, the key code will be printed out via serial port.
Move the program folder “Freenove_Ultimate_Starter_Kit_for_ESP32/Python/Python_Codes” to disk(D) in advance with the path of “D:/Micropython_Codes”.
Open “Thonny”, click “This computer” -> “D:” -> “Micropython_Codes” -> “22.1_Matrix_Keypad”. Select”keypad.py”, right click your mouse to select “Upload to /”, wait for “keypad.py” to be uploaded to ESP32-WROVER and then double click “Matrix_Keypad.py”.
22.1_Matrix_Keypad
Click “Run current script”, push the key board and the key value will be printed in “Shell”. As shown in the illustration below:
The following is the program code:
1from keypad import KeyPad
2import time
3
4keyPad=KeyPad(14,27,26,25,13,21,22,23)
5
6def key():
7 keyvalue=keyPad.scan()
8 if keyvalue!= None:
9 print(keyvalue)
10 time.sleep_ms(300)
11 return keyvalue
12
13while True:
14 key()
Import keypad module.
1from keypad import KeyPad
Associate the keypad module to ESP32 pins.
1keyPad=KeyPad(14,27,26,25,13,21,22,23)
Call function keypad.scan() of the keypad module. When the keypad module detects that the key is pressed, it returns the value of the pressed key; when no key is pressed, the return value is None.
1keyvalue=keyPad.scan()
Call function keyPan.scan() to obtain the value of the pressed key. Once it is obtained, print it out.
1def key():
2 keyvalue=keyPad.scan()
3 if keyvalue!= None:
4 print(keyvalue)
5 time.sleep_ms(300)
6 return keyvalue
Reference
- Class keypad
Before each use of the object KeyPad , please make sure keypad.py has been uploaded to “/” of ESP32 and then add the statement “ from keypad import KeyPad “ to the top of python file.
KeyPad(row1,row2,row3,row4,col1,col2,col3,col4): Intialize keypad module and associate its pins with ESP32.
scan(): Non-blocking keypad scan function. If no key is pressed, it returns None; Otherwise, it returns the valued of the pressed key.
Project 22.2 Keypad Door
In this project, we use keypad as a keyboard to control the action of the servo motor.
Component List
ESP32-WROVER x1
|
GPIO Extension Board x1
|
|
Breadboard x1
|
||
Jumper M/M
|
Servo x1 |
4x4 Matrix Keypad x1 |
NPN transistorx1 (S8050)
|
Active buzzer x1 |
Resistor 1kΩ x1 |
Circuit
Schematic diagram |
|
Hardware connection |
If you need any support, please feel free to contact us via: support@freenove.com
|
Code
Move the program folder “Freenove_Ultimate_Starter_Kit_for_ESP32/Python/Python_Codes” to disk(D) in advance with the path of “D:/Micropython_Codes”.
Open “Thonny”, click “This computer” -> “D:” -> “Micropython_Codes” -> “22.2_Keypad_Door”. Select “keypad.py” and “myservo.py”, right click your mouse to select “Upload to /”, wait for “keypad.py” and “myservo.py” to be uploaded to ESP32-WROVER and then double click “Keypad_Door.py”.
22.2_Keypad_Door
Click “Run current script”, press the keypad to input password with 4 characters. If the input is correct, the servo will move to a certain degree, and then return to the original position. If the input is wrong, an input error alarm will be generated.
The following is the program code:
1from myservo import myServo
2from keypad import KeyPad
3from machine import Pin
4import time
5
6keyPad=KeyPad(14,27,26,25,13,21,22,23)
7servo=myServo(19)
8servo.myServoWriteAngle(0)
9activeBuzzer=Pin(18,Pin.OUT)
10
11keyOut=['1','2','3','4']
12keyIn=['','','','']
13def key():
14 keyvalue=keyPad.scan()
15 if keyvalue!= None:
16 print('Your input:',keyvalue)
17 time.sleep_ms(200)
18 return keyvalue
19
20keyInNum=0
21try:
22 while True:
23 keydata=key()
24 if keydata!=None:
25 activeBuzzer.value(1)
26 time.sleep_ms(100)
27 activeBuzzer.value(0)
28 keyIn[keyInNum]=keydata
29 keyInNum=keyInNum+1
30
31 if keyInNum==4:
32 if keyIn==keyOut:
33 print("passWord right!")
34 servo.myServoWriteAngle(90)
35 time.sleep_ms(1000)
36 servo.myServoWriteAngle(0)
37 else:
38 print("passWord error!")
39 activeBuzzer.value(1)
40 time.sleep_ms(1000)
41 activeBuzzer.value(0)
Define an array and set the password.
1keyOut=['1','2','3','4']
Each time a key is pressed, the buzzer will short beep once, and the key value of the key will be stored in the keyIn array.
1while True:
2 keydata=key()
3 if keydata!=None:
4 activeBuzzer.value(1)
5 time.sleep_ms(100)
6 activeBuzzer.value(0)
7 keyIn[keyInNum]=keydata
When 4 keys are pressed, it will judge whether the password is correct. If it is correct, the servo will rotate 90 degrees, and then turn back after 1 second. If the password is wrong, the buzzer will long beep once and the keyInNum value will be cleared.
1if keyInNum==4:
2 if keyIn==keyOut:
3 print("passWord right!")
4 servo.myServoWriteAngle(90)
5 time.sleep_ms(1000)
6 servo.myServoWriteAngle(0)
7 else:
8 print("passWord error!")
9 activeBuzzer.value(1)
10 time.sleep_ms(1000)
11 activeBuzzer.value(0)












