Library for PCF8575 I2C Expander

PCF8575 is 16-bit I/O expander for the two-line bidirectional bus (I2C). It features 16-bit quasi-bidirectional I/O port, P07-P00 and P17-P10. It can easily be switched from output to input; write I/O with “1” to be used as an input. Quasi-bidirectional means that outputs can be used both as an input and output without the need to reconfigure the port.

There are 8 address combinations (A2 A1 A0) so it is possible to have eight I2C expanders on same I2C bus.

Connection PCF8575 – STM32 is plain simple. There is no need for external pull-up resistors although I had some problems with resetting I2C line so I had to put 4K7 resistors. PCF can work with 3.3V or 5V so there is no need to worry about burning it up.

When the port outputs a logic high, it is weakly driven, allowing an external device to pull the pin low.

When the pin is driven low, it is driven strongly and able to sink large current.

 

Connection

In the scheme below two PCF8575 are connected with STM on different I2C bus; they have same addresses. First PCF is reading state of a PIR (Passive Infrared Sensor) and second PCF is just toggling LEDs (setting pins as inputs – outputs).

STM32F4 with 2xPCF8575 and PIR
STM32F4 with 2xPCF8575 and PIR

Library Usage

Library is written for STM32F4 but it should work with the rest of STM32F family. Project is created using STM32CubeMX with I2C1 – I2C3 enabled and clock on 180MHz. If you don’t know how to create project with STM32Cube you can check last tutorial on STM32CubeMX.

  1. Initialize PCF8575 address with
  2. To set pins as input or output simply write “1” or “0” with PCF8575_Write with pin number as argument (0x8000 to set P17 as input or 0x0040 to set P6 as input).
  3. Same as write, to read use PCF8575_Read function.

PCF8575 Functions

Examples

Library can be downloaded from the link below:

[download id=”7″]

Full example is also available on github.

Related posts