1

I'm new to Python and currently working on a project on my Pi 3 mod b. I use an Adafruit ADC1015 to convert analogue signal. However, even if i have the code to get some volt measurments, i get an error of " AttributeError: 'int' object has no attribute 'readADCSingleEnded'".

To explain that, the python script i'm trying to run is the following:

#!/usr/bin/python
import time, signal, sys
from Adafruit_ADS1x15 import ADS1x15

def signal_handler(signal, frame):
    print 'You pressed Ctrl+C!'
    sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)

ADS1015 = 0x00  
ADS1115 = 0x01  

gain = 4096  # +/- 4.096V


sps = 250  # 250 samples per second

# Initialise the ADC using the default mode (use default I2C address)
# Set this to ADS1015 or ADS1115 depending on the ADC you are using!
adc = ADS1015(ic=ADS1015)

# Read channel 0 in single-ended mode using the settings above
volts=adc.readADCSingleEnded(0, gain, sps) / 1000

# To read channel 3 in single-ended mode, +/- 1.024V, 860 sps use:
# volts = adc.readADCSingleEnded(3, 1024, 860)

print "%.6f" % (volts)

The "ADS1x15" file we import contains the following code related to the error:

# Constructor
def __init__(self, address=0x48, ic=__IC_ADS1015, debug=False):
# Depending on if you have an old or a new Raspberry Pi, you
# may need to change the I2C bus.  Older Pis use SMBus 0,
# whereas new Pis use SMBus 1.  If you see an error like:
# 'Error accessing 0x48: Check your I2C address '
# change the SMBus number in the initializer below!
self.i2c = Adafruit_I2C(address)
self.address = address
self.debug = debug
# Make sure the IC specified is valid
if ((ic < self.__IC_ADS1015) | (ic > self.__IC_ADS1115)):
  if (self.debug):
    print "ADS1x15: Invalid IC specfied: %h" % ic
  return -1
else:
  self.ic = ic

# Set pga value, so that getLastConversionResult() can use it,
# any function that accepts a pga value must update this.
self.pga = 6144    


 def readADCSingleEnded(self, channel=0, pga=6144, sps=250):
"Gets a single-ended ADC reading from the specified channel in mV. \
The sample rate for this mode (single-shot) can be used to lower the noise \
(low sps) or to lower the power consumption (high sps) by duty cycling, \
see datasheet page 14 for more info. \
The pga must be given in mV, see page 13 for the supported values."

# With invalid channel return -1
if (channel > 3):
  if (self.debug):
    print "ADS1x15: Invalid channel specified: %d" % channel
  return -1

# Disable comparator, Non-latching, Alert/Rdy active low
# traditional comparator, single-shot mode
config = self.__ADS1015_REG_CONFIG_CQUE_NONE    | \
         self.__ADS1015_REG_CONFIG_CLAT_NONLAT  | \
         self.__ADS1015_REG_CONFIG_CPOL_ACTVLOW | \
         self.__ADS1015_REG_CONFIG_CMODE_TRAD   | \
         self.__ADS1015_REG_CONFIG_MODE_SINGLE    

# Set sample per seconds, defaults to 250sps
# If sps is in the dictionary (defined in init) it returns the value of the constant
# othewise it returns the value for 250sps. This saves a lot of if/elif/else code!
if (self.ic == self.__IC_ADS1015):
  config |= self.spsADS1015.setdefault(sps, self.__ADS1015_REG_CONFIG_DR_1600SPS)
else:
  if ( (sps not in self.spsADS1115) & self.debug):    
print "ADS1x15: Invalid pga specified: %d, using 6144mV" % sps     
  config |= self.spsADS1115.setdefault(sps, self.__ADS1115_REG_CONFIG_DR_250SPS)

# Set PGA/voltage range, defaults to +-6.144V
if ( (pga not in self.pgaADS1x15) & self.debug):      
  print "ADS1x15: Invalid pga specified: %d, using 6144mV" % sps     
config |= self.pgaADS1x15.setdefault(pga, self.__ADS1015_REG_CONFIG_PGA_6_144V)
self.pga = pga

# Set the channel to be converted
if channel == 3:
  config |= self.__ADS1015_REG_CONFIG_MUX_SINGLE_3
elif channel == 2:
  config |= self.__ADS1015_REG_CONFIG_MUX_SINGLE_2
elif channel == 1:
  config |= self.__ADS1015_REG_CONFIG_MUX_SINGLE_1
else:
  config |= self.__ADS1015_REG_CONFIG_MUX_SINGLE_0

# Set 'start single-conversion' bit
config |= self.__ADS1015_REG_CONFIG_OS_SINGLE

# Write config register to the ADC
bytes = [(config >> 8) & 0xFF, config & 0xFF]
self.i2c.writeList(self.__ADS1015_REG_POINTER_CONFIG, bytes)

# Wait for the ADC conversion to complete
# The minimum delay depends on the sps: delay >= 1/sps
# We add 0.1ms to be sure
delay = 1.0/sps+0.0001
time.sleep(delay)

# Read the conversion results
result = self.i2c.readList(self.__ADS1015_REG_POINTER_CONVERT, 2)
if (self.ic == self.__IC_ADS1015):
    # Shift right 4 bits for the 12-bit ADS1015 and convert to mV
    return ( ((result[0] << 8) | (result[1] & 0xFF)) >> 4 )*pga/2048.0
else:
# Return a mV value for the ADS1115
# (Take signed values into account as well)
val = (result[0] << 8) | (result[1])
if val > 0x7FFF:
  return (val - 0xFFFF)*pga/32768.0
else:
  return ( (result[0] << 8) | (result[1]) )*pga/32768.0

I believed this would run smmothly, as it is a part something that is related to the ADC, but i haven't managed to solve this problem, even if i tried a lot.

1 Answer 1

1

Found it. Line

adc = ADS1015(ic=ADS1015)

Should be

adc = ADS1x15(ic=ADS1015)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.