I2Cbeginner

Matrix Keypad with I2C Adapter (4x4, PCF8574)

A 4×4 keypad on two I²C wires instead of eight GPIO pins. The TK85 adapter at a fixed 0x20, the compact adapter in the keypad packs with eight addresses, the soft and heavy-duty keypads, and the two keymaps they need.

Comes in this kit — not sold separately
I2C 4x4 Matrix Membrane Keypad, 5 Sets
4x4 Matrix Keypad with I2C Adapter, Heavy Duty, 2-Pack

Specifications

TinkerBlock TK85The adapter's v2 form: a 22.4 × 30.4 mm TinkerBlock board with a gold edge and two LEGO holes, in the 46-piece kit
Soft keypad 5-pack5 flexible membrane keypads, 69 × 76 mm with an adhesive back, and 5 compact adapters with pins, in a storage box
Heavy-duty 2-pack2 rigid keypads with raised keys and 8 pins soldered on, and 2 compact adapters with a socket
Two formsTK85 (v2): 22.4 × 30.4 mm, bus pins out of the bottom edge, address fixed. Compact adapter (v3): 20 × 20 mm, bus pins out of the side, 3 address pads
Adapter chipNXP PCF8574 I²C I/O expander: 8 port pins, P0–P7, one per keypad wire
Headers8-pin right-angle for the keypad, P7 to P0 left to right; 4-pin right-angle for your board: GND, VCC, SDA, SCL
Wires to your board4 — GND, VCC, and the 2 signal wires SDA and SCL
Supply voltage2.5–6 V for the chip. Use your board's own logic voltage, 3.3 V (3V3) or 5 V (5V): the I²C pull-ups go to VCC. No regulator
I2C address0x20, fixed on the TK85; 0x20 as shipped on the compact adapter, 0x20–0x27 with the three pads on its back
I2C clock100 kHz at most, per the PCF8574 datasheet
Pull-ups10 kΩ on SDA and on SCL, on as shipped, removed by cutting the I2C PULLUP pad on the back
Keys16 in a 4 × 4 grid, read one at a time

What it does

A 4×4 matrix keypad is sixteen switches wired as four row wires and four column wires: pressing a key joins one row to one column. That is eight wires, and wired straight to a board it is eight GPIO pins — nearly half an Arduino Uno.

The adapter moves those eight wires onto a PCF8574, a chip with eight pins of its own that is controlled over I²C. What is left on your board is the I²C bus: SDA and SCL, plus power and ground. Screens, sensors and clocks share the same two wires, each at its own address. The TK85 answers at 0x20.

Drag to turn it
The TK85: the keypad plugs onto the eight pins at the top, your board's four wires onto the pins at the bottom. Turn it over for the back.

The I2CKeyPad library by Rob Tillaart reads a key with two writes and two reads, and returns a number from 0 to 15. A keymap string turns that into the character printed on the key.

Two forms of the adapter

The adapter is built in two forms, and this page and its handbook lead with the first.

  • The TK85 (v2), in the TinkerBlock 46-piece kit: a TinkerBlock board, 22.4 × 30.4 mm, with a gold edge and two LEGO holes. The keypad header leaves the top edge and the four bus pins the bottom. Its address pins are wired to GND, so it is 0x20, fixed.
  • The compact adapter (v3), in the two keypad packs: a bare 20 mm square, its bus pins out of the side, and three address pads on the back that give it 0x20 to 0x27.

The chip, the keypad header and its pin order, the pull-ups, the library and the keymaps are the same on both. Two forms of the adapter lines them up.

TinkerBlock TK85 — front
Front
TinkerBlock TK85 — back
Back
TinkerBlock TK85 — side
Side

The boxes

The TK85 is photographed with a soft membrane keypad on its ribbon, the same kind as the 5-pack's. The two packs carry the compact adapter:

The soft keypad 5-pack: one large flexible membrane keypad with blue number keys and red A to D, star and hash keys, four smaller ones below it, five small black adapter boards with eight right-angle pins, and a teal box printed I2C MATRIX KEYPAD KIT.
The soft keypad 5-pack: five membrane keypads with ribbon tails, five compact adapters with pins, and the box.
The heavy-duty 2-pack: a large keypad in a black rigid frame with raised keys 1 to 9, 0, A to D, star and hash, plugged into an adapter below it, a second keypad and adapter to the right, and the teal I2C MATRIX KEYPAD KIT box.
The heavy-duty 2-pack: two rigid keypads with pins soldered on, and two compact adapters with a socket.

Which keymap

KeypadComes inKeymap
Soft membranethe 5-pack, and with the TK85 as photographedDCBA#9630852*741NF
Heavy-dutythe 2-packD#0*C987B654A321NF

The soft keypad brings its rows out first and the heavy-duty keypad its columns, so the same key lands on a different number. A keypad from anywhere else: run the keymap finder.

Wiring, in four lines

  1. GND to the board's GND.
  2. VCC to the board's own logic voltage: 5V on an Uno, 3V3 on an ESP32.
  3. SDA to the board's SDA: A4 on an Uno, GPIO 21 on an ESP32, GPIO 8 on an ESP32-S3.
  4. SCL to the board's SCL: A5, GPIO 22, GPIO 9.

VCC matters because the adapter's own 10 kΩ pull-ups tie SDA and SCL to it. Fed from 5 V next to an ESP32, the bus idles at 5 V against 3.3 V pins.

The TK85 wired to an Arduino Uno: 5V to VCC, GND to GND, A4 to SDA and A5 to SCL

Two on one bus

A TK85 is always 0x20, so two TK85s cannot share a bus: give the second its own — Wire1 on an ESP32 — or use a compact adapter for it. A compact adapter moves by bridging one of the three pads on its back with solder. Its pads printed A0 and A2 are wired the other way round: bridging the one marked A0 gives 0x24, and the one marked A2 gives 0x21. Scan after soldering and use what it prints.

Each adapter also brings a pair of 10 kΩ pull-ups, on as shipped. With more than a handful on one bus, cut the I2C PULLUP pad on all but one. More than one keypad works through both.

Example

Install I2CKeyPad from the Arduino Library Manager. The MicroPython version does the same two reads by hand and needs no library.

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

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

I2CKeyPad keyPad(0x20);   // fixed on the TK85; the compact adapter ships here
char last = 'N';

void setup() {
  Serial.begin(115200);
  Wire.begin();
  Wire.setClock(100000);   // the PCF8574 is rated for 100 kHz
  if (!keyPad.begin()) {
    Serial.println("No keypad at 0x20");
    while (true) delay(1000);
  }
  keyPad.loadKeyMap(keymap);
}

void loop() {
  char c = keyPad.getChar();
  if (c != last) {
    if (c != 'N' && c != 'F') Serial.println(c);
    last = c;
  }
  delay(20);
}
View on GitHub · blocks/tk85-matrix-keypad/arduino/matrix_keypad/matrix_keypad.ino @ v1.13

Where to start

The handbook below is twelve short articles with a working figure in each. If you only read one, read one adapter, two keymaps. If you want keys on the serial monitor now, the first keypress is the whole build. The board is also in 3D, every pin and part named.

When it doesn’t work

Nothing responds.
Scan the bus. A TK85 answers at 0x20, always; a compact adapter at 0x20 as shipped, or 0x20–0x27 if a pad on its back is bridged. If nothing answers anywhere, the fault is GND, VCC, SDA or SCL — on an Uno those last two are A4 and A5.
Every key prints a different key.
The sketch has the other keypad's keymap. The soft keypad needs "DCBA#9630852*741NF" and the heavy-duty keypad "D#0*C987B654A321NF". With the wrong one, 2 prints 4 and A prints *, while 1, 5, 9 and D look fine.
Two keys at once prints F.
That is the I2CKeyPad library refusing to guess. A second key pulls a second line low, and it returns FAIL rather than report a key that may not have been pressed. The keypad reads one key at a time.
Can I put two TK85s on one bus?
No. The TK85's address pins are wired to GND, so every TK85 is at 0x20. Put the second on another bus — Wire1 on an ESP32 or ESP32-S3 — or use a compact adapter from one of the packs, whose pads move it.
I bridged the A0 pad on a compact adapter and it is at 0x24, not 0x21.
The design files wire the pad printed A0 to the chip's A2 pin, and the pad printed A2 to A0; only A1 is as labelled. The address is still different from 0x20, which is what matters. Always scan after soldering and use the address it prints.
Can I power the adapter from 5 V on an ESP32?
No. The adapter's pull-ups tie SDA and SCL to its VCC, so from 5 V both lines sit at 5 V against 3.3 V pins. The chip runs from 2.5 V, so feed it 3V3.
An old sketch sets the I²C clock to 400 kHz.
Set it to 100 kHz. That is the PCF8574's rated maximum, and a key still reads in well under a millisecond.

The matrix keypad handbook

12 articles · about 55 minutes

This page is the reference: what the part is, what it is made of, and the questions people arrive already asking. The handbook is the walk — the same part in the order somebody actually meets it.

Why the adapter

2 articles

Sixteen keys need only eight wires if they are wired as a grid. The adapter turns those eight into the two an I²C bus already uses.

What is in your box

3 articles

Three boxes, one adapter circuit in two forms, two kinds of keypad. Which plugs into which, what differs between the TK85 and the compact adapter, and what every pin and pad is for.

How it reads a key

2 articles

Two I²C reads find any key, and the number they produce is not the character printed on it. The keymap is the translation, and there are two.

Limits and more keypads

2 articles

It reads one key at a time, by design. And it shares its bus: one fixed address on the TK85, eight on the compact adapter, and pull-up resistors that add up.

Two builds and a check

3 articles

The first keypress on an Uno or an ESP32, a four-digit code lock, and the sketch that finds the right keymap for a keypad you have never seen.

Lessons using TK85

Each one is a working build, not a snippet.

Edit this page — content/modules/matrix-keypad.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 →