ESP32 MicroPython Keypad 4x4
This guide will learn how to use a ESP32 with a 4x4 keypad with MicroPython. We will go through the following steps:
- How to connect a 4x4 keypad to the ESP32.
- How to write MicroPython script for the ESP32 to check if a key is pressed keys on a 4x4 keypad.
- How to write MicroPython script to verify a password entered from a 4x4 keypad.
Hardware Preparation
1 | × | ESP-WROOM-32 Dev Module | |
1 | × | USB Cable Type-C | |
1 | × | Membrane Keypad 4x4 | |
1 | × | Jumper Wires | |
1 | × | Breadboard | |
1 | × | (Recommended) Screw Terminal Expansion Board for ESP32 | |
1 | × | (Recommended) Power Splitter For ESP32 |
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
Overview of 4x4 Keypad
The 4x4 keypad includes 16 soft buttons organized in rows and columns, forming a grid. Each button is referred to as a key.
Pinout
A 4x4 keypad uses 8 pins, split into two types: rows and columns.
- 4 pins connect to the rows (R1, R2, R3, R4).
- 4 pins connect to the columns (C1, C2, C3, C4).
Wiring Diagram
- How to connect ESP32 and keypad 4x4 using breadboard
This image is created using Fritzing. Click to enlarge image
- How to connect ESP32 and keypad 4x4 using screw terminal block breakout board
ESP32 MicroPython Code
Detailed Instructions
Here’s instructions on how to set up and run your MicroPython code on the ESP32 using Thonny IDE:
- Make sure Thonny IDE is installed on your computer.
- Confirm that MicroPython firmware is loaded on your ESP32 board.
- If this is your first time using an ESP32 with MicroPython, check out the ESP32 MicroPython Getting Started guide for step-by-step instructions.
- Connect the 4x4 keypad to the ESP32 as shown in the diagram.
- Connect the ESP32 board to your computer with a USB cable.
- Open Thonny IDE on your computer.
- In Thonny IDE, go to Tools Options.
- Under the Interpreter tab, choose MicroPython (ESP32) from the dropdown menu.
- Make sure the correct port is selected. Thonny IDE usually detects it automatically, but you might need to select it manually (like COM12 on Windows or /dev/ttyACM0 on Linux).
- Navigate to the Tools Manage packages on the Thonny IDE.
- Search “DIYables-MicroPython-Keypad”, then find the Keypad library created by DIYables.
- Click on DIYables-MicroPython-Keypad, then click Install button to install Keypad library.
- Copy the provided MicroPython code and paste it into Thonny's editor.
- Save the code to your ESP32 by:
- Clicking the Save button or pressing Ctrl+S.
- In the save dialog, choose MicroPython device.
- Name the file main.py.
- Click the green Run button (or press F5) to execute the script.
- Press some keys on the 4x4 keypad.
- Check out the message in the Shell at the bottom of Thonny.
If it does not work, please check your wiring carefully. It is very easy to confise when connecting ESP32 to the 4x4 keypad.
Keypad and Password
A keypad is often used to type a password. Here, we use two special keys:
- A key to begin or re-enter the password, like the "*" key.
- A key to complete the password entry, like the "#" key.
The password will consist of other keys, except for the two special keys. When you press a key:
- If the key is neither "*" nor "#", include it in the password being typed.
- If the key is "#", verify if the typed password is correct. Then, reset the password.
- If the key is "*", reset the password.
Keypad - Password Code
- Copy the above code and paste it to the Thonny IDE's editor.
- Save the script to your ESP32
- Type the keys "123" and then hit "#".
- Type the keys "1234A" and then hit "#".
- Check out the message in the Shell at the bottom of Thonny.