I wrote some code for version 1.10 of micropython for ESP32 and the light sensor TSL2561.
In the first code-snippet I get ISR calls in an endless loop. This example 1 works.
The seconds code-snippet is trying to wake up the ESP32 from deepsleep with such an interrupt.
But it will not wake up. The TSL2561 is active-low.
I also tried other ways by using these irq-definitions:
esp32.wake_on_ext1(pins = [TSL_Pin], level = Pin.WAKE_LOW)
esp32.wake_on_ext0(pin = TSL_Pin, level = Pin.WAKE_LOW)
In a similar example with a PIR motion sensor which is active high both examples (IRQ and wake-up) worked directly (see example code 3 and 4)
Perhaps anybody can give me a hint.
BTW: I've also tried to get it work with external pull up resistors. Same problem.
Thanks a lot in advance.
Best regards, Uwe
Example 1 - IRQ test with tsl2561 - WORKS
Code: Select all
# TSL2561 Specs
# https://cdn-shop.adafruit.com/datasheets/TSL2561.pdf
# perhaps this new library could help but it's for Circuit Python
# https://github.com/adafruit/Adafruit_CircuitPython_TSL2561/blob/master/adafruit_tsl2561.py
# the library below seems to be deprecated
# https://github.com/adafruit/micropython-adafruit-tsl2561
# docs: https://micropython-tsl2561.readthedocs.io/en/latest/
import tsl2561
from machine import I2C, Pin
i2c = I2C(sda=Pin(21), scl=Pin(22))
sensor = tsl2561.TSL2561(i2c)
PinNumber = 14
gain = 1
thresholdLow = 200
thresholdHigh = 2000
sensor.gain(gain) # allowed values: [1|16] 1 for bright / 16 for low light
sensor.integration_time(402) # allowed values: ([0|13|101|402])
print(sensor.read(False,True))
import time
light = False
def handle_light_interrupt(p):
global light
light = True
print('IRQ handler: pin change ' ,p)
print('IRQ handler: pin value ' ,p.value())
def enable_interrupt(gain,low,high):
sensor.interrupt(False)
sensor.threshold(-1, 0 , 0)
if gain == 16:
low = low *10
high = high * 10
sensor.threshold(1, low, high)
print('enable_interrupt - threshold(1, ' + str(low) + ', ' + str(high) + ' )')
int_pin = Pin(PinNumber, Pin.IN, Pin.PULL_UP)
# int_pin = Pin(PinNumber, Pin.IN)
int_pin.irq(handler=handle_light_interrupt, trigger=Pin.IRQ_FALLING)
enable_interrupt(gain,thresholdLow,thresholdHigh)
while True:
broadband,infrared = sensor.read(False,True)
print('loop: pin value = ' + str(int_pin.value()) + ', broadband = ' + str(broadband))
time.sleep(1)
if light:
print('light change detected!')
if broadband < thresholdLow:
print('low light ' + str(broadband))
else:
print('bright light ' + str(broadband))
enable_interrupt(gain,thresholdLow,thresholdHigh)
time.sleep(2)
light = False
Code: Select all
import time
import tsl2561
import machine
from machine import I2C, Pin
i2c = I2C(sda=Pin(21), scl=Pin(22))
sensor = tsl2561.TSL2561(i2c)
gain = 1
thresholdLow = 200
thresholdHigh = 2000
sensor.gain(gain) # allowed values: [1|16]
sensor.integration_time(402) # allowed values: ([0|13|101|402])
print(sensor.read(False,True))
def enable_interrupt(gain,low,high):
sensor.interrupt(False)
sensor.threshold(-1, 0 , 0)
if gain == 16:
low = low *10
high = high * 10
sensor.threshold(1, low, high)
print('enable_interrupt - threshold(1, ' + str(low) + ', ' + str(high) + ' )')
from machine import Pin
import esp32
PinNumber = 14
TSL_Pin = Pin(PinNumber, mode = Pin.IN, pull = Pin.PULL_UP)
TSL_Pin.value()
esp32.wake_on_ext1(pins = [TSL_Pin], level = esp32.WAKEUP_ALL_LOW)
enable_interrupt(gain,thresholdLow,thresholdHigh)
TSL_Pin.value()
machine.deepsleep(20*1000)
Code: Select all
# PIR interrupt demo
from machine import Pin
from time import sleep
motion = False
def handle_interrupt(v):
global motion
motion = True
PinNumber = 27
pir = Pin(PinNumber, Pin.IN)
pir.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)
while True:
if motion:
print('Motion detected!')
motion = False
Code: Select all
from machine import Pin, TouchPad
import time
import esp32
PinNumber = 27
PIR_Pin = Pin(PinNumber, mode = Pin.IN, pull = Pin.PULL_DOWN)
PIR_Pin.value()
esp32.wake_on_ext1(pins = [PIR_Pin], level = Pin.WAKE_HIGH)
import machine
machine.deepsleep(10*1000)