1

I use an ads1115 to get values from an analogic sensor to a raspberry pi 3 but I'm having a hard time figuring out how to get them from python.

I use the SMBus library to get the i2c values but I can't find how to actually get the analog 0 AIN0 channel values. I found the i2c address for the ads1115 to be 0x48 but I can't find the address for the AIN0 channel, thus I don't have the second argument for the command smbus.read_byte_data(0x48, ???) and I tried some addresses like 0x00, 0x01 but it always gives me the same values even tho the sensor values should be changing.Here's my code :

from smbus import SMBus
import RPi.GPIO as GPIO
import time

def main():
    i2cbus = SMBus(1)
    i2caddress = 0x48
    value = i2cbus.read_byte_data(0x48, """dont't know""")

if __name__ == "__main__":
    main()
0andriy
  • 4,183
  • 1
  • 24
  • 37

1 Answers1

1

To read digital data from ADS1115 ADC IC, you must connect analog signals to A[0]~A[3] pins of ADS1115. After ADS1115 ADC module converts the analog signal on pin A[0]~A[3] to digital form, you should read the value through the I2C interface. Check out the references section to review the case study with the ADS1115 module.

The following command call reads 2 bytes of data starting from the 0x00 register address of the I2C device whose slave address is 0x48.

# read_i2c_block_data(i2c_address, register, length, force=None)
# i2c_address -> ADS1115  I2C slave address
# register    -> ADS1115 conversion register address
# length      -> desired block length
value = i2cbus.read_i2c_block_data(0x48, 0x00, 2)
References
Sercan
  • 4,739
  • 3
  • 17
  • 36
  • Linux kernel has already the driver. – 0andriy Jan 11 '22 at 20:11
  • @0andriy The kernel drivers are not easy to access from python. The use of a python library makes things much easier. – PMF Jan 13 '22 at 14:29
  • @PMF, `libiio` has Python bindings, what stops you to use it? https://analogdevicesinc.github.io/libiio/v0.20/python/index.html (I believe this is available in your distro, e.g. Debian _python3-libiio/unstable,unstable 0.23-2 all Python bindings for libiio_) Besides the fact that with it you may access to 500+ drivers in the kernel at once. – 0andriy Jan 13 '22 at 16:20
  • @0andriy _That_ is a possible approach. I was referring to writing the bindings yourself. Whether the bindings internally use a kernel driver or do their own thing (just on top of a raw i2c device) is irrelevant for the user, but one should use a binding if it exists. – PMF Jan 13 '22 at 17:39
  • @PMF, there are a lot of disadvantages by **not** using in-kernel drivers, one of which is what we may clearly see in this question. I guess at the end of the day we are on the same page. – 0andriy Jan 13 '22 at 17:41