Dear friends,
The code snippet interfaces Arduino Uno/ Wemos mini with PCF8574 , 8 bit IO port expander via I2C bus:
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
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!
			
			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 SubNote:
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!
 
				 
			 
 
		 
			 
 
		