B4R Code Snippet Reading/Input and Writing/Output using an PCF8575 16 bit I/O extension shield module

SubName: Manipulating all 16 bits/pins from a PCF8575 16 bit I/O IC using only 2 pins from an Arduino or ESP8266, communication is done through I²C Bus communications.
Description: If you are running out of I/O pins on your Arduino based device then I highly recommend that you take a serious look into using a PCF8575 16 bit I/O extension shield module. With the following code you can easily connected the PCF5875 via I²C Bus communications to read from or write to all 16 bits/pins of the PCF8575.

Writing to the PCF8575 is a simple task of just writing an array of bytes using WireMaster.WriteTo, for example.
B4X:
WireMaster.WriteTo(I2CAddress, Array As Byte(0xFF, 0xAA))    'Equals '1111 1111, 1010 1010'
The above code '0xFF, 0xAA' represents '1111 1111, 1010 1010' respectively, thus giving you 1111111110101010. If need be you can also convert Binary to Hex (Hexadecimal) and write into the PCF8575 that way.

Below: Starting from pin 0 (P0), every 1 second I manually moved the orange wire connected to the 3.3V line of the Arduino to the next pin on the PCF8575, P0 to P7 then P10 to P17 thus reading all 16 bits/pins.

********************* PROGRAM STARTING ****************
AppStart
0000000000000001
0000000000000010
0000000000000100
0000000000001000
0000000000010000
0000000000100000
0000000001000000
0000000010000000
0000000100000000
0000001000000000
0000010000000000
0000100000000000
0001000000000000
0010000000000000
0100000000000000
1000000000000000

Libraries: rWire
B4X:
'WIRE LEGEND for PCF8575 I2C 16 bit I/O Extension Shield Module
'PCF8575 to Arduino
'VCC = 5V
'GND = GND
'23 (SDA) = A4
'22 (SCK) = A5
'P0 to P17 = 3.3V to 5V

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial

    Private WireMaster As WireMaster
    Private TmrReadPCF8575 As Timer
    Private I2CAddress As Byte = 0x20
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")

    WireMaster.Initialize
    WireMaster.WriteTo(I2CAddress, Array As Byte(0x00, 0x00))         'Set all bits to 0. Not really needed in this example but I added it anyway.
    'WireMaster.WriteTo(I2CAddress, Array As Byte(0xFF, 0xAA))        'Set bits to 1111 1111 1010 1010

    TmrReadPCF8575.Initialize("TmrReadPCF8575_Tick", 1000)
    TmrReadPCF8575.Enabled = True
End Sub

Sub TmrReadPCF8575_Tick
    Dim P0ToP7(8), P10ToP17(8) As Byte
    BytesToBin(WireMaster.RequestFrom(I2CAddress, 8)(1), P10ToP17)    '1 byte equals 8 bits in length
    BytesToBin(WireMaster.RequestFrom(I2CAddress, 8)(0), P0ToP7)      '1 byte equals 8 bits in length
    Log(JoinBytes(Array(P10ToP17, P0ToP7)))                           'Join the 2 bytes together to make it 16 bits in length
End Sub

Sub BytesToBin (Bytes As Byte, PCFPins() As Byte)
    For i = 0 To PCFPins.Length - 1
        If Bit.Get(Bytes, 7 - i) = 1 Then PCFPins(i) = Asc(1) Else PCFPins(i) = Asc(0)
    Next
End Sub

Tags: Arduino, PCF8575, 1, 2, byte, 8, 16, bit, pin, Read, Write, Input, Output

How to connect the PCF8575 to your Arduino.
PCF8575-Extension-Shield_bb.jpg


Here is the PCF8575 on a mini breadboard connected to an UNO based microcontroller board.
IMG_20170712_041549.jpg


Enjoy...
 
Last edited:
Top