Introduction: Bus Pirate W/SPI and MCP4131 Digital Pot

I was trying to understand Bus Pirate V3 and explore its features, also I wanted to connect it to an easy chip to handle it, I had kept in my desk a 4131 digital potentiometer for long time with no use. I decided to write this guide because there is very little documentation about the use of BP and how to operate it.

This experiment helped me better understand the SPI communications.

Here is the datasheet for the MCP4131-103 chip

This chip is a Digital Potentiometer with SPI interface.

For more information about SPI I recommend view this video:

How a DigiPot or Digital Potentiometer works Tutorial by eTechTom.

Step 1: Connections

Wiring:

BP <=> 4131

1.GND <=> 4.VSS

3.+5v <=> 8.VDD

4.ADC <=> 1.CS (Optional. Used as a voltmeter to test Slave Select [SS] is ready)

7.CLK <=> 2.SCK

8.MOSI <=> 3.SDI/SDO

9.CS <=> 1.CS

10.MISO <=> Resistor 220ohm <=> 3.SDI/SDO

ON 4131:

8.VDD <=> +5V

7.P0B <=> GND

6.P0W (is the wiper) <=> LED anode

5.P0A <=> +5V

Step 2: BP SPI Configuration

This is the sequence to configure BP SPI:

HiZ>m
1. HiZ 2. 1-WIRE 3. UART 4. I2C 5. SPI 6. 2WIRE 7. 3WIRE 8. LCD x. exit(without change)

(1)>5

command m will display BP mode menu, select option 5 to config SPI.

Set speed:
 1. 30KHz
 2. 125KHz
 3. 250KHz
 4. 1MHz
(1)>4 

Select option number 4, according to its datasheet the chip can handle from 0 to 10 Mhz. Next options will be selected by default.

Clock polarity:
 1. Idle low *default
 2. Idle high
(1)> 1
Output clock edge:
1. Idle to active
2. Active to idle *default
(2)> 2
Input sample phase:
1. Middle *default
2. End
(1)>1
CS:
1. CS
2. /CS *default
(2)> 2
Select output type:
1. Open drain (H=Hi-Z, L=GND)
2. Normal (H=3.3V, L=GND)
(1)>2
Ready
SPI>

Here some additional configuration to test CS and circuit:

SPI>C
a/A/@ controls CS pin
SPI>P
WARNING: pins not open drain (HiZ)
Pull-up resistors ON
Warning: no voltage on Vpullup pin
SPI>W
POWER SUPPLIES ON
SPI>A
AUX HIGH
SPI>d
VOLTAGE PROBE: 3.28V
SPI>a
AUX LOW
SPI>d
VOLTAGE PROBE: 0.00V
SPI>@
AUX INPUT/HI-Z, READ: 0
SPI>

Step 3: Write/Read Commands

According to 4131 datasheet (table 7.1) the chip has four commands. This chip only have one address 0000 or 0b0000 or 0x00. Other chips like 4132 has two pot and two address.

Write Increment/Decrement one step:

To increment one step of the wiper resistance there is a 8-Bits command: 01. To complete the sequence we need to send 0000 memory address + 01 command + data bits. The result will be 0b00000100, equals 0x04.

Command "a" put LOW pin CS and select the chip to talk to.

The command on BP terminal will be:

SPI>[4]
/CS ENABLED
WRITE: 0x04
/CS DISABLED

In this case [ will be equals command a and ] will be equals command A

SPI>a 4 A
AUX LOW WRITE: 0x04
AUX HIGH

To decrement on step, execute command 0b00001000 or 0x08.

SPI>[8]
/CS ENABLED
WRITE: 0x08
/CS DISABLED

Write a value:

This chip has 129 steps, which means you can send it a value from 0 to 128, according to this value this will be the wiper output resistance. The command is 00 and in this case is a 16-Bits value. Like this, 0000 memory address + 00 command + 00 data bits. After that we need to send it the value of 0-128. Ex: 0b00000000 0b00000000 = 0, or 0b00000000 0b10000000 = 128.

This will turn on the LED

 SPI>[0 128]
/CS ENABLED
WRITE: 0x00
WRITE: 0x80
/CS DISABLED
SPI>

This will turn off the LED

SPI>[0 0]
/CS ENABLED
WRITE: 0x00
WRITE: 0x00
/CS DISABLED

This will put the LED in the middle point

SPI>[0 64]
/CS ENABLED
WRITE: 0x00
WRITE: 0x40
/CS DISABLED

To read current value:

There is also a 16-Bit, the command will be 11. The sequence will be 0b0000 for memory address + 11 for the read command + 00 for data. Then we need to read two bytes with the response. The bits sequence will be 0b00001100 and read data. don't put in high CS until reading.

SPI>[12 r:2]
/CS ENABLED
WRITE: 0x0C
READ: 0x40 0xFE
/CS DISABLED

The response id 0x40 = 64 DEC.

Do not forget to keep experimenting with the tool and enjoy the results.