4x4 Matrix Blue Keypad - EN
4x4 Matrix Blue Keypad - EN
Thank you for purchasing our AZ-Delivery 4x4 Matrix Keypad Module. On
the following pages, you will be introduced to how to use and set-up this
handy device.
Have fun!
-1-
Table of Contents
Introduction....................................................................................................3
Specifications................................................................................................4
The pinout.....................................................................................................5
How to set-up Arduino IDE............................................................................6
How to set-up the Raspberry Pi and the Python.........................................10
Connecting the keypad with microcontroller board......................................11
Arduino IDE Library.....................................................................................13
Sketch example.......................................................................................14
Connecting the keypad with Raspberry Pi...................................................18
Python script............................................................................................19
-2-
Introduction
Keypads are a great way to make users interact with electronic devices. It
can be use for navigating menus, passwords, playing games, controlling
robots, etc.
The keys on the 4x4 matrix keypad module are simple momentary switches.
Keys are made of thin conductive material that is stacked with an isolation
foil in between. Keypad membranes are curved in the shape of the dome,
so that keys do not make permanent contact when pressed.
The keys on the keypad are arranged in rows and columns. The 4x4 keypad
has 4 rows and 4 columns as shown on the following image:
-3-
Specifications
-4-
The pinout
The 4x4 matrix keypad module has eight pins. The pinout is shown on the
following image:
-5-
How to set-up Arduino IDE
If the Arduino IDE is not installed, follow the link and download the
installation file for the operating system of choice.
For Windows users, double click on the downloaded .exe file and follow
the instructions in the installation window.
-6-
For Linux users, download a file with the extension .tar.xz, which has to
be extracted. When it is extracted, go to the extracted directory and open
the terminal in that directory. Two .sh scripts have to be executed, the first
called arduino-linux-setup.sh and the second called install.sh.
To run the first script in the terminal, open the terminal in the extracted
directory and run the following command:
sh arduino-linux-setup.sh user_name
user_name - is the name of a superuser in the Linux operating system. A
password for the superuser has to be entered when the command is
started. Wait for a few minutes for the script to complete everything.
After the installation of these scripts, go to the All Apps, where the
Arduino IDE is installed.
-7-
Almost all operating systems come with a text editor preinstalled (for
example, Windows comes with Notepad, Linux Ubuntu comes with
Gedit, Linux Raspbian comes with Leafpad, etc.). All of these text
editors are perfectly fine for the purpose of the eBook.
-8-
If the Arduino IDE is used on Windows, port names are as follows:
-9-
How to set-up the Raspberry Pi and Python
For the Raspberry Pi, first the operating system has to be installed, then
everything has to be set-up so that it can be used in the Headless mode.
The Headless mode enables remote connection to the Raspberry Pi,
without the need for a PC screen Monitor, mouse or keyboard. The only
things that are used in this mode are the Raspberry Pi itself, power supply
and internet connection. All of this is explained minutely in the free eBook:
Raspberry Pi Quick Startup Guide
- 10 -
Connecting the keypad with Atmega328P Board
Connect the 4x4 matrix keypad module with the microcontroller board as
shown on the following connection diagram:
Columns Rows
Keypad pin Board pin Keypad pin Board pin
C1 D3 R1 D7
C2 D2 R2 D6
C3 D1 R3 D5
C4 D0 R4 D4
- 11 -
Library for Arduino IDE
- 12 -
Sketch example
#include <Keypad.h>
#define COLS 4
#define ROWS 4
char KEYS[ROWS][COLS]={
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte colPIN[COLS] = { 3, 2, 1, 0 };
byte rowPIN[ROWS] = { 7, 6, 5, 4 };
char pressedTaster;
Keypad myKeypad = Keypad(makeKeymap(KEYS), rowPIN, colPIN, ROWS, COLS);
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.end();
pressedTaster = myKeypad.getKey();
if (pressedTaster) {
Serial.begin(9600);
Serial.print(pressedTaster);
Serial.println();
}
}
- 13 -
Upload the sketch to the microcontroller board and open Serial Monitor
(Tools > Serial Monitor). The result should look like the output on
the following image:
- 14 -
The sketch starts with including the library Keypad.
Two macros are created which define how many rows and columns are on
the keypad. In this case, there are four rows and four columns.
After that, two byte arrays called rowPin and colPin are created and
they define which pins are used on microcontroller board for row and
column pin, respectively.
In the setup() function the serial communication is started with the baud
rate of 9600bps.
Then, in the loop() function, the state of the keypad is read with the
following line of code: char key = keypad.getKey();
- 15 -
Next, the state of the key variable is checked and corresponding message
is displayed in the Serial Monitor. If no key is pressed, the value saved in
the key variable is None, which means that when the value is check with
if block, the if block results is false and the block is skipped. If one key
is pressed, the value of that key is saved in the key variable. The if block
results with true after which the value in the key variable is checked again.
This time the value is checked for specific name, the name of specific key of
the keypad. In the sketch the value that is checked is the character '4':
if(key == '4')
If other pins are pressed only pin names are displayed in the Serial Monitor.
- 16 -
Connecting the keypad with Raspberry Pi
Connect the 4x4 matrix keypad module with the Raspberry Pi as shown on
the following connection diagram:
Columns Rows
Keypad pin RaspPi pin Physical pin Keypad pin RaspPi pin Physical pin
C1 GPIO6 31 R1 GPIO17 11
C2 GPIO13 33 R2 GPIO27 13
C3 GPIO19 35 R3 GPIO22 15
C4 GPIO26 37 R4 GPIO5 19
- 17 -
Python script
Two scripts are made. The first is the class script and the second is the
main executable script. The following script code is modified from the script.
def getKey(self):
# Set all columns as output low
for j in range(len(self.COL)):
GPIO.setup(self.COL[j], GPIO.OUT)
GPIO.output(self.COL[j], GPIO.LOW)
- 18 -
# two tabs
# Scan rows for pushed key/button
# A valid key press should set "rowVal" between 0 and 3.
rowVal = -1
for i in range(len(self.ROW)):
tmpRead = GPIO.input(self.ROW[i])
if tmpRead == 0:
rowVal = i
- 19 -
# two tabs
if len(self.COL) == 3:
if colVal < 0 or colVal > 2:
self.exit()
return
def exit(self):
# Reinitialize all rows and columns as input at exit
for i in range(len(self.ROW)):
GPIO.setup(self.ROW[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
for j in range(len(self.COLUMN)):
GPIO.setup(self.COL[j],GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.cleanup()
- 20 -
The following code is the code of main script:
import time
import keypad_class as kpad
last_digit = digit
time.sleep(0.1)
except KeyboardInterrupt:
print('\nSrcipt end!')
finally:
kp.exit()
- 21 -
Save the script by the name keypad.py in the same directory where
keypad_class.py script is saved. To run the main script, open the
terminal in the directory where the scripts are saved and run the following
command:
python3 keypad.py
The result should look like the output on the following image:
The class script is not covered in this book, only the main script is
explained.
- 22 -
The script keypad.py starts with importing one library, time and with
importing constructor from the keypad_class script.
Next, the object called kp is created with the following line of code:
kp = keypad(4, 17, 27, 22, 24, 25, 12, 16)
where these numbers represent the pins of the Raspberry Pi to which
keypad pins are connected (C1, C2, C3, C4, R1, R2, R3, R4). The kp object
represents the keypad itself.
After this, two variables digit and last_digit are created. These two
variables are used for detecting which key is pressed.
To end the script press CTRL + C on the PC keyboard. This is called the
keypad interrupt. When the keypad interrupt happens, the except block of
code is executed and the message Script end! is displayed in the
terminal.
The finally block of code is executed on the script end. In this block of
code all used GPIO pin modes and/or interfaces are disabled.
- 23 -
In the indefinite block of code, first the state of the keypad is read, with the
following line of code: digit = kp.keypad()
Then, the state is checked if the new pressed key is different from the last
pressed key. If it is different, then new key state is displayed and the value
of the new pressed key is saved in the last pressed key variable
last_digit. The keypad state when the key is pressed is displayed in the
terminal. The state of the keypad is displayed only when the state is
changed, even if the key is pressed for a longer period of time.
digit = kp.getKey()
if not last_digit == digit:
if not digit == None:
print('{}'.format(digit))
last_digit = digit
time.sleep(0.1)
- 24 -
Now is the time to learn and make projects on your own. You can do that
with the help of many example scripts and other tutorials, which can be
found on the Internet.
https://az-delivery.de
Have Fun!
Impressum
https://az-delivery.de/pages/about-us
- 25 -