Hello;
I am at my wit's end. I am trying to create a generic Python s/w application for the Raspberry Pi (via SPI) that will control the AD5204 or AD5206digital Pot. I have been successful using the Arduino Microcontroller (using their example code), but I need it to be able to run solely using the Raspberry Pi. Has anyone out there been successful with this?? Below is a snippet of my code. It is "generic" because it also controls (successfully) an MCP4131 Digital Pot, but I MUCH prefer the AD5206/AD5204 Digital Pot. Any-who, here is what I have so far, and the function/method in question is the one titled: def write_pot_5204( channelValue, resValue ).
I have also tried to play with it using Java (incorporating Java's relatively new pi4j libraries, but to no avail), so if anyone has been successful using Java to control it, that would be welcome also.
Circuit-wise, I use it in the same configuration as the Arduino set-up listed here:
Thank you ahead of time...
Be Well,
Craig
The code (below):
import spidev
import time
import os
import sys # For use of Command Line Arguments...
import RPi.GPIO as GPIO
import wiringpi2 as wiringpi
wiringpi.wiringPiSetupGpio()
slaveSelectPin_CE0 = 8 # E.g., the CE "0" Pin
slaveSelectPin_CE1 = 7 # E.g., the CE "1" Pin
misoGPIO = 10 # E.g., The "MOSI" Pin
wiringpi.pinMode( slaveSelectPin_CE0, 1 ) # Sets GPIO CE "0" to OUTPUT
wiringpi.pinMode( misoGPIO, 1 ) # Sets GPIO MOSI to OUTPUT
spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz = 976000
os.system( "clear" )
nbrCmdLineArgs = len( sys.argv )
nameOfThisFile = sys.argv[0]
channelValue = int( sys.argv[1] )
resValue = int( sys.argv[2] )
potType = int( sys.argv[3] )
# --------------------------------------------
#
#
def writeInfoScreenToUser( ):
infoMsg = "Your Format for this Script should be as follows:\n\"%s CHANNEL# ResistanceVal potType\"\n\n";
print ( infoMsg % nameOfThisFile )
# --------------------------------------------
#
#
def write_pot_4131( input ):
print ( "Value = ", input )
msb = input >> 8
lsb = input & 0xFF
spi.xfer([msb, lsb])
# --------------------------------------------
#
#
def write_pot_5204( channelValue, resValue ):
print ( "channelValue = %d" % channelValue )
print ( "resValue = %d" % resValue )
wiringpi.digitalWrite( slaveSelectPin_CE0, 0 ) # Sets Port CE0 to 0 (0V, off)
# resValue.to_bytes((resValue.bit_length() + 7) / 8, 'big') or b'\0'
resValue = resValue >> 8
finalVlalue = channelValue & resValue
# wiringpi.digitalWrite( slaveSelectPin_CE0, channelValue )
# wiringpi.digitalWrite( slaveSelectPin_CE0, resValue )
wiringpi.digitalWrite( misoGPIO, 0 )
wiringpi.digitalWrite( misoGPIO, finalVlalue )
wiringpi.digitalWrite( misoGPIO, 1 )
# spi.xfer([ channelValue, resValue ])
wiringpi.digitalWrite( slaveSelectPin_CE0, 1 ) # Sets Port CE0 to 1 (5V, ON)
writeInfoScreenToUser()
if nbrCmdLineArgs >= 4:
print ( "Number of arguments: %s arguments" % nbrCmdLineArgs )
print ( 'Argument List: %s' % str(sys.argv))
print ( "\n" )
print ("Inputs properly received... (Channel: %d, Resistance: %d, DigiPot Type: %d)" %
( channelValue, resValue, potType ))
else:
errorMsg = """
ERROR!!!
you MUST have at LEAST 3 command Line variables!!
the FORMAT should be as FOLLOWS:
<thisPythonFile> CHANNEL RESISTANCE_VALUE POT_TYPE (should be either "4131" or "5204/5206")"""
print ( errorMsg )
sys.exit( 1 )
if potType == 4131:
write_pot_4131( resValue )
else:
write_pot_5204( channelValue, resValue )