1

I'm trying to understand how I can use a variable for specifying the pin number on an ADS1115. Generally one would read from the analog input by specifying something like this

import board
import busio
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn

chan0 = AnalogIn(ADS.ADS1115(i2c, address = 0x48), ADS.P0)

What I want to do is use a variable for the "P0" part of it.

I can make it into an object like

p_object = ADS.P0
chan0 = AnalogIn(ADS.ADS1115(i2c, address = 0x48), p_object)

But I this doesn't help when I'm init ing my class.

Here is the full code I'm working with. I'm wanting to swap out the P0 with 'pin_number', which I understand is probably not a number, but is it a string?

class Voltage_sensor:
    '''
    Given an ADS1115 address and a corresponding pin number 
    will read voltage values from an AC715
    '''
    def __init__(self, address, pin_number) -> None:
        self.address = int(address)
        self.pin_number = pin_number
        try:
            self.chan = AnalogIn(ADS.ADS1115(i2c, address = self.address), ADS.P0) #<- wanting to replace this with the pin number.
            self.chan = AnalogIn(ADS.ADS1115(i2c, address = self.address), ADS.self.pin_number) # something like this, but this doesn't work
            print(f"Adding ADS1115 at address {hex(self.address)}")

        except:
            print(f"Voltage Sensor not found at {hex(self.address)}")
itaki
  • 39
  • 3
  • It would just be `self.pin_number`, or even just `pin_number` since you're still in the `__init__()` method where you have that as a parameter. Putting `ADS.` in front of it makes no sense, the module knows nothing about the pin number you want to use. – jasonharper Jan 27 '22 at 00:05
  • @jasonharper I agree in most circumstances, but that doesn't work here. If I don't `import adafruit_ads1x15.ads1115 as ADS`, then the full like would look like `self.chan = AnalogIn(adafruit_ads1x15.ads1115.ADS1115(i2c, address = self.address), adafruit_ads1x15.ads1115.P0)`. Meaning that P0 is an object deep inside the adafruit module. I guess I don't know how to call an object using a variable. – itaki Jan 27 '22 at 00:20
  • You would indeed need to pass something like `ADS.P0` as the parameter when creating an instance of your class. It's the `ADS.self.pin_number` that I was objecting to - the module has no attribute named `self`. – jasonharper Jan 27 '22 at 00:44
  • Could you create a dict with keys as the pin number? e.g. `self.pin_dict = {'0' : ADS.P0, '1' : ADS.P1, ...}` then you can easily convert a integer into a pin object using `self.pin_dict[pin_number]` – Pepsi-Joe Jan 27 '22 at 01:02
  • @Pepsi-Joe This works! Not sure that it's the most pythonic answer, but I can make it work. Thanks! – itaki Jan 28 '22 at 02:35

1 Answers1

1

I ended up creating a dictionary of pins

self.pins = {'0' : ADS.P0, '1' : ADS.P1, '2' : ADS.P2, '3' : ADS.P3}

Then I just used an integer to call the corresponding pin.

itaki
  • 39
  • 3