PCF8575 (i2c 16 Pins Digital I/O Expander) Fast Easy Usage (Arduino, Esp8266 and Esp32)

by xxreef in Circuits > Arduino

25448 Views, 5 Favorites, 0 Comments

PCF8575 (i2c 16 Pins Digital I/O Expander) Fast Easy Usage (Arduino, Esp8266 and Esp32)

mainInstructables.jpg
Test of pcf8575 with esp8266 to control 16 led

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

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.

Supplies

  • Arduino or esp8266 or esp32
  • pcf8575

Module Connections

testReadWriteLedButton_bb-1024x496[1].jpg
breadbordPCF8575-1024x576[1].jpg

The connections to the module are straight forward.

  • 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 i2c pcf8575 IC with arduino and esp8266.

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

How I2c Works

F2S40QEJB3ZUS46.LARGE1_[1].jpg

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

pcf8575-250x250[1].jpg
pcf8575module-e1563220439840[1].jpg

You can use a normal IC or module.

Normal IC on Aliexpress

Module on Aliexpress (ver1) Aliexpress (ver2).

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

PCF8575 pcf8575(&I2Ctwo);

//PCF8575 pcf8575(&I2Ctwo, 21,22);

//PCF8575 pcf8575(&I2Ctwo, 0x5C);

//PCF8575 pcf8575(&I2Ctwo, 21,22,0x5C);

You must set input/output mode:

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

Usage: Read Values

pcf8575pinouts-e1563220294540[1].jpg

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);

if you want read a single input:

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

Usage Write Values

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 Schema

testReadWriteLedButton_bb-1024x496[1].jpg

For the examples I use this wire schema on breadboard:

Video of Test With 16 Led

Test of pcf8575 with esp8266 to control 16 led

Thanks