How to pass I2C addresses to Adafruit CircuitPython code? (Running ADS1115)

梦想的初衷 提交于 2021-01-28 11:42:42

问题


I'm trying to run two Adafruit ADS1115s off of one Raspberry Pi, using two I2C addresses (0x48, 0x49). The address for each device can be set by tying the ADDR pin high (0x49) or leaving it floating (default, 0x48). I've confirmed that each board works when the address is set to 0x48, and running "i2cdetect 1" confirms that both boards are connected at the correct addresses.

I can successfully run this sample code

My question is this: How do I get the code to read from I2C address 0x49 instead of 0x48? I can't find documentation anywhere. Please advise.


回答1:


Since there is a Python library the rules of Python language are applied, in particular OOP with class inheritance. That said, The class ADS1115 is inherited from ADS1x15, which in its turn has __init__() method (in OOP constructor) defined as follows:

def __init__(self, address=ADS1x15_DEFAULT_ADDRESS, i2c=None, **kwargs):

which means that it knows about at least two positional arguments with names address and i2c with default values ADS1x15_DEFAULT_ADDRESS and None respectively. So, you need in your code redefine them, i.e. instead of ads = ADS.ADS1115(i2c) use

ads = ADS.ADS1115(address=0x48, i2c=i2c)

For the second put there 0x49.




回答2:


Aha!
ads1 = ADS.ADS1115(i2c, address=0x49)

Source: https://github.com/adafruit/Adafruit_CircuitPython_ADS1x15/issues/20



来源:https://stackoverflow.com/questions/60102070/how-to-pass-i2c-addresses-to-adafruit-circuitpython-code-running-ads1115

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!