Overview
The I/O module provides digital input/output expansion, rotary encoder support, and PCF8574 I2C expander integration.
PCF8574 I/O Expander
PCF8574Module
Comprehensive wrapper for PCF8574 I2C I/O expander with 8 GPIO pins.
#define ENABLE_MODULE_PCF8574
#include "Kinematrix.h"
PCF8574Module pcf(0x27); // I2C address 0x27
void setup() {
pcf.begin();
// Setup pins
pcf.setupLED(PCF_PIN0, LOW);
pcf.setupButton(PCF_PIN1);
// Control
pcf.turnOnLED(PCF_PIN0);
}
void loop() {
if (pcf.readButton(PCF_PIN1)) {
pcf.toggleLED(PCF_PIN0);
}
}
Create PCF8574 instance
address, interruptPin, interruptFunction
uint8_t, uint8_t, void (*)()
With interrupt support
Initialize PCF8574
Returns true if initialization successful
Configure pin mode
Pin number (P0-P7 or PCF_PIN0-PCF_PIN7)
PCF_INPUT (0), PCF_INPUT_PULLUP (1), or PCF_OUTPUT (2)
Initial state for output pins
Write digital value to pin
Read digital value from pin
Returns HIGH or LOW
Read all 8 pins as byte
Returns byte with pin states (bit 0 = P0, bit 7 = P7)
Write all 8 pins from byte
Configure pin as button input
Read button state
Returns true if button pressed
Configure pin as LED output
Configure two pins for rotary encoder
Read rotary encoder value
Pointer to encoder value variable
Reverse rotation direction
DigitalIn
Debounced digital input with long-press detection and event counting.
#define ENABLE_MODULE_DIGITAL_INPUT
#include "Kinematrix.h"
DigitalIn button(2, INPUT_PULLUP); // Pin 2 with pullup
void setup() {
button.setDebounceTime(50); // 50ms debounce
button.setCountMode(COUNT_FALLING);
}
void loop() {
button.update();
if (button.isPressed()) {
Serial.println("Button pressed!");
}
if (button.isLongPressed(2000)) {
Serial.println("Long press detected");
}
Serial.printf("Press count: %lu\n", button.getCount());
}
Create digital input
mode
int
default:"INPUT_PULLUP"
Pin mode (INPUT or INPUT_PULLUP)
Set debounce delay
Debounce time in milliseconds
Update input state (call in loop)
Get debounced state
Returns HIGH or LOW
Check if button is currently pressed
Check if pressed for duration
Check for long press
Long press duration in ms
Check if button was just released
Set event counting mode
COUNT_FALLING (0), COUNT_RISING (1), or COUNT_BOTH (2)
Get event count
Returns number of events detected
Reset event counter to zero
Digital Output
DigitalOut
Digital output with timing delays, toggle, and state management.
#define ENABLE_MODULE_DIGITAL_OUTPUT
#include "Kinematrix.h"
DigitalOut led(13); // Pin 13
DigitalOut relay(5, true); // Pin 5, reversed logic
void setup() {
led.init();
relay.init();
}
void loop() {
// Simple toggle every 500ms
led.toggleAsync(500);
// Turn on with delay
relay.onDelay(1000); // Delay 1 second before turning on
relay.update(); // Must call in loop
}
Create digital output
Reverse logic (true = active LOW)
Turn output on immediately
Turn output off immediately
Set output state
True for on, false for off
Toggle output state
toggleCallback
void (*)()
default:"nullptr"
Optional callback on toggle
Toggle at interval (non-blocking)
delay_time
unsigned long
default:"100"
Toggle interval in ms
toggleCallback
void (*)(bool)
default:"nullptr"
Optional callback with state
Update timing (call in loop for delays)
Get current output state
Returns true if on, false if off
Rotary Encoder
RotaryModule
Rotary encoder with direction detection.
#define ENABLE_MODULE_ROTARY_ENCODER
#include "Kinematrix.h"
RotaryModule encoder(2, 3); // CLK pin 2, DT pin 3
int counter = 0;
void encoderISR() {
encoder.readPosition();
}
void setup() {
encoder.init(encoderISR);
}
void loop() {
if (encoder.isMoveCW()) {
counter++;
Serial.println(counter);
}
if (encoder.isMoveCCW()) {
counter--;
Serial.println(counter);
}
}
Create rotary encoder instance
Initialize encoder
pin_callback
void (*)()
default:"nullptr"
Interrupt callback function
Read encoder position (call in ISR)
Get current position
Returns position value
Get rotation direction
Returns 1 for CW, -1 for CCW, 0 for stopped
Check for clockwise rotation
Check for counter-clockwise rotation
Check if encoder is stopped
PCF8574 Pin Definitions
#define PCF_PIN0 P0 // Pin 0
#define PCF_PIN1 P1 // Pin 1
#define PCF_PIN2 P2 // Pin 2
#define PCF_PIN3 P3 // Pin 3
#define PCF_PIN4 P4 // Pin 4
#define PCF_PIN5 P5 // Pin 5
#define PCF_PIN6 P6 // Pin 6
#define PCF_PIN7 P7 // Pin 7
ESP32
Full support with custom I2C pins