B4R Library MCP4xxx

Here is a library using this arduino library for the MCP4xxx series SPI digital potentiometer/rheostat, for example the MCP4131.

Be sure to read the description for the arduino library as not all chips in the series are supported. On some chips MISO, MOSI are multiplexed and need special wiring, this is explained well in the description for the arduino library.

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Public mcp As MCP4xxx 'testing for MCP4131
    Public const total=9800 As UInt 'measured from term A to B
    Public const wiperohms=110 As UInt 'measured from term A to wiper when wiper is at 0
    Public ohminc=0 As UInt
End Sub

Private Sub AppStart
    Serial1.Initialize(9600)
    Log("AppStart")
 
    'testing for MCP4131
    mcp.Initialize(10,mcp.POT_0,mcp.RES7BIT,mcp.POTENTIOMETER)
 
    Log("wiper range ",mcp.WiperRange)
    ohminc=total/mcp.WiperRange
    Log("increments of ",ohminc," ohms")
    Log("wiper pos ",mcp.WiperGet)
 
    Log("Is terminal A is connected? ",mcp.ConnTermA)
    mcp.ConnTermASet(False)
    Log("Is terminal A is connected? ",mcp.ConnTermA)
    mcp.ConnTermASet(True)
    Log("Is terminal A is connected? ",mcp.ConnTermA)

    swing_wiper
End Sub

Sub swing_wiper
    Do While True
        For i = 0 To 127
            mcp.WiperSet(i)
            'mcp.WiperIncrement
            Log("Set to: ",(mcp.WiperGet*ohminc)+wiperohms," ohms")
            Delay(5000)
        Next
    Loop
End Sub

Updates

v1.01
Fixed a problem that didn't allow you to use both potentiometers.
 

Attachments

  • rMCP4xxx.zip
    6.4 KB · Views: 355
Last edited:

kolbe

Active Member
Licensed User
Longtime User
Thanks I understand. It's just a quick example (bad example) of how to use the library. I've actually noticed that long delays will cause a panic exception on the esp32 and it will reboot. I'm just guessing here but I think it interferes with the other core that's doing wifi or triggers a watchdog timer? It's hard to reproduce but seems related to long delays... but this is a topic for a separate thread.
 
Top