How-to · Beginner · 3 min read
4x4 Matrix Keypad with Arduino
Wiring and Code
By Geekobot Learning Team · Updated 24 Oct 2026
On this page
A 4x4 matrix keypad arranges 16 buttons in four rows and four columns, so it needs only 8 pins instead of 16. The microcontroller drives one row LOW at a time and reads which column goes LOW, which identifies the key. The Keypad library does this scanning for you. To save pins, use a PCF8574 I2C expander or a resistor ladder on one analog pin.
Key specs at a glance
- Buttons
- 16 (4x4) or 12 (3x4)
- Pins
- 8 (4 rows + 4 columns); 7 for 3x4
- Type
- Membrane (flat, adhesive) or hard plastic keys
- Voltage
- Passive switches; works at 3.3 V or 5 V
- Library
- Keypad (Mark Stanley and Alexander Brevig)
- Pin saving
- PCF8574 I2C expander, or a resistor ladder to one ADC pin
Wiring
Looking at the front of a typical membrane keypad, the 8 pins from left to right are rows 1 to 4 then columns 1 to 4. On an Uno, connect them to D9 to D2. Pin orders vary, so if keys come out wrong, check with a multimeter in continuity mode while pressing keys. On an ESP32, avoid strapping and input-only pins for the row outputs.
Code
`` #include <Keypad.h> const byte R=4, C=4; char keys[R][C]={{'1','2','3','A'},{'4','5','6','B'},{'7','8','9','C'},{'*','0','#','D'}}; byte rowPins[R]={9,8,7,6}, colPins[C]={5,4,3,2}; Keypad kp=makeKeypad(makeKeymap(keys),rowPins,colPins,R,C); void setup(){Serial.begin(9600);} void loop(){char k=kp.getKey(); if(k) Serial.println(k);} ``
The library debounces keys and handles the scanning. For a password lock, collect keys into a string, compare when # is pressed, clear on *, and switch a relay or solenoid lock. Show entered digits as * on an LCD.
Saving pins
a PCF8574 board drives all 8 lines over SDA and SCL, sharing the bus with an I2C LCD. Use the Keypad_I2C library.
wire different resistors so each key gives a different voltage on one analog pin. Cheap, but sensitive to resistor tolerance and only one key at a time.
pressing three keys at the corners of a rectangle can make a fourth appear pressed. Add a diode per key if you need reliable multi-key presses.
Read a 4x4 keypad with Arduino
You need: Arduino Uno, 4x4 membrane keypad, 8 male-to-male jumper wires · Tools: Computer with Arduino IDE
Get the parts3 of 3 found in our store · ₹807
Arduino UnoArduino Uno R3 Development Board with USB Cable₹385
4x4 membrane keypadMembrane Matrix Keypad₹30
8 male-to-male jumper wiresDupont Jumper Wires (40 pcs)₹49
- 1Identify the pinsNote which 4 pins are rows and which 4 are columns, usually rows first from the left.
- 2Wire the keypadConnect rows to D9 to D6 and columns to D5 to D2.
- 3Install the libraryInstall the Keypad library from the Library Manager.
- 4Upload the sketchUpload the example with your key map and pin lists.
- 5Test each keyOpen the Serial Monitor and press each key; fix the pin order if characters are swapped.
Common mistakes to avoid
- Getting rows and columns swapped, so keys map wrongly.
- Using ESP32 input-only pins as row outputs.
- Peeling and re-sticking the membrane, damaging the tail.
- Expecting reliable multi-key presses without diodes.
- Leaving a password in plain text where it is printed to Serial.
Frequently asked questions
How many pins does a 4x4 keypad need?
Eight: four rows and four columns. A PCF8574 I2C expander reduces that to two.
Why are my keypad keys printing the wrong characters?
The row or column pins are in a different order. Swap the pin arrays in code until the map matches.
Does a keypad need resistors?
No. The library uses the microcontroller's internal pull-ups on the columns.
Can I use a keypad with ESP32?
Yes, at 3.3 V. Choose GPIOs that support output for rows and avoid strapping pins.
Can I connect a keypad to one analog pin?
Yes, with a resistor ladder, but it handles only one key at a time and needs careful calibration.
Parts for this guide
Terms in this guide
Projects that use it
Put this into practice: 11 step-by-step builds in our project library use parts from this guide.
Keep learning
Sources
Written by the Geekobot team from manufacturer datasheets. Always check the datasheet for the exact part and variant you buy.


