Controlling a MCP4131 with MicroPython

Hello, and thank you for reading this question.

I have the Arduino Nano ESP32 working with MicroPython. So far I have been able to read in an analog value using the ADC and change the color of the onboard RGB LED.

I am looking for guidance on how to control a MCP4131 which seems trival with C/C++ given the availability of the library, but I am using MicroPython and haven't found any examples. I plan to have the hardware connected as follows:

  • MCP4131, pin 1 (CS) => Nano ESP32, MicroPyhton pin 21 (D10)
  • MCP4131, pin 2 (SCK) => Nano ESP32, MicroPyhton pin 48 (D13)
  • MCP4131, pin 3 (SDI/SDO) => Nano ESP32, MicroPyhton pin 38 (D11)
  • MCP4131, pin 4 (Vss) => Nano ESP32 Ground
  • MCP4131, pin 8 (Vdd) => Nano ESP32 3.3V

Any weblinks or suggestions on how to use MicroPython to control the MCP4131 would be greatly appreciated as I haven't been able to find a library to use with MicroPython.

I don't have a Nano but I do intend to get one so I can compare it with the esp's I have. The micropython docs refer to the esp32 as having id's for their hardware spi interface. I see, but I could be mistaken, that the nano has just the one set of hardware spi pins which you have already identified (10,11,12 and 13 with 12 not used) so initialization should be as simple as

import machine
from machine import Pin,SPI

spi = machine.SPI(1, baudrate=400000)   # Create SPI peripheral 0 at frequency of 400kHz.
                                        # Depending on the use case, extra parameters may be required
                                        # to select the bus characteristics and/or pins to use.
cs = Pin(10, mode=Pin.OUT, value=1)     # Create chip-select on pin 10.

above the id is entered as 1 if that's not right you could try 0 or 2

You can go even further initializing by entering the pin numbers

SPI(1, 10000000, sck=Pin(13), mosi=Pin(11), miso=None)

Once you have got past that part there are a couple of ways you can write data as bytes or an array of bytes, I don't know what your device expects but the following should help.

try:
    cs(0)                               # Select peripheral (active low).
    spi.write(b"12345678")              # Write 8 bytes.
finally:
    cs(1)                               # Deselect peripheral.

or

try:
    cs(0)                               # Select peripheral (active low).
    spi.write(bytearray([0x06]))  	# Write the value 6

finally:
    cs(1)   				# Deselect peripheral.

This was very helpful and got me on the right path. Because MicroPython remaps the pins I changed the variable values. Also, the MCP4131 requires two bytes to set the resistance value.

from machine import Pin,SPI

# set baud rate to 1MHz
# sck (D13) is mapped to MicroPython Pin 48
# sdi (D11) is mapped to MicroPython Pin 38

spi = machine.SPI(1, baudrate=1000000, sck=Pin(48), mosi=Pin(38), miso=None)
    
# chip select is trigger by D10 which is mapped to MicroPython Pin 21   
cs = Pin(21, mode=Pin.OUT, value=1)

# to adjust the wiper on the digital potentiometer 16 bits are sent
# The first 4 are the address, which for the MCP4131 is the wiper
# and is represented by 0000
# The next two are the command (00 is write)
# The remaining 2 bits and the 8 bits of the second byte is the value
# Since the MCP4131 uses 128 or 256 steps those remaining two bits are 00
# The second byte has the value used to set the potentiometer wiper

while True:
    cs(0) # select the MCP4131 chip
    spi.write(bytearray([0x00])) # command byte
    spi.write(bytearray([0x42])) # adjust this value to change the resistance
    cs(1) # release the MCP4131 chip