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 name | I2CKeyPad |
|---|---|
| Author | Rob Tillaart |
| Include | #include <I2CKeyPad.h> |
| Boards | every board Mokxi compiles for but ATtiny85 |
| In the simulator | Simulated: it runs against the part the way it does on the bench. |
| Parts it drives | Keypad, 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.
- ESP32-C3
- Arduino Uno
- Arduino Nano
- Arduino Mega 2560
- Arduino Leonardo
- SparkFun Pro Micro
- Arduino Micro
- ESP32-C6
- WCH CH32V003
- Raspberry Pi Pico
- Raspberry Pi Pico W
- Seeed XIAO SAMD21
- STM32
- STM32 Nucleo-F411RE
- STM32 Blue Pill
- micro:bit V2
- Arduino Uno R4
- ESP32 DevKit
- ESP32-S3-DevKitC-1
- ESP8266 NodeMCU
- Arduino Pro Mini 3.3 V
- Seeed XIAO ESP32-C3
- ESP32-C3 SuperMini
- Seeed XIAO ESP32-C6
- ESP32-DevKitC V4
- LOLIN32 Lite
- Wemos D1 R32
- Cheap Yellow Display
- TTGO T-Display
- Seeed XIAO ESP32-S3
- Arduino Nano ESP32
- Arduino Nano RP2040 Connect
- Seeed XIAO RP2040
- Waveshare RP2040-Zero
- Arduino Uno R4 WiFi
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.
#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
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.