PCF8575 I2C 16-Bit Digital Input Output Expander

PCF8575 I2C 16-Bit Digital Input Output Expander © CC BY-NC-SA

Library to use I2C PCF8575 IC with Arduino and ESP8266. This IC can control until 16 digital devices like button or LED with only two pins.

  • 250 views
  • 0 comments
  • 1 respect

Components and supplies

Apps and online services

About this project

This 16-bit I/O expander for the two-line bidirectional bus (I2C) is designed for 2.5-V to 5.5-V VCCoperation.

The PCF8575 device provides general-purpose remote I/O expansion for most microcontroller families by way of the I2C interface [serial clock (SCL), serial data (SDA)].

The device features a 16-bit quasi-bidirectional input/output (I/O) port (P07–P00, P17–P10), including latched outputs with high-current drive capability for directly driving LEDs. Each quasi-bidirectional I/O can be used as an input or output without the use of a data-direction control signal. At power on, the I/Os are high. In this mode, only a current source to VCC is active.

  • I2C to Parallel-Port Expander
  • Open-Drain Interrupt Output
  • Low Standby-Current Consumption of 10 µA Max
  • Compatible With Most Microcontrollers
  • 400-kHz Fast I2C Bus
  • Address by Three Hardware Address Pins for Use of up to Eight Devices
  • Latched Outputs With High-Current Drive Capability for Directly Driving LEDs
  • Current Source to VCC for Actively Driving a High at the Output
  • Latch-Up Performance Exceeds 100 mA Per JESD 78, Class II
  • ESD Protection Exceeds JESD 22
  • 2000-V Human-Body Model
  • 200-V Machine Model
  • 1000-V Charged-Device Model

Module Connections

The connections to the module are straightforward.

  • Supply 3.3 or 5V power and ground.
  • Connect I2C SCL and SDA lines to same on the MCU.
  • If used, connect the INT line to an interrupt input on the MCU and use a pull-up resistor.

I write a library to use IC2 PCF8575 IC with Arduino and ESP8266.

So can read and write digital value with only 2 wire (perfect for ESP-01).

I try to simplify the use of this IC, with a minimal set of operation.

How I2C Works

I2C works with it’s two wires, the SDA (data line) and SCL (clock line).

Both these lines are open-drain, but are pulled-up with resistors.

Usually there is one master and one or multiple slaves on the line, although there can be multiple masters, but we’ll talk about that later.

Both masters and slaves can transmit or receive data, therefore, a device can be in one of these four states: master transmit, master receive, slave transmit, slave receive.

Library

You can find my library here.

To download.

Click the DOWNLOADS button in the top right corner, rename the uncompressed folder PCF8575.

Check that the PCF8575 folder contains PCF8575.cpp and PCF8575.h.

Place the PCF8575 library folder your /libraries/ folder.

You may need to create the libraries subfolder if its your first library.

Restart the IDE.

IC or Module

You can use a normal IC or module.

You can buy here AliExpress.

You can buy here AliExpress - AliExpress

Usage

As already say I try to simplify the use of this IC, with a minimal set of operation.

PCF8575 address map 0x20-0x27

On constructor you must pas the address of i2c, you can use A0, A1, A2 pins to change the address, you can find the address value here (to check the adress use this guide I2cScanner)

PCF8575(uint8_t address);  

for esp8266 if you want specify SDA e SCL pin use this:

PCF8575(uint8_t address, uint8_t sda, uint8_t scl);  

For esp32 you can pass directly che TwoWire, so you can choice the secondary i2c channel:

// Instantiate Wire for generic use at 400kHz  
TwoWire I2Cone = TwoWire(0);  
// Instantiate Wire for generic use at 100kHz  
TwoWire I2Ctwo = TwoWire(1);  
// Set dht12 i2c comunication with second Wire using 21 22 as SDA SCL  
DHT12 dht12(&I2Ctwo);  
//DHT12 dht12(&I2Ctwo, 21,22);  
//DHT12 dht12(&I2Ctwo, 0x5C);  
//DHT12 dht12(&I2Ctwo, 21,22,0x5C);  

You must set input/output mode:

pcf8575.pinMode(P0, OUTPUT);  
pcf8575.pinMode(P1, INPUT);  
pcf8575.pinMode(P2, INPUT);  

then IC as you can see in the image have 16 digital input/output:

So to read all analog input in one trasmission you can do (even if I use a 10millis debounce time to prevent too much read from I2C):

PCF8575::DigitalInput di = PCF8575.digitalReadAll();  
Serial.print("READ VALUE FROM PCF P1: ");  
Serial.print(di.p0);  
Serial.print(" - ");  
Serial.print(di.p1);  
Serial.print(" - ");  
Serial.print(di.p2);  
Serial.print(" - ");  
Serial.println(di.p3);  

To follow a request (you can see It on issue #5) I create a define variable to work with low memori device, if you decomment this line on.h file of the library:

// #define PCF8575_LOW_MEMORY  

Enable low memory props and gain about 7byte of memory, and you must use the method to read all like so:

byte di = pcf8575.digitalReadAll();  
Serial.print("READ VALUE FROM PCF: ");  
Serial.println(di, BIN);  

where di is 2 byte in uint16_u variable like 1110001 1110001, so you must do a bitwise operation to get the data, operation that I already do in the “normal” mode, here an example:

p0 = ((di & bit(0)>0)?HIGH:LOW;  
p1 = ((di & bit(1)>0)?HIGH:LOW;  
p2 = ((di & bit(2)>0)?HIGH:LOW;  
p3 = ((di & bit(3)>0)?HIGH:LOW;  
p4 = ((di & bit(4)>0)?HIGH:LOW;  
p5 = ((di & bit(5)>0)?HIGH:LOW;  
p6 = ((di & bit(6)>0)?HIGH:LOW;  
p7 = ((di & bit(7)>0)?HIGH:LOW;  
p8 = ((di & bit(8)>0)?HIGH:LOW;  
p9 = ((di & bit(9)>0)?HIGH:LOW;  
p10 = ((di & bit(10)>0)?HIGH:LOW;  
p11 = ((di & bit(11)>0)?HIGH:LOW;  
p12 = ((di & bit(12)>0)?HIGH:LOW;  
p13 = ((di & bit(13)>0)?HIGH:LOW;  
p14 = ((di & bit(14)>0)?HIGH:LOW;  
p15 = ((di & bit(15)>0)?HIGH:LOW;  

if you want read a single input:

int p1Digital = PCF8575.digitalRead(P1); // read P1  

If you want write a digital value you must do:

PCF8575.digitalWrite(P1, HIGH);  

or:

PCF8575.digitalWrite(P1, LOW);  

You can also use interrupt pin: You must initialize the pin and the function to call when interrupt raised from PCF8575

// Function interrupt  
void keyPressedOnPCF8575();  
// Set i2c address  
PCF8575 pcf8575(0x39, ARDUINO_UNO_INTERRUPT_PIN, keyPressedOnPCF8575);  

Remember you can’t use Serial or Wire on interrupt function.

The better way is to set only a variable to read on loop:

void keyPressedOnPCF8575(){  
   // Interrupt called (No Serial no read no wire in this function, and DEBUG disabled on PCF library)  
    keyPressed = true;  
}  

Connections schematic

For the examples, I use this wire schema on breadboard:

Additional Examples From PCF8574 Reusable on PCF8575

In the time peoples help me to create new examples, you can find It to my site.

Code

Github
https://github.com/xreef/PCF8575_library

Comments

Similar projects you might like

NANO I2C IO Expander

Project tutorial by Joel Soranzo

  • 3,059 views
  • 2 comments
  • 8 respects

ESP8266 - Beginner Tutorial + Project

by Inventor Niv

  • 20,120 views
  • 6 comments
  • 47 respects

Master Slave I2C Connection

by PIYUSH_K_SINGH

  • 7,374 views
  • 0 comments
  • 9 respects

Speak to Arduino and Control It with Google Assistant

Project tutorial by ElectroPeak

  • 22,715 views
  • 20 comments
  • 143 respects
Add projectSign up / Login