
Cómo funciona
Un teclado matricial explorado como cualquier otro: cuatro filas G0–G3 se activan de una en una y se leen cuatro columnas G4–G7: 8 pines para 16 teclas. El script explora continuamente y detecta nuevas pulsaciones por flanco (las teclas del lienzo quedan retenidas hasta otro clic), formando una cadena: dígitos añaden, * borra y # envía. El PIN correcto (1234) imprime UNLOCKED y cambia la OLED; cualquier otro obtiene DENIED.
Componentes del montaje
- ESP32-C3
- OLED 128×64
- Teclado 4×4 (Keypad 4×4)
Cómo está conectado
- ESP32-C3 · g0→Teclado 4×4 (Keypad 4×4) · r0
- ESP32-C3 · g1→Teclado 4×4 (Keypad 4×4) · r1
- ESP32-C3 · g2→Teclado 4×4 (Keypad 4×4) · r2
- ESP32-C3 · g3→Teclado 4×4 (Keypad 4×4) · r3
- ESP32-C3 · g4→Teclado 4×4 (Keypad 4×4) · c0
- ESP32-C3 · g5→Teclado 4×4 (Keypad 4×4) · c1
- ESP32-C3 · g6→Teclado 4×4 (Keypad 4×4) · c2
- ESP32-C3 · g7→Teclado 4×4 (Keypad 4×4) · c3
- ESP32-C3 · 3v3→OLED 128×64 · vcc
- ESP32-C3 · gnd2→OLED 128×64 · gnd
- ESP32-C3 · g9→OLED 128×64 · scl
- ESP32-C3 · g8→OLED 128×64 · sda
El código
Este script de MicroPython se ejecuta en la placa emulada en cada arranque; edítalo en Code.
from machine import I2C, Pin import framebuf, time i = I2C(0, scl=Pin(9), sda=Pin(8)) buf = bytearray(1024) fb = framebuf.FrameBuffer(buf, 128, 64, framebuf.MONO_VLSB) for c in b'\xae\xd5\x80\xa8\x3f\xd3\x00\x40\x8d\x14\x20\x00\xa1\xc8\xda\x12\x81\xcf\xd9\xf1\xdb\x40\xa4\xa6\xaf': i.writeto(0x3c, bytes([0, c])) def show(): i.writeto(0x3c, b'\x00\x21\x00\x7f\x22\x00\x07') i.writeto(0x3c, b'\x40' + buf) # TRY IT: click 1 2 3 4 on the keypad, then # to unlock (* clears). # 4x4 matrix keypad: rows drive, columns read (active-high scan). rows = [Pin(n, Pin.OUT) for n in (0, 1, 2, 3)] cols = [Pin(n, Pin.IN, Pin.PULL_DOWN) for n in (4, 5, 6, 7)] KEYS = ['123A', '456B', '789C', '*0#D'] def scan(): pressed = set() for r in range(4): for x in range(4): rows[x].value(1 if x == r else 0) time.sleep_ms(2) # let the row drive settle before reading for c in range(4): if cols[c].value(): pressed.add(KEYS[r][c]) return pressed PIN = '1234' entry = '' msg = 'TRY 1234 THEN #' prev = set() def render(): fb.fill(0) fb.text('SECURE LOCK', 20, 2) fb.text('CODE: ' + '*' * len(entry), 8, 24) fb.text(msg, 8, 46) show() render() print('lock ready -- click 1 2 3 4 on the keypad, then # to unlock (* clears)') while True: cur = scan() for k in cur - prev: # keys newly pressed since the last scan if k == '#': msg = 'UNLOCKED' if entry == PIN else 'DENIED' print('submit', entry, '->', msg) if msg == 'DENIED': print('hint: the code is 1234, then # to submit') entry = '' elif k == '*': entry, msg = '', 'CLEARED' elif k in '0123456789': entry = (entry + k)[:8] msg = 'THEN # TO SUBMIT' if entry else 'TRY 1234 THEN #' prev = cur render() time.sleep_ms(40)
Prueba esto
- Pulsa 1 2 3 4 y #: UNLOCKED en OLED y Serial.
- Introduce algo incorrecto y #: DENIED; * borra un error.
- Cambia PIN en el script y ejecuta de nuevo.
- Recuerda la retención: vuelve a pulsar una tecla retenida para liberarla antes de la próxima entrada.



