DEV Community

Cover image for Arduino MicroPython Keypad 3x4
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

Arduino MicroPython Keypad 3x4

This build turns a cheap 3x4 membrane keypad into an input device for an Arduino running MicroPython. It's a handy pattern any time you need a human to type something physical into your project, like a PIN or a mode selector, instead of relying on a serial console or a web UI.

You'll wire up the keypad, read key presses in MicroPython, and then build a simple password-entry flow with the * and # keys as controls.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: Arduino MicroPython Keypad 3x4

What you need

Wiring

Arduino MicroPython Keypad 3x4 wiring diagram

A 3x4 keypad has 7 pins: 4 for rows (R1-R4) and 3 for columns (C1-C3). There's no power or ground pin to worry about, the keypad is just a grid of switches. On the Arduino Giga R1 WiFi, the row pins connect to D14, D15, D16, D17 and the column pins connect to D18, D19, D20. Jumper wires are enough for this, no resistors needed since the library handles the pin logic internally.

How it works

Each key sits at the intersection of one row wire and one column wire, with 12 keys total for 4 rows and 3 columns. Pressing a key electrically connects its row to its column. The microcontroller scans through the rows and columns to figure out which intersection just got connected, and that tells it which key was pressed. This scanning is handled for you by the keypad library, so your code just asks for the result.

Code

The sketch uses the DIYables_MicroPython_Keypad library. You define ROW_PINS and COLUMN_PINS matching your wiring, a KEYMAP list describing which character sits at each grid position, and create a Keypad object from those. set_debounce_time() prevents a single physical press from being read as multiple presses. In the main loop, get_key() returns the pressed key or nothing, which you print to the Thonny shell.

The password example builds on this: * clears whatever's typed so far, # checks the typed password against an expected value and then clears it, and every other key gets appended to the password string.

arduino-micropython-keypad-3x4.py

from DIYables_MicroPython_Keypad import Keypad
import time

NUM_ROWS = 4
NUM_COLS = 3

# Constants for GPIO pins
ROW_PINS = ['D14', 'D15', 'D16', 'D17']  # The Arduino Giga WiFi pins connected to the row pins
COLUMN_PINS = ['D18', 'D19', 'D20']  # The Arduino Giga WiFi pins connected 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("Arduino MicroPython - Keypad 3x4 example")

# Main loop to check for key presses
while True:
    key = keypad.get_key()
    if key:
        print("Key pressed: ", key)
Enter fullscreen mode Exit fullscreen mode

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-keypad-3x4

Top comments (0)