matrix keypad/Why the adapter/02. Eight pins down to two
Why the adapter · 02 of 11

Eight pins down to two

Wired straight to a board, a 4×4 keypad takes eight pins — nearly half of an Arduino Uno. Through the adapter it takes the two I²C pins, and shares them with every other I²C part on the board.

Eight pins is a lot

An Arduino Uno has eighteen pins a sketch can use: D2 to D13 and A0 to A5. D0 and D1 are the USB serial port. A keypad wired directly takes eight of the eighteen, and it never gives them back.

Where the pins go
10 of 18 used
The keypad is wired
Other I²C parts on the board1
Pins used
10
Pins left
8
Used by the keypad
8
Eight pins for one keypad. That is nearly half an Uno, and the first I²C part you add takes A4 and A5 on top: 10 of 18 gone before a single motor or LED is wired.

What the adapter is

The chip on the adapter is a PCF8574. It has eight pins of its own, P0 to P7, and it is controlled over I²C: the board sends it a byte to set the eight pins, and asks it for a byte to find out what they are doing. The keypad's eight wires plug into those eight pins.

So the keypad moves off your board's pins and onto the chip's. What is left on your board is the I²C bus: two signal wires, SDA and SCL, plus power and ground.

Why it shares

I²C was designed for many devices on the same two wires. Each one has an address, and it only answers when the board calls that address. The adapter answers at 0x20 as it ships. A screen, a clock or a sensor on the same bus has its own address and ignores the keypad's traffic.

That is why adding the second, third and fourth I²C part in the figure costs nothing: they all hang off A4 and A5.

The price is small. The board needs a library to talk to the chip, and reading the keypad takes a few I²C messages instead of a few pin reads — still well under a millisecond.

When it does not work

My old sketch uses the Keypad library on eight pins. Will it work here?

No. That library drives the eight wires itself, and here the eight wires belong to the adapter's chip. Use I2CKeyPad by Rob Tillaart, which talks to the chip over I²C; the first-keypress article has the whole sketch.

Can I use the keypad and an I²C screen at the same time?

Yes, on the same SDA and SCL wires. Every I²C device has its own address. The adapter ships at 0x20, and the common LCD backpacks answer at 0x27 or 0x3F, so they do not collide.

Is it slower than wiring the keypad directly?

Slower, and it does not matter. Reading a key takes a few I²C transactions at 100 kHz, well under a millisecond. A finger stays on a key for tens of milliseconds.

Where this goes next

The adapter comes in three boxes, with two different keypads. Which plugs into which.

Three boxes, one adapter

Edit this page — content/books/matrix-keypad/eight-pins-down-to-two.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