4.2 4x4 Keypad

The 4x4 keyboard, also known as the matrix keyboard, is a matrix of 16 keys excluded in a single panel.

The keypad can be found on devices that mainly require digital input, such as calculators, TV remote controls, push-button phones, vending machines, ATMs, combination locks, and digital door locks.

In this project, we will learn how to determine which key is pressed and get the related key value.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

PURCHASE LINK

Kepler Kit

450+

Kepler Kit

You can also buy them separately from the links below.

SN

COMPONENT INTRODUCTION

QUANTITY

PURCHASE LINK

1

Raspberry Pi Pico W

1

BUY

2

Micro USB Cable

1

3

Breadboard

1

BUY

4

Jumper Wires

Several

BUY

5

Resistor

4(10KΩ)

BUY

6

4x4 Keypad

1

BUY

Schematic

sch_keypad

4 pull-down resistors are connected to each of the columns of the matrix keyboard, so that G6 ~ G9 get a stable low level when the keys are not pressed.

The rows of the keyboard (G2 ~ G5) are programmed to go high; if one of G6 ~ G9 is read high, then we know which key is pressed.

For example, if G6 is read high, then numeric key 1 is pressed; this is because the control pins of numeric key 1 are G2 and G6, when numeric key 1 is pressed, G2 and G6 will be connected together and G6 is also high.

Wiring

wiring_keypad

To make the wiring easier, in the above diagram, the column row of the matrix keyboard and the 10K resistors are inserted into the holes where G6 ~ G9 are located at the same time.

Code

Note

  • You can open the file 4.2_4x4_keypad.ino under the path of kepler-kit-main/arduino/4.2_4x4_keypad.

  • Or copy this code into Arduino IDE.

  • Don’t forget to select the board(Raspberry Pi Pico) and the correct port before clicking the Upload button.

  • The library Keypad is used here. Please refer to Add libraries for adding it to the Arduino IDE.

After the program runs, the Shell will print out the keys you pressed on the Keypad.

How it works

By calling the Keypad.h library, you can easily use Keypad.

#include <Keypad.h>

Library Functions:

Keypad(char *userKeymap, byte *row, byte *col, byte numRows, byte numCols)

Initializes the internal keymap to be equal to userKeymap.

userKeymap: The symbols on the buttons of the keypads.

row, col: Pin configuration.

numRows, numCols: Keypad sizes.

char getKey()

Returns the key that is pressed, if any. This function is non-blocking.