Using the PCF8575 I2c I/o Expander to Read Inputs With Arduino

Introduction: Using the PCF8575 I2c I/o Expander to Read Inputs With Arduino


What I have used...

Step 1: Build the Circuit

The SDA and SCL lines enable i2c communication. 

Which pins are SDA and SCL?
Mega2560: 20 (SDA), 21 (SCL)
Leonardo: 2 (SDA), 3 (SCL)






Step 2: The Code

Okay now the scary part - the code. 

If you want to use i2c devices it is best to understand how the code works otherwise you are going to have many problems down the road. 

i2c is handled using the Arduino wire library.

To understand i2c you are going to have to do a bit of reading. Expect to be very confused at first. If you plan to do any intermediate and advanced electronics learning i2c is imperative. 

I will try to give a very simple explanation of i2c. 

One thing you will notice, as you begin to work with electronics, is that you always have a mess of wires. A long time ago Phillips said 'hell with all these wires - we need a way for all the devices to talk to eachother with only two wires'. Shortly after, i2c was created. Now you can buy i2c chips which do just about everything, and all these chips are able to communicate with eachother using only two wires. i2c is often called the 'two wire interface'.
So how do these devices talk to each other? Well, they all have an address, much like you have a mailing address at your house. With this unique address you can talk to each device individually. The Arduino, which would be the master-i2c-device, can send instructions to the individual slave-i2c-devices using a fancy communication protocol. In Arduino, the send-communication looks like this:

Wire.beginTransmission(aDeviceAddress); //who are you talking to?
Wire.send(someDate); //what data are you sending this device
Wire.endTransmission(); //end communication

And the receive-communication looks like this:
byte dataReceived[2]; //a two byte array to hold our data
Wire.beginTransmission(aDeviceAddress); ////who are you talking to?
Wire.endTransmission(); //end communication
Wire.requestFrom(aDeviceAddress,2); //request two bytes of data
    dataReceived[0] = Wire.receive(); //read byte 1
    dataReceived[1] = Wire.receive(); //read byte 2

The real tricky part of i2c is interpreting the datasheet and understanding how you and expected to receive or send data to the device. When writing i2c code you constantly have to reference the datasheet. 
The Arduino code file is attached below. 

NOTE if you are using Arduino1.0 
Wire.send and Wire.receive statements MUST BE changed to Wire.write and

Step 3: Testing

Here is a sample video of the code working. 

Be the First to Share


    • Backyard Contest

      Backyard Contest
    • Fabric Challenge

      Fabric Challenge
    • Pets Challenge

      Pets Challenge




    Reply 9 years ago on Introduction

    Oly digital. 


    Reply 2 years ago

    is it possible to read "pulse"( reed switch) signal , like rain gauge ?
    i want to use 20 rain gauges with my school project, what do you suggest to use for that ?


    6 years ago

    Do you have any schematic diagram for this project???


    8 years ago on Introduction



    Reply 8 years ago on Introduction

    not possible
    2^3 = 8


    9 years ago on Introduction


    But how should I address and read these pins?