I'm trying to control a digital potentiometer using RPi's SPI interface but for some reason it is failing, and I can't figure out why.
I have successfully controled the potentiometer by manually setting the CS, SCK and DataOut pins in the chip, but I'm trying now to use the SPI interface that automates all this process.
This is the code I have written:
import spidev import time spi = spidev.SpiDev() spi.open(0, 0) spi.max_speed_hz = 100000 spi.cshigh = False #spi.loop = True #spi.threewire = True spi.mode = 0b00 try: for i in range(0, 50, 10): print "Write " + str(i) + " - ", resp = spi.xfer2([0x00, i]) print "Read: " + str(len(resp)) + " " + str(resp) + " " + str(resp) time.sleep(1) finally: spi.close()
The former program shows this output when running but the LED doesn't light:
$ sudo python DigitalPot-SPI.py Write 0 - Read: 2 0 0 Write 10 - Read: 2 0 0 Write 20 - Read: 2 0 0 Write 30 - Read: 2 0 0 Write 40 - Read: 2 0 0
And this is how my circuit looks like:
In the MCP4131's datasheet this is the pin order I'm using (and I've used it successfully controlling the potentiometer manually):
When I uncomment the line that reads
spi.threewire = True the following happens to the console output (I wanted to uncomment it as the MCP4131 multiplexes its inputs and outputs):
$ sudo python DigitalPot-SPI.py Write 0 - Read: 2 0 0 Traceback (most recent call last): File "DigitalPot-SPI.py", line 17, in <module> time.sleep(1) IOError: [Errno 22] Invalid argument
Any help will be appreciated.