Arduino library · Input

I2CKeyPad: an example that runs

Yes. You can use the I2CKeyPad library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Keypad, 4x4). The example below, "A 4x4 keypad through a PCF8574", runs on the Arduino Uno R3.

Built in on Mokxi: #include <I2CKeyPad.h> and it builds, with nothing to install. Free, and no account needed.

What I2CKeyPad does

A 4x4 (or 5x3, 6x2, 8x1) matrix keypad scanned through a PCF8574 on two wires, with a key map, debounce threshold and the no-key and fail codes. That is what I2CKeyPad, by Rob Tillaart, covers. The sketch below uses it on the Arduino Uno: “A 4x4 keypad through a PCF8574”.

Rob Tillaart's I2CKeyPad library (0.5.x), a matrix keypad of up to 4x4 (or 5x3, 6x2, 8x1) scanned through a PCF8574 I/O expander.

No key is I2C_KEYPAD_NOKEY (16); two keys, or a failed read, are I2C_KEYPAD_FAIL (17); a call within the debounce threshold of the last good one returns I2C_KEYPAD_THRESHOLD.

The calls follow release 0.5.2 of the library, so a sketch written here builds on a real board too. The example is the one Mokxi's own tests build and run, so what is printed here is what runs.

Library Manager nameI2CKeyPad
AuthorRob Tillaart
Include#include <I2CKeyPad.h>
Boardsevery board Mokxi compiles for but ATtiny85
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesKeypad, 4x4, I/O expander, PCF8574

Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <I2CKeyPad.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "I2CKeyPad" by Rob Tillaart from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add I2CKeyPad to lib_deps in PlatformIO. Mokxi follows the calls of release 0.5.2, so a sketch written here builds there too.

The calls you will use

In the order a sketch meets them.

I2CKeyPad keyPad(KEYPAD_ADDRESS)

Makes keyPad, the I2CKeyPad object the calls below are made on.

keyPad.begin()

Starts it up and returns true if it started, false if it did not. Call it once in setup().

keyPad.loadKeyMap(keymap)

Loads the key map. Arguments: key map.

keyPad.isPressed()

Returns true or false for "is pressed".

keyPad.getChar()

Returns the char as a whole number.

keyPad.getLastKey()

Returns the last key as a whole number.

Example: A 4x4 keypad through a PCF8574

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#include "Wire.h"
#include "I2CKeyPad.h"

const uint8_t KEYPAD_ADDRESS = 0x20;

I2CKeyPad keyPad(KEYPAD_ADDRESS);

char keymap[19] = "123A456B789C*0#DNF";  // N = NoKey, F = Fail

void setup() {
  Serial.begin(115200);
  Serial.print("I2C_KEYPAD_LIB_VERSION: ");
  Serial.println(I2C_KEYPAD_LIB_VERSION);

  Wire.begin();
  Wire.setClock(400000);
  if (keyPad.begin() == false) {
    Serial.println("ERROR: cannot communicate to keypad.");
    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(" 	");
    Serial.println(ch);
    delay(100);
  }
}

Wiring

  • Arduino Uno R3 A4 to I/O expander, PCF8574 SDA
  • Arduino Uno R3 A5 to I/O expander, PCF8574 SCL
  • Arduino Uno R3 5V to I/O expander, PCF8574 VCC
  • Arduino Uno R3 GND to I/O expander, PCF8574 GND
  • I/O expander, PCF8574 P0 to Keypad, 4x4 R1
  • I/O expander, PCF8574 P1 to Keypad, 4x4 R2
  • I/O expander, PCF8574 P2 to Keypad, 4x4 R3
  • I/O expander, PCF8574 P3 to Keypad, 4x4 R4
  • I/O expander, PCF8574 P4 to Keypad, 4x4 C1
  • I/O expander, PCF8574 P5 to Keypad, 4x4 C2
  • I/O expander, PCF8574 P6 to Keypad, 4x4 C3
  • I/O expander, PCF8574 P7 to Keypad, 4x4 C4

Open it in the editor, wired and ready to run

Projects that use I2CKeyPad

Built-in projects that include it. Each page has the full sketch and opens in the editor, already wired.

Try I2CKeyPad without installing anything

Open the example, change the sketch, and keep your version in a free account.