A matrix keypad is a grid of buttons (like on a phone or calculator) that saves wiring by using rows and columns instead of separate wires for each key.
### How It Works Simply:
- **Setup**: Keys are arranged in rows (horizontal) and columns (vertical). For a 4x4 keypad (16 keys), you only need 8 wires: 4 for rows, 4 for columns.
- **Press Detection**: A controller (like Arduino) scans by sending a signal down one row at a time and checking columns for a connection.
- Press a key? It connects its row and column, so the controller detects which one.
It's like a crossword puzzle: rows and columns intersect at keys, and scanning finds the "word" (pressed key). For projects, plug it in and use simple code to read inputs!



### Lonely Binary Matrix Keypad I2C Adapter
Usually, a standard 4x4 matrix keypad requires 8 GPIO pins. We provide an I2C adapter, which means you can connect this keypad to the I2C bus using only two GPIO pins. Since it uses the I2C bus, you can add more sensors to the same bus. What a space saver!
The Lonely Binary Matrix Keypad I2C adapter includes built-in pull-up resistors for both SDA and SCL lines, as required by the I2C bus. If you add more sensors that also have their own built-in pull-up resistors, you can easily disable the keypad's pull-ups by cutting the I2C PULL-UP pad on the back.
You can also change the I2C address by shorting the A0, A1, and/or A2 pads on the back.


### I2C Address Scan
``` cpp
#include <Wire.h>
void setup()
{
Serial.begin(9600);
while (!Serial); // Leonardo: wait for serial monitor
Serial.println("\nI2C Scanner");
}
void loop()
{
byte error, address;
int nDevices;
Serial.print("SDA: ");
Serial.print(SDA);
Serial.print(", SCL: ");
Serial.println(SCL);
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}
```

### Sample Code
#### Option 1: Using Arduino Library Manager (Easiest)
1. Open the Arduino IDE.
2. Go to **Sketch > Include Library > Manage Libraries**.
3. Search for "I2CKeyPad" (by Rob Tillaart).
4. Click **Install** on the result.
5. Restart the IDE if prompted.
#### Option 2: Manual Install from GitHub
1. Go to [https://github.com/RobTillaart/I2CKeyPad](https://github.com/RobTillaart/I2CKeyPad) and download the ZIP file (click **Code > Download ZIP**).
2. In Arduino IDE, go to **Sketch > Include Library > Add .ZIP Library**.
3. Select the downloaded ZIP file.
4. Restart the IDE.
Due to differences in the hardware layout between the Soft and Heavy Duty matrix keypads, you'll need to adjust the keymap at the start of your code.
- For the Soft keypad: char keymap[19] = "DCBA#9630852*741NF";
- For the Heavy Duty keypad: char keymap[19] = "D#0*C987B654A321NF";
``` cpp
#include "Wire.h"
#include "I2CKeyPad.h"
const uint8_t KEYPAD_ADDRESS = 0x20;
I2CKeyPad keyPad(KEYPAD_ADDRESS);
//Heavy Duty Keypad
char keymap[19] = "D#0*C987B654A321NF"; // N = NoKey, F = Fail
//Soft Keypad
//char keymap[19] = "DCBA#9630852*741NF"; // N = NoKey, F = Fail
void setup()
{
delay(2000);
Serial.begin(9600);
Serial.println();
Serial.println(__FILE__);
Serial.print("I2C_KEYPAD_LIB_VERSION: ");
Serial.println(I2C_KEYPAD_LIB_VERSION);
Serial.println();
Wire.begin(SDA,SCL);
Wire.setClock(400000);
if (keyPad.begin() == false)
{
Serial.println("\nERROR: cannot communicate to keypad.\nPlease reboot.\n");
while (1);
}
keyPad.loadKeyMap(keymap);
}
void loop()
{
if (keyPad.isPressed())
{
char ch = keyPad.getChar(); // note we want the translated char
int key = keyPad.getLastKey();
Serial.print(key);
Serial.print(" \t");
Serial.println(ch);
delay(100);
}
}
```
