B4R Code Snippet Interfacing PCF8574 (8 BIT) & PCF8575 (16 bit )IO port expander module...

Dear friends,
The code snippet interfaces Arduino Uno/ Wemos mini with PCF8574 , 8 bit IO port expander via I2C bus:
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private master As WireMaster
   
    Dim timer1 As Timer
    Dim state As Boolean
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    master.Initialize
    timer1.Initialize("timer1_Tick",2000)
    timer1.Enabled=True
   
   
End Sub

Sub timer1_Tick

    state=DigitalRead(7)
   
    If state Then
       
        DigitalWrite(7,False)
    Else
       
        DigitalWrite(7,True)
    End If
   
End Sub

Private Sub DigitalRead(pin As UInt)  As Boolean
    Dim pinstatus As Int
    master.WriteTo(0x71, Array As Byte(0x00))
    Dim result() As Byte = master.RequestFrom(0x38, 1)
    If result.Length = 0 Then
        Return False
    Else
        pinstatus= Bit.Get(result(0),pin)
        Log(pinstatus)
            If pinstatus=0 Then
                Return False
            Else
                Return True
            End If
               
    End If
End Sub

Private Sub DigitalWrite(pin As UInt,pinstate As Boolean)
   
    master.WriteTo(0x71, Array As Byte(0x00))
    Dim result() As Byte = master.RequestFrom(0x38, 1)
    If pinstate =False Then
        Bit.Clear(result(0),pin)
        master.WriteTo(0x38, Array As Byte(0x00,result(0)))
    Else
        Bit.Set(result(0),pin)
        master.WriteTo(0x38, Array As Byte(0x00,result(0)))
    End If
   
End Sub

Note:
The address for PCF8574 is: 0x20 whereas for PCF8574A it is :0x38 (which I have) with A0,A1,A2 all grounded.
The functions written for DigitalRead and DigitalWrite are not perfect hence you may suggest modifications/changes...
The code works with Arduino Uno as well as Wemos mini boards.
Hope somebody finds this useful. BTW it was my first attempt of interfacing any I2C device right from scratch!
 

Attachments

  • IMG_20170606_164920.jpg
    IMG_20170606_164920.jpg
    392.2 KB · Views: 773

rbghongade

Active Member
Licensed User
Longtime User
Here is the code for interfacing PCF8575 (16 bit) IO expander:
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private master As WireMaster
  
    Dim timer1,timer2 As Timer
    Dim state As Boolean
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    master.Initialize
    timer1.Initialize("timer1_Tick",50)
    timer1.Enabled=True
    Clear_All
  
End Sub

Sub timer1_Tick
Dim state As Boolean
    state=DigitalRead(13)
    state=Not(state)
    DigitalWrite(13,state)
    Log("Pin13:",state)
End Sub


Private Sub DigitalRead(pin As UInt)  As Boolean
    Dim pinstatus As Int
    master.WriteTo(0x20, Array As Byte(0x01))
    Dim result() As Byte = master.RequestFrom(0x20, 2)
    If result.Length = 0 Then
        Return False
    Else
        If pin>=0 And pin<=7 Then
        pinstatus= Bit.Get(result(0),pin)
      
        If pinstatus=0 Then
            Return False
        Else
            Return True
        End If
        End If
        If pin>=10 And pin<=17 Then
            pinstatus= Bit.Get(result(1),pin-10)
          
            If pinstatus=0 Then
                Return False
            Else
                Return True
            End If
        End If
    End If
End Sub

Private Sub DigitalWrite(pin As UInt,pinstate As Boolean)
  
    master.WriteTo(0x20, Array As Byte(0x01))
  
    Dim result() As Byte = master.RequestFrom(0x20, 2)
    If pinstate =False Then
        If pin >=0 And pin <=7 Then
            Bit.Clear(result(0),pin)
            master.WriteTo(0x20, Array As Byte(result(0),result(1)))
        End If
        If pin >=10 And pin <=17 Then
            Bit.Clear(result(1),pin-10)
            master.WriteTo(0x20, Array As Byte(result(0),result(1)))
        End If
    Else
        If pin >=0 And pin <=7 Then
            Bit.set(result(0),pin)
            master.WriteTo(0x20, Array As Byte(result(0),result(1)))
        End If
            If pin >=10 And pin <=17 Then
            Bit.set(result(1),pin-10)
        master.WriteTo(0x20, Array As Byte(result(0),result(1)))
        End If
    End If
  
End Sub

Private Sub Clear_All
    master.WriteTo(0x20, Array As Byte(0x00,0x00))
End Sub

NOTE: The pin numbers are 0 through 7 and 10 through 17
 

Attachments

  • IMG_20170614_145852.jpg
    IMG_20170614_145852.jpg
    426.4 KB · Views: 546
Top