chandrawi / ADS1x15-ADC

Python library used for ADS1x15 analog to digital converter
MIT License
20 stars 6 forks source link

'ADS1115' object has no attribute '_ADS1115__requestADC' #1

Closed kautzz closed 2 years ago

kautzz commented 2 years ago

Hey there, thanks for the lib. Not sure if it's a bug or I am doing it wrong... I'm having an issue reading differential signals on the ADS1115. I was under the impression that it should be possible to read two separate differential signals.

For example: AIN0 - battery ground, AIN1 - battery positive, AIN2 - photodiode ground, AIN3 - photodiode signal But while running the following code:

diff_adc_1 = ads.readADC_Differential_0_1()
battery_voltage = ads.toVoltage(diff_adc_1)

diff_adc_2 = ads.readADC_Differential_2_3()
light_intensity = ads.toVoltage(diff_adc_2)

I get the error message:

  File "/home/pi/c14garden/sensors.py", line 53, in read
    diff_adc_2 = ads.readADC_Differential_2_3()
  File "/home/pi/.local/lib/python3.9/site-packages/ADS1x15/ADS1x15.py", line 412, in readADC_Differential_2_3
    self.requestADC_Differential_2_3()
  File "/home/pi/.local/lib/python3.9/site-packages/ADS1x15/ADS1x15.py", line 408, in requestADC_Differential_2_3
    self.__requestADC(0x3000)
AttributeError: 'ADS1115' object has no attribute '_ADS1115__requestADC'

I am a bit confused because ads.readADC_Differential_0_1() works fine on its own and the code of ads.readADC_Differential_2_3() is pretty much identical to it.

Can you deny or confirm whether what I'm trying to do is possible? Tried ads.setInput(0) as well as ads.setInput(3) but it makes no difference.

chandrawi commented 2 years ago

Hey kautzz,

Quick answer, it is bug in the library.

Can you deny or confirm whether what I'm trying to do is possible? Tried ads.setInput(0) as well as ads.setInput(3) but it makes no difference.

It is possible to set input multiplexer this way.

ads.readADC_Differential_0_3(), ads.readADC_Differential_1_3(), ads.readADC_Differential_2_3() methods in ADS1115 class are calling the private method __requestADC() which derived from ADS1x15 class. I think we can fix this by changing __requestADC() name to _requestADC() (no longer private), so it can be called from ADS1115.

chandrawi commented 2 years ago

I have fix the problem. Now, you can install new wheel package from latest commit. Go to ADS1x15-ADC folder after you clone this repository then run this command.

pip3 install dist/ADS1x15_ADC-1.1.2-py3-none-any.whl
kautzz commented 2 years ago

Those sneaky underscores 😁 Thank you for taking the time to fix it. I'll give it another try tonight.

chandrawi commented 2 years ago

I just update the library to version 1.2.0 and upload to pypi. Just install using pip

pip3 install ADS1x15_ADC

Please check this version too! Let me know if your issue fixed in this version.

kautzz commented 2 years ago

Thanks for the update and nice job on the documentation! With the fix my code executes but I'm running into another issue.

Somehow the PGA seems to be set to 1 and the multiplexer set to 0 automatically while doing readADC_Differential_2_3(). So that the following pseudo-code:

import ADS1x15
ads = ADS1x15.ADS1115(1)

while True:
    print(date)

    ads.setInput(3)
    ads.setGain(0)

    print("Gain before read: " + str(ads.getGain()))
    print("Multiplexer before read: " + str(ads.getInput()))

    diff_adc_2 = ads.readADC_Differential_2_3()
    light_intensity = ads.toVoltage(diff_adc_2)

    print("Gain after read: " + str(ads.getGain()))
    print("Multiplexer after read: " + str(ads.getInput()))

    print(results)
    time.sleep(10)

Returns:

2022-01-21 11:08:50.027930
Gain before read: 0
Multiplexer before read: 3
Gain after read: 1
Multiplexer after read: 0
{"sensor": "ADS1115", "raw_adc_2": 30237, "light_intensity": 1.8898701742606892}

2022-01-21 11:09:01.051390
Gain before read: 0
Multiplexer before read: 3
Gain after read: 1
Multiplexer after read: 0
{"sensor": "ADS1115", "raw_adc_2": 30335, "light_intensity": 1.8959953611865596}

Full code here

The reported ADC value is incorrect and does not change based on the actual analog output of the light sensor. My first guess was that I made a mistake hooking up the components that would lead to the device restarting with default settings on every read... but then again the default gain would be 0 not 1.

chandrawi commented 2 years ago

I check it is another bug. It is happen all the time, bug always found after previous bug fixed! 😁 I just update again the library to version 1.2.1

After installing latest version. I try this code

import time
import ADS1x15
ads = ADS1x15.ADS1115(1)

ads.setGain(0)

while True :

    print("Gain before read: " + str(ads.getGain()))
    print("Multiplexer before read: " + str(ads.getInput()))

    diff_adc_2 = ads.readADC_Differential_1_3()
    light_intensity = ads.toVoltage(diff_adc_2)

    print("Gain after read: " + str(ads.getGain()))
    print("Multiplexer after read: " + str(ads.getInput()))

    print([diff_adc_2, light_intensity])
    print()
    time.sleep(5)

It is return

Gain before read: 0
Multiplexer before read: 2
Gain after read: 0
Multiplexer after read: 3
[-22, -0.004125125888851589]

Gain before read: 0
Multiplexer before read: 3
Gain after read: 0
Multiplexer after read: 3
[-1, -0.00018750572222052677]

Let me know again if this fix. So finally we can close this issue

kautzz commented 2 years ago

🙌 Nicely done! Working as I thought it would. Can I buy you a beer or something?

chandrawi commented 2 years ago

No need dude, I just need ⭐⭐⭐⭐⭐