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.
This chip is a Digital Potentiometer with SPI interface.
For more information about SPI I recommend view this video:
Step 1: Connections
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
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:
1. HiZ 2. 1-WIRE 3. UART 4. I2C 5. SPI 6. 2WIRE 7. 3WIRE 8. LCD x. exit(without change)
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
Input sample phase:
1. Middle *default
2. /CS *default
Select output type:
1. Open drain (H=Hi-Z, L=GND)
2. Normal (H=3.3V, L=GND)
Here some additional configuration to test CS and circuit:
a/A/@ controls CS pin
WARNING: pins not open drain (HiZ)
Pull-up resistors ON
Warning: no voltage on Vpullup pin
POWER SUPPLIES ON
VOLTAGE PROBE: 3.28V
VOLTAGE PROBE: 0.00V
AUX INPUT/HI-Z, READ: 0
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:
In this case [ will be equals command a and ] will be equals command A
SPI>a 4 A
AUX LOW WRITE: 0x04
To decrement on step, execute command 0b00001000 or 0x08.
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
This will turn off the LED
This will put the LED in the middle point
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.
READ: 0x40 0xFE
The response id 0x40 = 64 DEC.
Do not forget to keep experimenting with the tool and enjoy the results.