Using the PCF8575 with Arduino involves wiring up the sensor to your Arduino-compatible microcontroller, installing the Adafruit PCF8574 library (which supports the PCF8575 as well!) 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 PCF8575 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 PCF8575 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 (purple 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 (purple 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 P8
- LED- to 470Ω resistor
- 470Ω resistor to + row on expander
Button to expander:
- One leg of button to - row on 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 -> pcf8575_buttonledirq and upload to your Arduino wired to the sensor.
#include <Adafruit_PCF8575.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 PCF8575 expander */ Adafruit_PCF8575 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 PCF8575 button/led IRQ test"); if (!pcf.begin(0x20, &Wire)) { Serial.println("Couldn't find PCF8575"); 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 wire up a button and LED. You should always use a resistor when wiring up an LED!
Page last edited January 21, 2025
Text editor powered by tinymce.