Skip to main content

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);
  }
}
PCF8574Module
constructor
Create PCF8574 instance
begin
bool
Initialize PCF8574
setupPin
void
Configure pin mode
digitalWrite
bool
Write digital value to pin
digitalRead
uint8_t
Read digital value from pin
digitalReadAll
byte
Read all 8 pins as byte
digitalWriteAll
bool
Write all 8 pins from byte
setupButton
void
Configure pin as button input
readButton
bool
Read button state
setupLED
void
Configure pin as LED output
turnOnLED
bool
Turn LED on
turnOffLED
bool
Turn LED off
toggleLED
bool
Toggle LED state
setupEncoder
void
Configure two pins for rotary encoder
readEncoder
bool
Read rotary encoder value

Digital Input

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());
}
DigitalIn
constructor
Create digital input
setDebounceTime
void
Set debounce delay
update
void
Update input state (call in loop)
getState
int
Get debounced state
isPressed
bool
Check if button is currently pressed
isLongPressed
bool
Check for long press
isReleased
bool
Check if button was just released
setCountMode
void
Set event counting mode
getCount
unsigned long
Get event count
resetCount
void
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
}
DigitalOut
constructor
Create digital output
init
void
Initialize output pin
on
void
Turn output on immediately
off
void
Turn output off immediately
set
void
Set output state
toggle
void
Toggle output state
toggleAsync
void
Toggle at interval (non-blocking)
onDelay
void
Turn on after delay
offDelay
void
Turn off after delay
update
void
Update timing (call in loop for delays)
getState
bool
Get current output state

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);
  }
}
RotaryModule
constructor
Create rotary encoder instance
init
void
Initialize encoder
readPosition
void
Read encoder position (call in ISR)
getPosition
int
Get current position
getDirection
int
Get rotation direction
isMoveCW
bool
Check for clockwise rotation
isMoveCCW
bool
Check for counter-clockwise rotation
isStop
bool
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

Platform Support

ESP32

Full support with custom I2C pins

ESP8266

Full support

AVR

Full support

Build docs developers (and LLMs) love