ADS1015 alert pin triggering

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
User avatar
ghayne
Posts: 42
Joined: Sat Jun 08, 2019 9:31 am
Location: Cwmllynfell, Wales

ADS1015 alert pin triggering

Post by ghayne » Tue Sep 24, 2019 10:28 am

Hi,

I have a photodiode connected to an ads1015 and would like to have the alert pin signal on every transition of the threshold value (low to high.
Somehow I cannot get it working using roberthh's ads1x15.py driver. Pin 12 is connected to the ads1015 alert pin
Test code: the photodiode is measuring a flashing LED with values low =550 high =650

Code: Select all

from machine import I2C, Pin, Timer
from ads1x15 import ADS1015
import urequests
import utime

i2c = I2C(scl=Pin(5), sda=Pin(4), freq=100000)
adc = ADS1015(i2c, 72, 3)
adc.alert_start(4, 0, threshold=600)

elapsed = 0.0
alert = Pin(12, Pin.IN)


def pulse(p):
    print('Pulse')

alert.irq(trigger=Pin.IRQ_FALLING, handler=pulse)

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: ADS1015 alert pin triggering

Post by Roberthh » Tue Sep 24, 2019 6:27 pm

Looking into the code, you could try:

adc.alert_start(4, 0, threshold_high=600)

I will set up a test tomorrow. Today I'm a little bit exhausted from physical work.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: ADS1015 alert pin triggering

Post by Roberthh » Wed Sep 25, 2019 11:37 am

I made a test and that works. test code:

Code: Select all

from machine import I2C, Pin, Timer, idle
from ads1x15 import ADS1115
# import urequests
import utime

i2c = I2C(scl=Pin(5), sda=Pin(4), freq=100000)
adc = ADS1115(i2c, 72, 3)
adc.alert_start(4, 0, threshold_high=2000)

elapsed = 0.0
alert = Pin(12, Pin.IN)


def pulse(p):
    global adc
    a = adc.alert_read()
    print('Pulse', a)

alert.irq(trigger=Pin.IRQ_FALLING, handler=pulse)

while True:
    idle()

User avatar
ghayne
Posts: 42
Joined: Sat Jun 08, 2019 9:31 am
Location: Cwmllynfell, Wales

Re: ADS1015 alert pin triggering

Post by ghayne » Wed Sep 25, 2019 1:00 pm

Thank you Roberthh, not tried it yet but will this work with the ads1015 I am using as well?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: ADS1015 alert pin triggering

Post by Roberthh » Wed Sep 25, 2019 1:14 pm

I assume so. The chips only differ by them resolution 12/16 bits and the conversion speed. I do not have an ADS1015 here. Otherwiase I would have tested that.

User avatar
ghayne
Posts: 42
Joined: Sat Jun 08, 2019 9:31 am
Location: Cwmllynfell, Wales

Re: ADS1015 alert pin triggering

Post by ghayne » Wed Sep 25, 2019 2:17 pm

It is working sort of, the adc.alert_read() is giving readings of low 7800 high 8600 roughly (way above the maximum possible for the ads1015). I set the threshold_high to 8000 but the alert pin is interrupting every 1ms.
The alert pin is obviously triggering for every sample above the threshold, not what I really wanted. I want only the first transition from low to high repeatedly so that I can measure the time between pulses.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: ADS1015 alert pin triggering

Post by Roberthh » Wed Sep 25, 2019 3:11 pm

That is possible with a change to the driver. in alert_start(), change _CLAT_LATCH to _CLAT_NONLAT. Should be line 180. I changed the function also to allow for a low threshold. Code snippet below:

Code: Select all

    def alert_start(self, rate=4, channel1=0, channel2=None,
                    threshold_high=0x4000, threshold_low=0):
        """Start continuous measurement, set ALERT pin on threshold."""
        self._write_register(_REGISTER_LOWTHRESH, threshold_low)
        self._write_register(_REGISTER_HITHRESH, threshold_high)
        self._write_register(_REGISTER_CONFIG, _CQUE_1CONV | _CLAT_NONLAT |
                             _CPOL_ACTVLOW | _CMODE_TRAD | _RATES[rate] |
                             _MODE_CONTIN | _GAINS[self.gain] |
                             _CHANNELS[(channel1, channel2)])

User avatar
ghayne
Posts: 42
Joined: Sat Jun 08, 2019 9:31 am
Location: Cwmllynfell, Wales

Re: ADS1015 alert pin triggering

Post by ghayne » Wed Sep 25, 2019 5:09 pm

Thank you Roberthh. I made the changes you suggested but it still is not working:

Code: Select all

from machine import I2C, Pin, Timer, idle
from ads1x15 import ADS1115
# import urequests
import utime

i2c = I2C(scl=Pin(5), sda=Pin(4), freq=100000)
adc = ADS1115(i2c, 72, 3)
adc.alert_start(4, 0, threshold_high=8600)

elapsed = 0.0
alert = Pin(12, Pin.IN)


def pulse(p):
    global adc
    a = adc.alert_read()
    #if (a<9000):
    print('Pulse', a, utime.ticks_ms())

alert.irq(trigger=Pin.IRQ_FALLING, handler=pulse)

while True:
    idle()
    

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: ADS1015 alert pin triggering

Post by Roberthh » Wed Sep 25, 2019 6:24 pm

You have to use the ADS1015 class. But there are inconsistencies in the parameter naming for threshold. That's the drawback that I never had a device for testing. I should get one.
But for the ADS1015 the driver scales the values. So you must only use +/-2048 as the range.

Edit: I have updated the repository, but I am not sure that it works. I have to get an ADS1015 unit.

User avatar
ghayne
Posts: 42
Joined: Sat Jun 08, 2019 9:31 am
Location: Cwmllynfell, Wales

Re: ADS1015 alert pin triggering

Post by ghayne » Wed Sep 25, 2019 7:03 pm

Roberthh wrote:
Wed Sep 25, 2019 6:24 pm
Edit: I have updated the repository, but I am not sure that it works. I have to get an ADS1015 unit.
Robert, if I can't get it working you can have mine :)

Post Reply