Two reads per key
The I2CKeyPad library finds a pressed key with two writes and two reads: one half of the chip's pins pulls low while the other half listens, then the halves swap. The two answers make a number from 0 to 15.
What a PCF8574 pin can do
Each of the chip's eight pins does one of two things, set by one bit of the byte your board last wrote.
A 0 pulls the pin hard to 0 V. TI's datasheet guarantees at least 10 mA of pull at 5 V, which nothing on a keypad can argue with.
A 1 only holds the pin up weakly — a current source of 30 to 310 µA. It reads as 1 unless something pulls it down, and a pressed key joined to a pin set to 0 does exactly that. So a pin set to 1 is also an input. The datasheet calls this quasi-bidirectional: no direction to set, just 0 or a weak 1.
Two reads
Hold down a key, then run it. The drawing is the soft keypad's wiring, with rows on P7 to P4 and columns on P3 to P0.
Idle. Every port pin is weakly high, and the key is just a bridge between a row wire and a column wire.
- Write 0xF0. The top four pins are weak 1s and the bottom four are hard 0s. Every column is pulled low; every row is listening.
- Read. The pressed key joins its row to a column at 0 V, so exactly one of the top four bits comes back 0. Which one is a number from 0 to 3.
- Write 0x0F, the other way round: rows at 0 V, columns listening.
- Read. One of the bottom four bits comes back 0. Another number, 0 to 3.
The key number is the first answer plus four times the second, 0 to 15. Four I²C transactions at 100 kHz take well under a millisecond.
The older way to read a keypad scans it: pull one column low, read the rows, move to the next column, four passes in all. The two-read method needs no loop and gets the same answer, because the chip lets any pin be a hard 0 or a weak 1 without being told which direction it is.
The number is not the character
getKey() returns that number. Nothing in it says the key had a 7 printed on it: it says which two pins the key joined. Turning 0–15 into a character is a lookup table, and the table depends on how the keypad is wired — which is the next article.
Two other answers are possible. 16 means no key: nothing went low. 17 means FAIL: a read matched none of the four one-key patterns, because two keys are down or because the chip did not answer at all.
When it does not work
17 is FAIL, and with no key down it means the chip did not answer: the library reads 0xFF from a missing device, which matches none of its patterns. Check the four wires and the address. A bus scan settles it in seconds.
Change it to 100000. The PCF8574 is rated for a 100 kHz clock, and 400 kHz is four times that. It may work on your bench and fail on a longer wire; at 100 kHz a key still reads in well under a millisecond.
The sketch reads the keypad many times a second and the key is still down every time. Print only when the answer changes, as the first-keypress sketch does.
16 is NOKEY: both reads came back with no line pulled low. That is the normal answer between presses.
The number is a position, not a character. The keymap turns one into the other, and the two keypads need different ones.
One adapter, two keymaps →Edit this page — content/books/matrix-keypad/two-reads-per-key.mdx
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.