Introduction: PCF8575 (i2c 16 Pins Digital I/O Expander) Fast Easy Usage (Arduino, Esp8266 and Esp32)
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.
- Arduino or esp8266 or esp32
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Module Connections
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.
Step 2: 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.
Step 3: Library
You can find my library here.
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.
Step 4: IC or Module
Step 5: 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)
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, 21,22);
//PCF8575 pcf8575(&I2Ctwo, 0x5C);
//PCF8575 pcf8575(&I2Ctwo, 21,22,0x5C);
You must set input/output mode:
pcf8575.pinMode(P1, INPUT); pcf8575.pinMode(P2, INPUT);
Step 6: Usage: Read Values
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
Step 7: Usage Write Values
If you want write a digital value you must do:
You can also use interrupt pin: You must initialize the pin and the function to call when interrupt raised from PCF8575
// Function interrupt
// 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:
// Interrupt called (No Serial no read no wire in this function, and DEBUG disabled on PCF library) keyPressed = true; }
Step 8: Connections Schema
For the examples I use this wire schema on breadboard: