I have a problem operating the AD5272 using Python 3 on Raspberry Pi 3 B+. AD5272 is the digitally controlled rheostat with I2C interface (my part has resistance 20 kOhm).
The problem is following:
The resistance between A and W terminals doesn't change whatever position I'm trying to set-up and stays around 10KOhm. (By default when AD5272 turns on, is sets the half of full resistance there). When I'm reading the resistance from RDAC - is reads zero resistance.
Here is my code:
#!/usr/bin/python3
import smbus2
import time, os
class AD527x ():
# command bits which are dependant on I2C address of device
def __init__ (self, bus=1, address=0x2E, resistance = 20000, positions = 1023, reset = False):
self.bus = smbus2.SMBus(bus)
self.positions = positions
self.address = address
self.resistance = resistance
def write_position (self, position):
# sets the rheostat to certain position
# if wiper position is higher than maximal
if position > self.positions:
position = self.positions
# approximate resistance of rheostat
resistance = self.resistance*position/self.positions
# for AD5274 needs to be shifted for 2 digits left
if self.positions == 256:
position = position << 2
print ("Binary representation of position for RDAC : " + bin (position))
# Writing position is sneding 2 bytes one by one
# MSB Data: 0 0 C3 C2 C1 C0 D9 D8
# For writing command bytes: C3 = 0; C2 = 0; C1 = 0; C0 = 1
# 0 0 0 0 0 1 ? ?
# ? ? - MSB of 10-digit binary representation of wiper position between 0 and 1023
# two first positions : D9_8 = (position & 0b1100000000) >> 8
MSB = (1 << 3) | ((position & 0b1100000000) >> 8)
print ("MSB : " + bin(MSB))
# take last 8 bits
LSB = position & 0b11111111
print ("LSB : " + bin(LSB))
print ("All Bytes : " + bin((MSB << 8) +LSB))
print ("Value : " + str(resistance))
self.bus.write_i2c_block_data(self.address, 0, [MSB, LSB])
self.read_position()
def read_position (self):
# reads current position of rheostat
# prepare the circuit to send data
# MSB Data: 0 0 C3 C2 C1 C0 D9 D8
# For reading command bytes: C3 = 0; C2 = 0; C1 = 1; C0 = 0
# 0 0 0 0 1 0 ? ?
# ? ? - Doen't matter - Just use zeros
# LSB - doesn't matter, just using 0b00000000
MSB = 0b00001000
LSB = 0b00000000
self.bus.write_i2c_block_data(self.address, 0, [MSB, LSB])
#read 2 bytes from RDAC
a = self.bus.read_byte(self.address)
time.sleep (0.005)
b = self.bus.read_byte(self.address)
value = ((a << 8) | b )
print (value)
# take 10 lats bits only
value = value & 0b1111111111
print (value)
def main():
device = AD527x()
device.write_position(1023)
if __name__ == "__main__":
main()
The output is:
Binary representation of position for RDAC : 0b1111111111
MSB : 0b1011
LSB : 0b11111111
All Bytes : 0b101111111111
Value : 20000.0
4096
0
Computer : Rapberry Pi 3, Model B+
OS : Raspbian 9
Python Version: 3.5.3
I2C package : smbus2
Part Number: AD5272BRMZ-20
Datasheet: http://www.farnell.com/datasheets/1706490.pdf
What am I doing wrong and how to fix it?
The wiring of part was triple checked. The reads from RDAC is always the same, whatever value I'm trying to write there. I tried both commands:
self.bus.write_i2c_block_data(self.address, 0, [MSB, LSB])
and
self.bus.write_byte (self.address, MSB)
self.bus.write_byte (self.address, LSB)
Result was always the same: Resistance doesn't change. Resistance was checked with external Ohmmeter device.
Please, help!
I just hit this same issue. I haven't looked at interfacing with Python yet but this may help nonetheless.
In my circuit, the AD5272 I2C is addressed with GND and I have terminal A connected to VDD (5V) and W in series with a 100K resistor to GND.
First I ensured the device was operational through the Linux driver, the modules should already be builtin. As root:
Measuring the voltage from W to GND gave ~5V at 0 and ~2.5V at 1023.
I then repeated with a logic analyser connected to the I2C bus. The commands for writes were:
This should hopefully provide a hint, re-read the sections in the datasheet on "write protection" and "control register description". In my setup I only want to use the digital interface and not stored memory for control, so I replicated from user space with a slight alteration: