🐺 Tutorial 23 - 4x4 Keypad
The 4x4 Matrix Keypad is a commonly used input device that provides a simple way to accept user input in the form of numbers, letters, or symbols. It consists of 16 keys arranged in a 4x4 matrix, with each key representing a unique combination of a row and a column.
To use the keypad, the row and column pins are connected to the microcontroller and a scanning algorithm is used to detect which key has been pressed. The scanning algorithm can be implemented in software or hardware, depending on the specific application requirements.
The 4x4 Matrix Keypad can be used in a wide range of applications, including security systems, access control systems, electronic door locks, and other devices that require user input. It is simple to use and cost-effective, making it a popular choice for hobbyists and professionals alike.
The 4x4 Matrix Keypad is available in a variety of package types, including membrane and mechanical, and can be easily integrated into electronic projects using a variety of programming languages, including C, C++, and Python.
Overall, the 4x4 Matrix Keypad is a versatile and widely-used input device that provides a simple and effective way to accept user input in a variety of applications. Its ease of use and compatibility with a wide range of devices make it a popular choice for hobbyists and professionals alike.
Components Needed
Component | Quantity |
---|---|
Raspberry Pi Pico W | 1 |
Micro USB Cable | 1 |
Breadboard | 1 |
Wires | Several |
Resistor | 4 - 10KΩ |
4x4 Keypad | 1 |
Fritzing Diagram
Code
import machine
import time
characters = [["1","2","3","A"],["4","5","6","B"],["7","8","9","C"],["*","0","#","D"]]
pin = [2,3,4,5]
row = []
for i in range(4):
row.append(None)
row[i] = machine.Pin(pin[i], machine.Pin.OUT)
pin = [6,7,8,9]
col = []
for i in range(4):
col.append(None)
col[i] = machine.Pin(pin[i], machine.Pin.IN)
def readKey():
key = []
for i in range(4):
row[i].high()
for j in range(4):
if(col[j].value() == 1):
key.append(characters[i][j])
row[i].low()
if key == [] :
return None
else:
return key
last_key = None
while True:
current_key = readKey()
if current_key == last_key:
continue
last_key = current_key
if current_key != None:
print(current_key)
time.sleep(0.1)