ESP32 sixteen-input keypad on an expander
Build it step by step
0 of 7 donePower the chip
VDD to 3V3, VSS to GND, RESET to 3V3.
Set the address
A0, A1, A2 to GND.
Wire the bus
SDA to GPIO21, SCL to GPIO22.
Wire eight buttons
GPA0 to GPA7, other side to GND.
Upload the sketch
Open the serial monitor at 115200.
Press a button
The key number prints.
Release it
A released message prints.
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
- Development BoardsBuyESP32 DevKit V1 × 1
- Integrated CircuitsBuy
- SwitchesBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| MCP23017 VDD (pin 9) | ESP32 3V3 | Power at 3.3 V |
| MCP23017 VSS (pin 10) | ESP32 GND | Ground |
| MCP23017 RESET (pin 18) | ESP32 3V3 | Must be held high or the chip stays in reset |
| MCP23017 A0, A1, A2 (pins 15, 16, 17) | ESP32 GND | Address 0x20 |
| MCP23017 SDA (pin 13) and SCL (pin 12) | ESP32 GPIO21 and GPIO22 | I2C data and clock |
| Eight buttons | MCP23017 GPA0 to GPA7, other side of each to GND | Keys 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.
| Check | Expected |
|---|---|
| Press GPA0 button | Key 0 pressed |
| Release it | Key 0 released |
| Press two buttons together | Both reported |
| Move a wire to GPB3 | Key 11 appears |
| No keys pressed | No 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 →
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.
