matrix keypad/Two builds and a check/09. The first keypress
Two builds and a check · 09 of 11

The first keypress

Four wires from the adapter to an Uno or an ESP32, one library from the Library Manager, and a sketch that prints each key once as it goes down. Feed the adapter from the board's own logic voltage, not from 5 V.

Install the library

In the Arduino IDE, open Sketch → Include Library → Manage Libraries, search for I2CKeyPad and install the one by Rob Tillaart. It needs nothing else.

Four wires

Four wires
ESP32
Your board
Adapter VCC from
SDA
GPIO 21
SCL
GPIO 22
SDA and SCL idle at
3.3 V
GND, VCC, SDA, SCL — and VCC from the board’s 3V3. That makes the bus idle at 3.3 V, the voltage the board’s pins run at. No level shifter and no extra resistors: the adapter carries its own pull-ups.
AdapterArduino UnoESP32ESP32-S3
GNDGNDGNDGND
VCC5V3V33V3
SDAA4GPIO 21GPIO 8
SCLA5GPIO 22GPIO 9

The ESP32 pins are the Arduino core's defaults. VCC is the one to get right: the adapter's pull-ups go to it, so it decides what voltage SDA and SCL sit at.

Wiring diagram: a Lonely Binary Uno on the left and a TK85 adapter on the right, with four wires — the Uno's 5V to VCC in red, GND to GND in grey, A4 to SDA in green and A5 to SCL in blue — and the adapter's keypad header at the top marked keyboard.
On an Uno: 5V to VCC, GND to GND, A4 to SDA, A5 to SCL. The keypad plugs into the header at the top.

Run it

Upload the sketch below, open the serial monitor at 115200 and press a few keys. Each one prints once. Holding a key prints nothing more; releasing it and pressing again prints it again.

The loop asks for the key every 20 ms and prints only when the answer changes. That one line — if (c != last) — is what stops a held key printing fifty times a second, and it is also why the N between presses and the brief F of a fast typist never appear.

The code

first_keypress.ino

Prints each key once, as it goes down. It checks the adapter answers before anything else, and reads every 20 ms — fast enough for any typist, slow enough to hide contact bounce. Uncomment the map for the keypad in your hand.

// The first keypress: print every key once, as it goes down.
//
// Wiring, adapter to board:
//   Adapter GND -> board GND
//   Adapter VCC -> board 5V on an Uno, 3V3 on an ESP32 or ESP32-S3
//   Adapter SDA -> Uno A4, ESP32 GPIO 21, ESP32-S3 GPIO 8
//   Adapter SCL -> Uno A5, ESP32 GPIO 22, ESP32-S3 GPIO 9
//
// Arduino IDE: install "I2CKeyPad" by Rob Tillaart from the Library
// Manager. Any of the three boards, no special Tools settings.
// Serial Monitor at 115200.

#include <Wire.h>
#include "I2CKeyPad.h"

const uint8_t KEYPAD_ADDRESS = 0x20;   // as shipped

// 16 keys, then N (no key) and F (fail). Pick the one for your keypad.
char keymap[19] = "DCBA#9630852*741NF";      // soft keypad: 5-pack, TinkerBlock
// char keymap[19] = "D#0*C987B654A321NF";   // heavy-duty keypad: 2-pack

I2CKeyPad keyPad(KEYPAD_ADDRESS);
char last = 'N';

void setup() {
  Serial.begin(115200);
  delay(500);
  Wire.begin();
  Wire.setClock(100000);   // the PCF8574 is rated for 100 kHz
  if (!keyPad.begin()) {
    Serial.println("No keypad at 0x20 - check GND, VCC, SDA and SCL.");
    while (true) delay(1000);
  }
  keyPad.loadKeyMap(keymap);
  Serial.println("Ready. Press a key.");
}

void loop() {
  char c = keyPad.getChar();
  if (c != last) {                       // only when something changed
    if (c != 'N' && c != 'F') Serial.println(c);
    last = c;
  }
  delay(20);
}

If it prints “No keypad at 0x20”, nothing answered at that address: check the four wires, then run the bus scan in the last article. If keys print but the wrong ones, it is the keymap, not the wiring.

When it does not work

It prints “No keypad at 0x20”

Nothing answered. Check that GND and VCC are really connected, that SDA and SCL are not swapped, and that they go to your board's I²C pins — A4 and A5 on an Uno, not the digital pins. If an address pad on the back has been bridged, the adapter is at a different address; scan the bus.

Keys print, but the wrong characters

The other keypad's map is loaded. Swap which keymap line is commented out. If neither is right — a keypad from elsewhere, or a socket fitted upside down — run the keymap finder.

Nothing appears in the serial monitor at all

Set the monitor to 115200 baud, the speed in Serial.begin(). On an ESP32-S3 with two USB sockets, make sure the monitor is open on the port the sketch was uploaded through.

Can I run the adapter from 5 V on an ESP32 to be safe?

No — that is the unsafe choice. The adapter's pull-ups tie SDA and SCL to its VCC, so from 5 V both lines sit at 5 V against pins rated for 3.3 V. The chip runs on anything from 2.5 V, so 3V3 is correct.

Where this goes next

The same four wires and the same loop, with a code, a lockout and a pin that opens.

A PIN code lock

Edit this page — content/books/matrix-keypad/first-keypress.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

This page covers several products. Choose yours to see the right discussions.

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum