Problems with interrupt handler for TSL2561 on ESP8266

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
udmicpy
Posts: 13
Joined: Tue Aug 15, 2017 3:47 pm

Problems with interrupt handler for TSL2561 on ESP8266

Post by udmicpy » Fri Aug 25, 2017 8:10 pm

Dear all,

I'm using the tsl2561.py module on an ESP8266-12E with 4 MByte.
As docu I used this
http://micropython-tsl2561.readthedocs. ... l2561.html
and this
https://media.readthedocs.org/pdf/micro ... sl2561.pdf

This is my code:

Code: Select all

import micropython
import machine
from time import sleep
import sys
import tsl2561
from machine import I2C, Pin
i2c = I2C(-1,Pin(5), Pin(4))
sensor = tsl2561.TSL2561(i2c)
sensor.active(True)
sleep(1)
sensor.threshold(-1,0,0)

micropython.alloc_emergency_exception_buf(100)

def my_Interrupt_Handler(pin):
    global stopLoop
    if stopLoop == False:
        stopLoop = True
        print("interrupt")
        #global sensor
        #sensor.threshold(-1,0,0)
        #sensor.interrupt(False)

# 0 == D3
pin = Pin(0, Pin.IN, Pin.PULL_UP)
pin.irq(trigger = machine.Pin.IRQ_FALLING, handler = my_Interrupt_Handler)
print("Pin val=" + str(pin.value()))

stopLoop = False

def IRQ_demo(sensor,pin,stopLoop):
    # activate interrupt
    sensor.threshold(0,10,20)
    sleep(1)
    loop = 0
    while 1:
        loop = loop + 1
        val = pin.value()
        print("loop = " + str(loop) + " light = " + str(sensor.read()) + " pin val=" + str(val))
        sleep(1)
        if stopLoop == True:
           print('got interrupt from TLS2561')
           stopLoop = False
           # reset and set threshold again
           sensor.threshold(-1,0,0)
           sensor.interrupt(False)
           sensor.threshold(0,10,20)

IRQ_demo(sensor,pin,stopLoop)

The problem is, that I get an interrupt only once.
After I got an interrupt I try to reset interrupt by calling sensor.threshold(-1,0,0,). Then the pin has the value 1 again.
But in the moment when I set threshold to 0,10,20 the pin value changes directly to 0 again.

Furthermore I'm not sure if the interrupt should arise when the light value is between min and max or if it is lower than min and higher than max.
But I tried both scenarios.

What am I doing wrong?
Also the short example code from the second document mentioned above does not work for me.

Hope that anybody can help me.

Thanks a lot in advance.

Best regards, Uwe
Last edited by udmicpy on Fri Sep 01, 2017 9:12 am, edited 3 times in total.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Problems with interrupt handler for TLS2561 on ESP8266

Post by deshipu » Fri Aug 25, 2017 9:58 pm

You need to clear the interrupt with the "interrupt" method.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Problems with interrupt handler for TLS2561 on ESP8266

Post by deshipu » Fri Aug 25, 2017 9:59 pm

udmicpy wrote: Also the short example code from the second document mentioned above does not work for me.
What doesn't work exactly?

udmicpy
Posts: 13
Joined: Tue Aug 15, 2017 3:47 pm

Q

Post by udmicpy » Sat Aug 26, 2017 9:27 am

Hello Radomir,

thanks for your immediate response.

The problem is, that I get an interrupt only once.
After I got an interrupt I try to reset interrupt by calling sensor.threshold(-1,0,0,). Then the pin has the value 1 again.
But in the moment when I set threshold to 0,10,20 the pin value changes directly to 0 again.

And I'm already using the interrupt-function.

Code: Select all

        if stopLoop == True:
           print('got interrupt from TLS2561')
           stopLoop = False
           # reset and set threshold again
           sensor.threshold(-1,0,0)
           sensor.interrupt(False)
           sensor.threshold(0,10,20)
So I'm not sure if I'm making a mistake or if I do not understand te concept of how to use IRQs with TLS2561 or how to use IRQs with micropython at all.

Hope you can give me a hint.

Best regards, Uwe

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Problems with interrupt handler for TLS2561 on ESP8266

Post by deshipu » Sat Aug 26, 2017 12:52 pm

Don't do

Code: Select all

sensor.threshold(-1,0,0)
, it makes no sense,

Code: Select all

sensor.interrupt(False)
should clear it. Just clear the interrupt and it should be fine. You are passing "0" as the "cycles" parameter, so you will get an interrupt after every conversion, no matter what thresholds you set. You probably want some higher number there if you actually want to use thresholds.

I'm re-reading the datasheet at https://cdn-shop.adafruit.com/datasheets/TSL2561.pdf and the interrupts are described at page 21 of it.

udmicpy
Posts: 13
Joined: Tue Aug 15, 2017 3:47 pm

Re: Problems with interrupt handler for TLS2561 on ESP8266

Post by udmicpy » Sat Aug 26, 2017 3:03 pm

Hello Radomir,

thanks for your immediate response.
I'm now using another cycle value for the threshold --> sensor.threshold(2,20,50)
But all what I tried did not work.
My IRQ Pin has initially the value 1. After the first interrupt occured it has value 0.
But when I call sensor.interrupt(False) it stays low (0).
I have to call sensor.threshold(-1,0,0) to get it up to high (1) again.
But when I call again sensor.threshold(2,20,50) to reactivate the interrupt I immediately get again the interrupt.
Even if I shut the sensor so that it does not get light anymore.

Perhaps you can take again a deeper look.
Is there all OK with the interrupt handler itself?
In another example with RTC and IRQ I've seen something like this:

Code: Select all

        state = machine.disable_irq()
        machine.enable_irq(state)
Thanks a lot in advance.

Best regards, Uwe

udmicpy
Posts: 13
Joined: Tue Aug 15, 2017 3:47 pm

Re: Problems with interrupt handler for TSL2561 on ESP8266

Post by udmicpy » Fri Sep 08, 2017 5:54 am

Hello again,

could anybody again try to help me.
For me the problem isn't already solved.

Help is appreciated.

Thanks a lot in advance.

Best regards, Uwe

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Problems with interrupt handler for TLS2561 on ESP8266

Post by pythoncoder » Fri Sep 08, 2017 8:30 am

udmicpy wrote:...
But when I call again sensor.threshold(2,20,50) to reactivate the interrupt I immediately get again the interrupt.
Even if I shut the sensor so that it does not get light anymore...
I may be speaking out of turn here as I have no experience with the device. But from my reading of the data this looks like correct behaviour: an interrupt occurs if the light level is outside of the range (20-50 in your case).

I'd try setting the thresholds wide apart so that normal lighting does not trigger an interrupt. Then you should get one only if you put the sensor in the dark.

It's generally best not to have print statements in interrupt handlers. I'd do something like have the interrupt handler increment a global count which you can display in your main loop to detect how many interrupts you've received. See https://docs.micropython.org/en/latest/ ... rules.html.

Another approach is to test the ESP8266 interrupt handler by faking the device interrupt. Have a timer periodically toggle a pin and, in place of the sensor, link that pin to the pin you're using for an interrupt. Check that your handler runs each time the pin goes low. If it does, you can then turn your attention to the sensor.

Debugging interrupt driven systems can be tricky and it requires a patient methodical approach. Good luck ;)
Peter Hinch
Index to my micropython libraries.

udmicpy
Posts: 13
Joined: Tue Aug 15, 2017 3:47 pm

Re: Problems with interrupt handler for TSL2561 on ESP8266

Post by udmicpy » Fri Sep 22, 2017 11:31 am

Hello Peter,

thank you very much for you hints concerning ISR debugging.
But I really do not understand why after the first received interrupt the PIN state can't be set to it's old value by using the function which is intended to do this. And the value of the threshold should be OK for testing cause it's OK for testing until I get the first interrrupt. So in my opinion that's a prove that it's not completely wrong.

But anyhow - I'll try to give my best.

Best regards, Uwe

Post Reply