Try Embedded

Write a driver (PCF8574)

Wrap a chip in a small driver: a struct for its state, functions for its operations, and a callback for its events.

Firmware · Advanced

In a real product: Product firmware platform (Industrial): A clean driver hides the chip so the product can change it later.

Sketches that talk to a chip with raw Wire calls scattered through loop() work, until there are two of the chip, or the bus changes, or someone else has to read the code. A driver puts everything about the chip in one place, and gives the application a small vocabulary: set a pin, read a pin, tell me when an input changes.

What you'll learn

  • A driver's state in a struct, so two chips are two variables
  • Keeping a shadow copy of the output latch, so changing one pin does not disturb the others
  • An event callback through a function pointer, so the driver does not know what the application does
  • Separating the driver (no Serial, no pin numbers of the board) from the application

How it works

Pcf8574 holds the chip's address, the latch the driver last wrote, the inputs it last saw, and a function pointer onChange. pcf_begin writes all 1s, the PCF8574's input state. pcf_write changes one bit of the shadow latch and writes the whole byte, keeping the input pins at 1. pcf_poll reads the port, compares it with the last read, and calls onChange(pin, level) for each input that changed.

The application only says what should happen: buttonChanged lights LED 0 while button 0 is held, and toggles LED 1 on each press of button 1. It never sees an I²C byte.

The driver polls when the chip's INT line (on pin 2) goes low, so the bus is quiet while nothing happens.

Try this

  • Add a second PCF8574 at 0x21 (A0 to 5V): declare another Pcf8574 and poll both. The driver code does not change.
  • Make pcf_write return an error when the chip does not acknowledge, and have the application report it.
  • Replace the callback with an event queue: what does the application gain, and what does it cost?

sketch.ino

// A small PCF8574 driver: state in a struct, a shadow latch, and a callback for input changes. LEDs sink current on
// P0 and P1; buttons on P4 and P5; INT on pin 2.
#include <Wire.h>

// ---- driver: knows the chip, not the application ----
struct Pcf8574 {
  uint8_t address;
  uint8_t latch;     // what was last written: 1 = input or high
  uint8_t inputs;    // the port as last read
  uint8_t inputMask; // the pins used as inputs
  void (*onChange)(uint8_t pin, bool level);
};

bool pcf_send(Pcf8574 &chip) {
  Wire.beginTransmission(chip.address);
  Wire.write(chip.latch | chip.inputMask);  // inputs must stay 1
  return Wire.endTransmission() == 0;
}

bool pcf_begin(Pcf8574 &chip) {
  chip.latch = 0xFF;
  chip.inputs = 0xFF;
  return pcf_send(chip);
}

bool pcf_write(Pcf8574 &chip, uint8_t pin, bool level) {
  if (level) chip.latch |= 1 << pin; else chip.latch &= ~(1 << pin);
  return pcf_send(chip);
}

void pcf_poll(Pcf8574 &chip) {
  if (Wire.requestFrom(chip.address, (uint8_t)1) != 1) return;
  uint8_t now = Wire.read();
  uint8_t changed = (now ^ chip.inputs) & chip.inputMask;
  chip.inputs = now;
  for (uint8_t pin = 0; pin < 8; pin++) {
    if ((changed & (1 << pin)) && chip.onChange) chip.onChange(pin, now & (1 << pin));
  }
}

// ---- application: knows what the buttons mean, not the chip ----
void buttonChanged(uint8_t pin, bool level);
Pcf8574 panel = { 0x20, 0xFF, 0xFF, 0x30, buttonChanged };  // inputs on P4 and P5
bool led1 = false;

void buttonChanged(uint8_t pin, bool level) {
  bool pressed = !level;  // buttons pull to GND
  Serial.print("button ");
  Serial.print(pin - 4);
  Serial.println(pressed ? " pressed" : " released");
  if (pin == 4) pcf_write(panel, 0, !pressed);      // LED 0 on while held (an LED is on at 0)
  if (pin == 5 && pressed) {
    led1 = !led1;
    pcf_write(panel, 1, !led1);                     // LED 1 toggles on each press
  }
}

void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT_PULLUP);
  Wire.begin();
  Serial.println(pcf_begin(panel) ? "PCF8574 ready" : "No PCF8574 at 0x20");
}

void loop() {
  if (digitalRead(2) == LOW) pcf_poll(panel);
}

Lessons behind it