How to Use the PCF8575 I/O Expander with Arduino

How to Use the PCF8575 I/O Expander with Arduino

The PCF8575 is a 16-bit I/O expander that communicates with a microcontroller via the I2C interface. It allows you to expand the number of input/output pins on your Arduino, making it perfect for projects requiring many sensors, buttons, or LEDs. This tutorial will guide you through connecting and using the PCF8575 with Arduino.


What You Will Need

  1. PCF8575 I/O Expander Module
  2. Arduino Board (e.g., Uno, Mega, Nano)
  3. Sensors, buttons, or LEDs for testing
  4. Breadboard and Jumper Wires
  5. A computer with the Arduino IDE installed

Step 1: Understanding the PCF8575 Module

The PCF8575 provides 16 additional GPIO pins that can be configured as inputs or outputs. It communicates via the I2C protocol and has an adjustable I2C address for connecting multiple modules to the same bus.

Pinout

Pin Function
VCC Power Supply (3.3V/5V)
GND Ground
SDA I2C Data Line
SCL I2C Clock Line
P0-P15 General Purpose I/O Pins

I2C Address Configuration

  • The I2C address is determined by the A0, A1, and A2 pins:
    • All pins to GND: 0x20 (default address)
    • All pins to VCC: 0x27

Step 2: Wiring the PCF8575 to Arduino

Here’s how to connect the PCF8575 to an Arduino:

PCF8575 Pin Arduino Pin
VCC 5V
GND GND
SDA A4 (SDA)
SCL A5 (SCL)

Connect your peripherals (e.g., LEDs or buttons) to the P0-P15 pins.

Note: For other Arduino boards, ensure you use the correct I2C pins.


Step 3: Install the Required Library

To simplify working with the PCF8575, install the "PCF8575" library.

Steps to Install:

  1. Open the Arduino IDE.
  2. Go to Sketch > Include Library > Manage Libraries.
  3. Search for "PCF8575" and click Install.

Step 4: Upload the Code

Example: Controlling LEDs

This code demonstrates how to turn LEDs on and off using the PCF8575:

#include <Wire.h>
#include <PCF8575.h>

PCF8575 pcf8575(0x20); // Initialize with the default I2C address

void setup() {
  Serial.begin(9600);

  // Set all pins as outputs
  for (int i = 0; i < 16; i++) {
    pcf8575.pinMode(i, OUTPUT);
  }

  Serial.println("PCF8575 initialized.");
}

void loop() {
  // Turn all LEDs on
  for (int i = 0; i < 16; i++) {
    pcf8575.digitalWrite(i, HIGH);
  }
  delay(1000);

  // Turn all LEDs off
  for (int i = 0; i < 16; i++) {
    pcf8575.digitalWrite(i, LOW);
  }
  delay(1000);
}

Example: Reading Buttons

This code demonstrates how to read input from buttons connected to the PCF8575:

#include <Wire.h>
#include <PCF8575.h>

PCF8575 pcf8575(0x20); // Initialize with the default I2C address

void setup() {
  Serial.begin(9600);

  // Set all pins as inputs
  for (int i = 0; i < 16; i++) {
    pcf8575.pinMode(i, INPUT);
  }

  Serial.println("PCF8575 initialized.");
}

void loop() {
  for (int i = 0; i < 16; i++) {
    int state = pcf8575.digitalRead(i);
    Serial.print("Pin ");
    Serial.print(i);
    Serial.print(": ");
    Serial.println(state);
  }
  delay(500);
}

Step 5: Test the Setup

  1. Connect the Arduino to your computer via USB.
  2. Open the Arduino IDE and select the correct Board and Port under the Tools menu.
  3. Upload the code to the Arduino by clicking Upload.
  4. For the LED example, observe LEDs turning on and off. For the button example, monitor button states in the Serial Monitor.

Applications of the PCF8575

  1. Expanding GPIO pins for Arduino projects
  2. Building complex LED matrices
  3. Reading multiple sensors or button arrays
  4. Controlling relays and actuators

Troubleshooting

  • No response from the module: Verify the I2C connections and address configuration.
  • Inconsistent readings: Ensure a stable power supply and proper pull-up resistors for the I2C lines.
  • Incorrect pin behavior: Double-check pin modes (INPUT/OUTPUT) in the code.

Conclusion

You’ve successfully interfaced the PCF8575 I/O expander with Arduino, allowing you to control or read from 16 additional GPIO pins. Experiment with different peripherals and expand your projects with this versatile module!

Leave a comment

Notice an Issue? Have a Suggestion?
If you encounter a problem or have an idea for a new feature, let us know! Report a problem or request a feature here.