B4R Question Wishing library for MCP23017 16I/O Expander for ESP8266...

rbghongade

Active Member
Licensed User
Longtime User
Dear friends,
As we know the number of GPIOs for WeMos Mini and WeMos D1 are only 11, we may require more GPIOs at our disposal. Such a device is the Microchip MCP23017, an I2C based 16 I/O expander. I came across an Arduino sketch which interfaces WeMos to MCP23017. I tested it with Arduino IDE and found it working flawlessly. I request somebody to create a library for B4R. The sketch is attached herewith.
regards,
 

Attachments

  • mcp23017_esp8266.zip
    1.2 KB · Views: 401

Erel

B4X founder
Staff member
Licensed User
Longtime User
I wasn't able to get the MCP23017 to work. Might be defective.

This code should turn on all the pins:
B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private master As WireMaster
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   master.Initialize
   master.WriteTo(0x20, Array As Byte(0, 0))
   master.WriteTo(0x20, Array As Byte(1, 0))
   Delay(100)
   master.WriteTo(0x20, Array As Byte(0x12, 0xFF))
   master.WriteTo(0x20, Array As Byte(0x13, 0xFF))
End Sub

Can you try it and see whether it works? Test it with both the ESP8266 and an Arduino.
 
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
Dear Erel,
Sorry, I missed this post. I tried the above code with Wemos and it works ok! Even tried to access individual pin and that too worked great. But it would be great if a library is wrapped for the same as remembering the hexcode for each pin would be unwieldy for use.
Thanks.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code to write to the pins (0 - 7 for each port):
B4X:
Sub Process_Globals
  Public Serial1 As Serial
  Private master As WireMaster
  Private PortState(2) As Byte
End Sub

Private Sub AppStart
  Serial1.Initialize(115200)
  Log("AppStart")
  master.Initialize
  master.WriteTo(0x20, Array As Byte(0, 0))
  master.WriteTo(0x20, Array As Byte(1, 0))
  DigitalWrite(0, 2, True)
End Sub

'PortIndex - 0 = Port A, 1 = Port B
Private Sub DigitalWrite (PortIndex As Byte, PinNumber As Byte, Value As Boolean)
   If Value Then
     PortState(PortIndex) = Bit.Set(PortState(PortIndex), PinNumber)
   Else
     PortState(PortIndex) = Bit.Clear(PortState(PortIndex), PinNumber)
   End If
   master.WriteTo(0x20, Array As Byte(0x12 + PortIndex, PortState(PortIndex)))
End Sub
 
Last edited:
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
Dear Erel,
Thanks for the code. How to read the port pin? Would it be master.RequestFrom(...) ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Set the port to input mode.
2. Read the pins values and get the bit values from the byte result:
B4X:
Sub Process_Globals
  Public Serial1 As Serial
  Private master As WireMaster
  Private PortState(2) As Byte
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   master.Initialize
   master.WriteTo(0x20, Array As Byte(0, 0)) 'A output
   master.WriteTo(0x20, Array As Byte(1, 0xFF)) 'B input
   Dim b As Byte = DigitalRead(1)
   If Bit.Get(b, 5) = 1 Then
     '''
   End If
End Sub

'PortIndex - 0 = Port A, 1 = Port B
Private Sub DigitalRead (PortIndex As Byte) As Byte
   master.WriteTo(0x20, Array As Byte(0x12 + PortIndex))
   Dim result() As Byte = master.RequestFrom(0x20, 1)
   If result.Length = 0 Then
     Return 0
   Else
     Return result(0)
   End If
End Sub

'PortIndex - 0 = Port A, 1 = Port B
Private Sub DigitalWrite (PortIndex As Byte, PinNumber As Byte, Value As Boolean)
   If Value Then
     PortState(PortIndex) = Bit.Set(PortState(PortIndex), PinNumber)
   Else
     PortState(PortIndex) = Bit.Clear(PortState(PortIndex), PinNumber)
   End If
   master.WriteTo(0x20, Array As Byte(0x12 + PortIndex, PortState(PortIndex)))
End Sub
 
Last edited:
Upvote 0
Top