Erwan Bertrand
Erwan Bertrand

Reputation: 11

How can I get i2c values from a Rapsberry with Python?

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()

Upvotes: 1

Views: 1277

Answers (1)

Sercan
Sercan

Reputation: 5091

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

Upvotes: 1

Related Questions