ESP32 MicroPython Keypad 3x4

This tutorial instructs you how to use a ESP32 and a 3x4 keypad with MicroPython. In detail, We will learn:

ESP32 MicroPython 3x4 Keypad

Hardware Preparation

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Membrane Keypad 3x4
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of 3x4 Keypad

The 3x4 keypad has 12 soft keys arranged in a grid of rows and columns. Each key represents a button.

Pinout

A 3x4 keypad has 7 pins. These pins are split into two types: rows and columns.

  • 4 pins connect to the rows (R1, R2, R3, R4).
  • 3 pins connect to the columns (C1, C2, C3).
3x4 Keypad Pinout

Wiring Diagram

  • How to connect ESP32 and keypad 3x4 using breadboard
The wiring diagram between ESP32 MicroPython Keypad 3x4

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and 3x4 keypad

ESP32 MicroPython Code

from DIYables_MicroPython_Keypad import Keypad import time NUM_ROWS = 4 NUM_COLS = 3 # Constants for GPIO pins ROW_PINS = [18, 5, 17, 16] # The ESP32 pin GPIO18, GPIO5, GPIO17, GPIO16 connect to the row pins COLUMN_PINS = [4, 0, 2] # The ESP32 pin GPIO4, GPIO0, GPIO2 connect to the column pins # Keymap corresponds to the layout of the keypad 3x4 KEYMAP = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '*', '0', '#'] # Initialize the keypad keypad = Keypad(KEYMAP, ROW_PINS, COLUMN_PINS, NUM_ROWS, NUM_COLS) keypad.set_debounce_time(400) # 400ms, addjust it if it detects twice for single press print("Keypad 3x4 example") # Main loop to check for key presses while True: key = keypad.get_key() if key: print("Key pressed: ", key)

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 your ESP32 to the 3x4 keypad 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.
ESP32 MicroPython 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 a few keys on your keypad.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Key pressed: 3 Key pressed: 6 Key pressed: 9 Key pressed: 4 Key pressed: * Key pressed: #
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

If it does not work, please check your wiring carefully. It is very easy to confise when connecting ESP32 to the keypad 3x4.

Keypad and Password

A keypad is often used to type in a password. Here, we focus on two important keys:

  • A key to start or restart typing the password. For example, use the key "*."
  • A key to finish typing the customer. For instance, use the key "#."

The password will consist of the other keys, except for two special keys that are not included.

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 clear it.
  • If the key is "*", clear the password.

Keypad - Password Code

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-keypad-3x4 """ from DIYables_MicroPython_Keypad import Keypad import time NUM_ROWS = 4 NUM_COLS = 3 # Constants for GPIO pins ROW_PINS = [18, 5, 17, 16] # The ESP32 pin GPIO18, GPIO5, GPIO17, GPIO16 connect to the row pins COLUMN_PINS = [4, 0, 2] # The ESP32 pin GPIO4, GPIO0, GPIO2 connect to the column pins # Keymap corresponds to the layout of the keypad 3x4 KEYMAP = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '*', '0', '#'] # Initialize the keypad keypad = Keypad(KEYMAP, ROW_PINS, COLUMN_PINS, NUM_ROWS, NUM_COLS) keypad.set_debounce_time(400) # 400ms, addjust it if it detects twice for single press # Define the correct password correct_password = "1234" input_password = "" print("Keypad 3x4 password") # Main loop while True: key = keypad.get_key() if key: print(key) if key == '*': input_password = "" # Clear input password elif key == '#': if input_password == correct_password: print("Password is correct") # DO YOUR WORK HERE else: print("Password is incorrect, try again") input_password = "" # Clear input password else: input_password += key # Append new character to input password string
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your ESP32
  • Type in "123", follow with pressing “#.”
  • Type in "1234", then press "#."
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot MPY: soft reboot Keypad 3x4 password 1 2 3 # Password is incorrect, try again 1 2 3 4 # Password is correct
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!