Projects / ESP32 sixteen-input keypad on an expander

ESP32 sixteen-input keypad on an expander

Intermediate2.5 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. ESP32

    Power the chip

    VDD to 3V3, VSS to GND, RESET to 3V3.

  2. ESP32

    Set the address

    A0, A1, A2 to GND.

  3. ESP32

    Wire the bus

    SDA to GPIO21, SCL to GPIO22.

  4. ESP32

    Wire eight buttons

    GPA0 to GPA7, other side to GND.

  5. ESP32

    Upload the sketch

    Open the serial monitor at 115200.

  6. ESP32

    Press a button

    The key number prints.

  7. ESP32

    Release it

    A released message prints.

In short

How do you build an ESP32 sixteen-input keypad on an expander?

An ESP32 reads up to sixteen push buttons through an MCP23017 over I2C using only two pins, and prints which key was pressed or released. You need ESP32 DevKit V1, MCP23017 16-bit I/O expander, Tactile push button 6 mm × 8 and Breadboard 830 Tie Points. It takes 7 steps and about 150 minutes.

Level
Intermediate
Time
About 150 min
Parts
4

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
MCP23017 VDD (pin 9)ESP32 3V3Power at 3.3 V
MCP23017 VSS (pin 10)ESP32 GNDGround
MCP23017 RESET (pin 18)ESP32 3V3Must be held high or the chip stays in reset
MCP23017 A0, A1, A2 (pins 15, 16, 17)ESP32 GNDAddress 0x20
MCP23017 SDA (pin 13) and SCL (pin 12)ESP32 GPIO21 and GPIO22I2C data and clock
Eight buttonsMCP23017 GPA0 to GPA7, other side of each to GNDKeys 0 to 7 (GPB0 to GPB7 are keys 8 to 15)

Code

The MCP23017 is an I2C expander with sixteen pins in two ports of eight. Each pin can be an input with an optional pull-up resistor inside the chip, so a button can go straight from the pin to ground with no extra parts. The program sets all the pins to inputs with pull-ups on, then reads both ports in one I2C message, which gives a sixteen-bit number where a zero means a pressed button. It compares each reading with the last, and when a bit changes it waits twenty milliseconds, reads again to skip contact bounce and prints the key number with pressed or released. The chip's RESET pin must be tied to 3.3 V, otherwise it never wakes up, which is a common mistake. Its three address pins set the address from 0x20 to 0x27, so up to eight chips can share the two wires, giving 128 inputs.

Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <Wire.h>

const uint8_t ADDR = 0x20;
const uint8_t IODIRA = 0x00, IODIRB = 0x01;   // direction: 1 means input
const uint8_t GPPUA = 0x0C, GPPUB = 0x0D;     // pull-up resistors
const uint8_t GPIOA = 0x12, GPIOB = 0x13;     // pin states

uint16_t lastState = 0xFFFF;                  // 1 means not pressed (pull-ups hold pins high)

void writeReg(uint8_t reg, uint8_t value) {
  Wire.beginTransmission(ADDR);
  Wire.write(reg);
  Wire.write(value);
  Wire.endTransmission();
}

uint16_t readAll() {
  Wire.beginTransmission(ADDR);
  Wire.write(GPIOA);
  Wire.endTransmission(false);
  Wire.requestFrom(ADDR, (uint8_t)2);         // port A then port B (the chip steps on by itself)
  uint8_t a = Wire.read();
  uint8_t b = Wire.read();
  return ((uint16_t)b << 8) | a;
}

void setup() {
  Serial.begin(115200);
  Wire.begin(21, 22);
  writeReg(IODIRA, 0xFF);                     // all pins inputs
  writeReg(IODIRB, 0xFF);
  writeReg(GPPUA, 0xFF);                      // all pull-ups on
  writeReg(GPPUB, 0xFF);
  Serial.println("Keypad ready");
}

void loop() {
  uint16_t now = readAll();
  uint16_t changed = now ^ lastState;
  if (changed) {
    delay(20);                                // simple debounce
    now = readAll();
    changed = now ^ lastState;
    for (int i = 0; i < 16; i++) {
      if (changed & (1 << i)) {
        Serial.print("Key "); Serial.print(i);
        Serial.println((now & (1 << i)) ? " released" : " pressed");
      }
    }
    lastState = now;
  }
  delay(5);
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual ESP32 Dev Module with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

Keypad ready
Key 0 pressed
Key 1 pressed
Key 2 pressed
Key 3 pressed
Key 4 pressed

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Press GPA0 buttonKey 0 pressed
Release itKey 0 released
Press two buttons togetherBoth reported
Move a wire to GPB3Key 11 appears
No keys pressedNo output

If it does not work

No reaction at all

Check RESET is tied to 3V3 and the address pins.

Keys reported twice

Increase the debounce delay.

Wrong key numbers

Remember port A is keys 0 to 7 and port B is keys 8 to 15.

Compile error

Select ESP32 Dev Module.

Constant presses

A floating pin, check the button goes to GND.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the ESP32 sixteen-input keypad on an expander?

ESP32 DevKit V1, MCP23017 16-bit I/O expander, Tactile push button 6 mm × 8 and Breadboard 830 Tie Points.

How long does the ESP32 sixteen-input keypad on an expander take to build?

About 150 minutes for a intermediate maker, in 7 steps.

How do I know the ESP32 sixteen-input keypad on an expander works?

Press GPA0 button: Key 0 pressed. Release it: Key 0 released. Press two buttons together: Both reported.

No reaction at all: what should I check?

Check RESET is tied to 3V3 and the address pins.

Keys reported twice: what should I check?

Increase the debounce delay.

Wrong key numbers: what should I check?

Remember port A is keys 0 to 7 and port B is keys 8 to 15.

Compile error: what should I check?

Select ESP32 Dev Module.