from machine import Pin, I2C
import utime
# 1. Setup I2C Bus & Interrupt Pin (GP14)
i2c = I2C(0, sda=Pin(4), scl=Pin(5))
int_pin = Pin(14, Pin.IN, Pin.PULL_UP)
# 2. Configure Sensor Threshold Registers (< 8 cm)
i2c.writeto_mem(0x39, 0x89, b'\xB4') # High threshold = 180
# 3. Interrupt Service Routine (ISR Callback Function)
def proximity_isr(pin):
# Executed instantly when INT pin drops LOW!
val = i2c.readfrom_mem(0x39, 0x1C, 1)[0]
print("⚠️ ALERT: Object Detected!", val)
# 4. Attach IRQ handler on FALLING edge (HIGH -> LOW)
int_pin.irq(handler=proximity_isr, trigger=Pin.IRQ_FALLING)
# 5. Main application loop runs smoothly with NO I2C lag
while True:
# RP2040 CPU is free to run other tasks...
utime.sleep_ms(1000)