MicroPython keypad door lock on a Pico: servo and buzzer
MicroPython on a Raspberry Pi Pico: type 1234 on a 4x4 keypad and a servo on GP0 opens the bolt; each key beeps a buzzer on GP15, a wrong code beeps long. The whole build, a Raspberry Pi Pico and 5 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Advanced About 45 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $8.55. On Mokxi they're free.
What you'll learn
- Scanning a matrix keypad row by row
- Splitting MicroPython code into modules
- Positioning a servo with PWM pulse width
- Collecting input and checking a code
- Driving a buzzer through a transistor
- Difficulty: Advanced
- Time: About 45 minutes
- Board: Raspberry Pi Pico
- Parts: 8
How it works
A 4x4 keypad is wired to eight Pico pins: four rows on GP10 to GP13 and four columns on GP6 to GP9. The keypad.py module drives each row high in turn and checks which column goes high.
Every key press beeps a buzzer on GP15 for 50 ms, and the key joins the digits typed so far. After four keys, the code is compared with CODE.
If you typed 1234, a servo on GP0 moves to 90 degrees for one second, then back to 0. Anything else gets a one second beep.
Parts list
8 parts, plus the jumper wires. They retail for about $8.55; on Mokxi they're free.
- 1 × Raspberry Pi PicoIn Fight Stick Kit.
- 1 × Keypad, 4x4In Vending Machine Kit, Escape Box Kit.
- 1 × Hobby servoIn Halloween Prop Kit, Christmas Lights Kit, Starter Projects Kit.
- 1 × Resistor, 1k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Transistor (NPN or PNP)In LED Cube Kit.
- 1 × BuzzerIn Starter Kit, Rocket Flight Computer Kit.
- 1 × Power, 5 V
- 1 × Power, 3.3 V
Kits that can build it
This starter kit has every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Hobby servo pin PWM | Raspberry Pi Pico pin GP0 |
| Keypad, 4x4 pin C4 | Raspberry Pi Pico pin GP6 |
| Keypad, 4x4 pin C3 | Raspberry Pi Pico pin GP7 |
| Keypad, 4x4 pin C2 | Raspberry Pi Pico pin GP8 |
| Keypad, 4x4 pin C1 | Raspberry Pi Pico pin GP9 |
| Keypad, 4x4 pin R4 | Raspberry Pi Pico pin GP10 |
| Keypad, 4x4 pin R3 | Raspberry Pi Pico pin GP11 |
| Keypad, 4x4 pin R2 | Raspberry Pi Pico pin GP12 |
| Keypad, 4x4 pin R1 | Raspberry Pi Pico pin GP13 |
| Resistor, 1k Ω pin 1 | Raspberry Pi Pico pin GP15 |
| Hobby servo pin GND | Ground |
| Hobby servo pin VCC | 5 V |
| Transistor (NPN or PNP) pin B | Resistor, 1k Ω pin 2 |
| Buzzer pin 2 | Transistor (NPN or PNP) pin C |
| Transistor (NPN or PNP) pin E | Ground |
| Buzzer pin 1 | 3.3 V |
The code
The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.
# A keypad door lock: every key beeps the buzzer (GP15, through an
# S8050). After four keys, 1234 swings the servo bolt on GP0 open for a
# second; anything else gets a long beep.
from machine import Pin
import time
from keypad import Keypad
from servo import Servo
CODE = "1234"
keys = Keypad(rows=(13, 12, 11, 10), cols=(9, 8, 7, 6))
buzzer = Pin(15, Pin.OUT, value=0)
bolt = Servo(0)
bolt.angle(0)
def beep(ms):
buzzer.value(1)
time.sleep_ms(ms)
buzzer.value(0)
typed = ""
print("Type the code")
while True:
key = keys.scan()
if key:
beep(50)
typed += key
print("typed", typed)
if len(typed) == 4:
if typed == CODE:
print("open")
bolt.angle(90)
time.sleep(1)
bolt.angle(0)
else:
print("wrong code")
beep(1000)
typed = ""
time.sleep_ms(20)
# keypad.py: a 4x4 matrix keypad.
# Each row is driven high in turn; a held key joins its row to its column,
# so that column reads high. The columns are pulled down when nothing is held.
from machine import Pin
import time
KEYS = ("123A", "456B", "789C", "*0#D")
class Keypad:
def __init__(self, rows, cols):
self.rows = [Pin(n, Pin.OUT, value=0) for n in rows]
self.cols = [Pin(n, Pin.IN, Pin.PULL_DOWN) for n in cols]
self.last = None
def held(self):
for r, row in enumerate(self.rows):
row.value(1)
time.sleep_us(50)
for c, col in enumerate(self.cols):
if col.value():
row.value(0)
return KEYS[r][c]
row.value(0)
return None
def scan(self):
"""The key just pressed, once per press, or None."""
key = self.held()
pressed = key if key != self.last else None
self.last = key
return pressed
# servo.py: a hobby servo on a PWM pin.
# The servo wants a pulse every 20 ms (50 Hz); the pulse's width picks the
# angle: 0.5 ms is 0 degrees and 2.5 ms is 180 degrees.
from machine import Pin, PWM
class Servo:
def __init__(self, pin):
self.pwm = PWM(Pin(pin))
self.pwm.freq(50)
def angle(self, degrees):
degrees = max(0, min(180, degrees))
self.pwm.duty_ns(500_000 + int(degrees * 2_000_000 / 180))
Code walkthrough
The key functions of the sketch, and what each one does.
beep main.py
Turns the buzzer pin on, waits for the given number of milliseconds, then turns it off. A key press uses 50 ms. A wrong code uses 1000 ms. The buzzer sits behind a transistor, so the Pico pin only drives the base.
def beep(ms):
buzzer.value(1)
time.sleep_ms(ms)
buzzer.value(0)Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Keys type the wrong digits This project
Check that the row and column wires are not swapped. Rows go to GP13 down to GP10 and columns go to GP9 down to GP6, in the order used inKeypad(rows=..., cols=...). - The servo jitters or does not move This project
The servo needs 5V and ground. Its signal wire goes to GP0. - The right code does not open the bolt This project
Enter exactly four keys. After the fourth, the sketch checks and then clears what you typed. - Pressing one key reports several keys, or the wrong key. Keypad
Match the keymap and the row and column pin order to your keypad's pinout. Without diodes, pressing three keys at once can ghost a fourth. - No keys are detected. Keypad
Rows need pull-ups, or use INPUT_PULLUP, while one column is driven low at a time. The Keypad library does this for you if you pass the pins in the right order. - The servo jitters, twitches or resets the board when it moves. Hobby servo
Power it from its own 5 V supply, not the Arduino 5 V pin, and join the grounds. A hobby servo can pull several hundred milliamps. Add a 100 uF to 470 uF capacitor across its supply. - The servo hums at the end of its travel or will not reach full angle. Hobby servo
Stay inside the pulse range, about 1000 to 2000 us for most hobby servos. Pushing past the end stop makes it strain. Calibrate the min and max pulse for your servo. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.