Using the PCF8574 with Arduino involves wiring up the sensor to your Arduino-compatible microcontroller, installing the Adafruit PCF8574 library and running the provided example code.

Wiring

Wire as shown for a 5V board like an Uno. If you are using a 3V board, like an Adafruit Feather, wire the board's 3V pin to the PCF8574 VIN.

These wiring diagrams include a button and an LED, which are necessary for the example below.

Here is an Adafruit Metro wired up to the PCF8574 using the STEMMA QT connector:

Metro to expander:

Use a STEMMA QT to male header pin cable.

  • Metro 5V to expander VIN (red wire)
  • Metro GND to expander GND (black wire)
  • Metro SCL to expander SCL (yellow wire)
  • Metro SDA to expander SDA (blue wire)
  • Metro 2 to expander INT (green wire)

Please follow the steps below for LED and button wiring.

Here is an Adafruit Metro wired up using a solderless breadboard:

Metro to expander:

  • Metro 5V to expander VIN (red wire)
  • Metro GND to expander GND (black wire)
  • Metro SCL to expander SCL (yellow wire)
  • Metro SDA to expander SDA (blue wire)
  • Metro 2 to expander INT (green wire

Please follow the steps below for LED and button wiring.

Connect the LED and the button to the expander as follows:

LED to expander:

  • LED- to expander P7
  • LED+ to 470Ω resistor
  • 470Ω resistor to + on top edge of expander

Button to expander:

  • One leg of button to - on top edge of expander
  • Opposite leg of button to expander P0

Library Installation

You can install the PCF8574 library for Arduino using the Library Manager in the Arduino IDE.

Click the Manage Libraries ... menu item, search for PCF8574 , and select the Adafruit PCF8574 library:

When asked about dependencies, click "Install all".

Load Example

Open up File -> Examples -> Adafruit PCF8574 -> buttonledirq and upload to your Arduino wired to the sensor.

#include <Adafruit_PCF8574.h>

/* Example for 1 button that is connected from PCF GPIO #0 to ground, 
 * and one LED connected from power to PCF GPIO #7
 * We also have the IRQ output connected to an Interrupt input pin on the 
 * Arduino so we are not constantly polling from the PCF8574 expander
 */

Adafruit_PCF8574 pcf;

#define PCF_BUTTON  0  // on the GPIO expander!
#define PCF_LED     7  // on the GPIO expander!

#define ARDUINO_IRQ 2  // make sure this pin is possible to make IRQ input

void setup() {
  while (!Serial) { delay(10); }
  Serial.begin(115200);
  Serial.println("Adafruit PCF8574 button/led IRQ test");

  if (!pcf.begin(0x20, &Wire)) {
    Serial.println("Couldn't find PCF8574");
    while (1);
  }

  pcf.pinMode(PCF_BUTTON, INPUT_PULLUP);
  pcf.pinMode(PCF_LED, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

  // set up the interrupt pin on IRQ signal toggle
  pinMode(ARDUINO_IRQ, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(ARDUINO_IRQ), button_detect, CHANGE);
}

// We use a flag to make sure we don't enter the interrupt more than once
volatile bool in_irq = false;

// called when the button is pressed!
void button_detect(void) {
  if (in_irq) return; // we are already handling an irq so don't collide!
  
  in_irq = true;
  interrupts(); // Arduino UNO seems to require that we turn on interrupts for I2C to work!
  bool val = pcf.digitalRead(PCF_BUTTON);
  pcf.digitalWrite(PCF_LED, val);
  in_irq = false;
}

void loop() {
  delay(100); // we do nothing here!
}

Once loaded, press the button to see the LED light up when the button is pressed.

The GIF below shows a "lazy" way to connect the button and LED to the expander. You should always include a resistor when wiring an LED!

This GIF shows it wired to the QT Py, but the button press to turn on the LED is the same.

This guide was first published on Aug 03, 2022. It was last updated on 2022-08-03 14:47:34 -0400.

This page (Arduino) was last updated on Aug 07, 2022.

Text editor powered by tinymce.