matrix keypad/Two builds and a check/11. When a key is wrong
Two builds and a check · 11 of 11

When a key is wrong

Three checks, in order: does the adapter answer, does every key produce a number, and is the keymap right. One sketch does all three — it finds the adapter on the bus, asks for each key in turn, and prints the keymap for whatever keypad is plugged in.

Check one: does it answer

Before any key, the board has to find the adapter. The sketch below calls every address the chip can have — 0x20 to 0x27, and 0x38 to 0x3F for the PCF8574A version — and stops at the first that answers. No answer means power, ground or a bus wire, never the keypad.

Check two: does every key make a number

With the adapter found, each key should produce one number from 0 to 15, and no two keys the same. A key that produces nothing is a wire that is not reaching the chip; four in a line is one of the eight wires.

Check three: is the map right

The sketch asks for each key in printed order and writes the character it asked for into the slot of the number it got. Run it here to see the map build.

Build the keymap by pressing every key once
0 / 16
Keypad on the bench
Keys pressed
0
Map so far
················NF
Matches the book
Each press fills one slot. The slot is the number the key produces; the character is the one printed on it. Run it for the other keypad and the same sixteen presses fill the slots in a different order.

On the soft keypad it prints DCBA#9630852*741NF; on the heavy-duty keypad, D#0*C987B654A321NF. A different string is not a fault — it is the right map for a keypad wired another way, a socket fitted upside down, or a keypad from somewhere else. Paste it into your sketch and the keys print as themselves.

The code

find_keymap.ino

Scans both PCF8574 address ranges and prints where it found the adapter. Then it asks for each key in printed order — 1, 2, 3, A, 4 and so on to D — and waits for that key to be pressed and released. After sixteen, it prints a keymap line ready to paste.

// Keymap finder: find the adapter, then press every key once in printed
// order, and it prints the keymap for your keypad.
//
// Wiring, adapter to board (as in "The first keypress"):
//   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. No special Tools settings. Serial Monitor at 115200.
// Unplug any I2C screen first, so the scan cannot mistake it for the keypad.

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

const char ORDER[] = "123A456B789C*0#D";   // the order it asks for keys

// PCF8574 answers at 0x20-0x27, PCF8574A at 0x38-0x3F.
uint8_t findAdapter() {
  for (uint8_t a = 0x20; a <= 0x3F; a++) {
    if (a == 0x28) a = 0x38;
    Wire.beginTransmission(a);
    if (Wire.endTransmission() == 0) return a;
  }
  return 0;
}

// Wait for exactly one key, then for it to be let go.
uint8_t waitForKey(I2CKeyPad &kp) {
  uint8_t k;
  do { k = kp.getKey(); delay(20); } while (k > 15);
  while (kp.getKey() != I2C_KEYPAD_NOKEY) delay(20);
  return k;
}

void setup() {
  Serial.begin(115200);
  delay(500);
  Wire.begin();
  Wire.setClock(100000);

  uint8_t addr = findAdapter();
  if (addr == 0) {
    Serial.println("No adapter at 0x20-0x27 or 0x38-0x3F. Check GND, VCC, SDA, SCL.");
    return;
  }
  Serial.print("Adapter at 0x");
  Serial.println(addr, HEX);

  I2CKeyPad kp(addr);
  kp.begin();

  char found[19] = "????????????????NF";
  for (int i = 0; i < 16; i++) {
    Serial.print("Press ");
    Serial.println(ORDER[i]);
    uint8_t k = waitForKey(kp);
    Serial.print("  key number ");
    Serial.println(k);
    found[k] = ORDER[i];
  }
  Serial.print("\nchar keymap[19] = \"");
  Serial.print(found);
  Serial.println("\";");
}

void loop() {}

A ? left in the printed map means one slot was never filled: a key was pressed twice, or the wrong key was pressed at a prompt. Run it again. If the map matches one of the two in this book, the keypad and adapter are both fine.

When it does not work

It says no adapter was found

Nothing answered in either range. Check GND and VCC with a meter first — an unpowered chip cannot answer — then SDA and SCL. On an Uno they are A4 and A5; on an ESP32, GPIO 21 and 22.

It hangs at one prompt no matter which key I press

Every key press is coming back as FAIL or not at all. If only some keys are dead, one keypad wire is not reaching its pin: reseat the socket. If every key is dead, a key may be stuck down, or two pins are bridged — the socket shifted by one pin does that.

Four keys in one row or column never register

One of the eight wires is open. On the soft keypad, look at the ribbon where it enters the socket and where it leaves the keypad; a sharp fold can crack a printed track. On the heavy-duty keypad, check that pin in the adapter's socket.

The address it prints is not 0x20

A pad on the back has been bridged, or the chip is a PCF8574A, which answers at 0x38 to 0x3F. Either is fine — put that address in your sketch.

Where this goes next

The keypad was the first device on the bus. Here is what changes when a screen, a sensor and a clock join it.

I²C on a shared bus

Edit this page — content/books/matrix-keypad/when-a-key-is-wrong.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