|
1 | 1 | # SPDX-FileCopyrightText: Copyright (c) 2026 Tim Cocks for Adafruit Industries |
2 | 2 | # SPDX-License-Identifier: MIT |
3 | 3 |
|
4 | | -"""Interrupt example for TCS3430 XYZ Tristimulus Color Sensor. |
5 | | -Sets a high threshold -- shine a flashlight on the sensor to trigger! |
6 | | -
|
7 | | -Connect INT to board.D8. The Adafruit breakout has an open-drain inverter |
8 | | -on INT, so active-HIGH at MCU. Configure as input with pull-up.""" |
| 4 | +"""Basic test for TCS3430 XYZ Tristimulus Color Sensor. |
| 5 | +Polls the ALS interrupt flag to know when new data is ready.""" |
9 | 6 |
|
10 | 7 | import time |
11 | 8 |
|
12 | 9 | import board |
13 | | -import digitalio |
14 | 10 |
|
15 | 11 | from adafruit_tcs3430 import TCS3430, ALSGain, InterruptPersistence |
16 | 12 |
|
17 | | -INT_PIN = board.D8 |
18 | | - |
19 | 13 | i2c = board.I2C() |
20 | 14 | tcs = TCS3430(i2c) |
21 | 15 |
|
22 | | -print("TCS3430 Interrupt Test") |
23 | | -print(f"Connect sensor INT to {INT_PIN}") |
24 | | -print() |
| 16 | +print("TCS3430 Basic Test") |
25 | 17 | print("TCS3430 found!") |
26 | 18 |
|
27 | | -int_pin = digitalio.DigitalInOut(INT_PIN) |
28 | | -int_pin.direction = digitalio.Direction.INPUT |
29 | | -int_pin.pull = digitalio.Pull.UP |
30 | | - |
31 | | -# Setup sensor |
32 | | -tcs.als_gain = ALSGain.GAIN_16X |
33 | | -tcs.integration_time = 100.0 |
34 | | -tcs.interrupt_persistence = InterruptPersistence.CYCLES_3 # require 3 consecutive |
35 | | - |
36 | | -# Read current ambient to set threshold above it |
37 | | -time.sleep(0.2) |
38 | | -x, y, z, ir1 = tcs.channels |
39 | | - |
40 | | -# Thresholds compare against CH0 (Z channel) |
41 | | -threshold = z * 2 # 2x above current ambient |
42 | | -if threshold < z + 200: |
43 | | - threshold = z + 200 |
44 | | -if threshold > 65000: |
45 | | - threshold = 65000 |
46 | | - |
47 | | -print(f"Current ambient Z: {z}") |
48 | | -print(f"Interrupt threshold (high): {threshold}") |
49 | | -print() |
50 | | -print("Shine a flashlight on the sensor to trigger!") |
51 | | -print("INT pin idle = LOW, active = HIGH (breakout inverter)") |
52 | | -print() |
| 19 | +# --- Tweak these settings for your environment --- |
| 20 | +tcs.als_gain = ALSGain.GAIN_64X # 1X, 4X, 16X, 64X, or 128X |
| 21 | +tcs.integration_time = 100.0 # 2.78ms to 711ms |
53 | 22 |
|
54 | | -# Window: low=0, high=threshold |
55 | | -# Interrupt fires when Z exceeds threshold (outside window) |
56 | | -tcs.als_threshold_low = 0 |
57 | | -tcs.als_threshold_high = threshold |
58 | | -tcs.interrupt_clear_on_read = False |
59 | | - |
60 | | -# Clean start |
| 23 | +# Enable ALS interrupt so we can poll AINT for data ready |
61 | 24 | tcs.als_interrupt_enabled = True |
62 | | -tcs.als_enabled = False |
| 25 | +tcs.interrupt_persistence = InterruptPersistence.EVERY |
63 | 26 | tcs.clear_als_interrupt() |
64 | | -tcs.als_enabled = True |
65 | | - |
66 | | -print(f"INT pin: {'HIGH' if int_pin.value else 'LOW'}") |
67 | | - |
68 | | -last_print = time.monotonic() |
69 | 27 |
|
70 | 28 | while True: |
71 | | - # Check INT pin state |
72 | | - if int_pin.value: |
73 | | - # Read channels to see what triggered it |
| 29 | + # Wait for new data |
| 30 | + if tcs.als_interrupt: |
74 | 31 | x, y, z, ir1 = tcs.channels |
75 | | - print(f"*** INTERRUPT! *** Z={z} Y={y} X={x} IR1={ir1}") |
76 | | - |
77 | | - # Clear and wait for pin to go idle before checking again |
78 | | - tcs.als_enabled = False |
| 32 | + print(f"X: {x} Y: {y} Z: {z} IR1: {ir1}") |
79 | 33 | tcs.clear_als_interrupt() |
80 | | - tcs.als_enabled = True |
81 | | - |
82 | | - # Wait for INT to settle back to idle |
83 | | - time.sleep(0.5) |
84 | | - continue |
85 | 34 |
|
86 | | - # Periodic ambient reading every 2 seconds |
87 | | - now = time.monotonic() |
88 | | - if now - last_print > 2.0: |
89 | | - last_print = now |
90 | | - x, y, z, ir1 = tcs.channels |
91 | | - print(f"Z={z} Y={y} (waiting...) INT={'HIGH' if int_pin.value else 'LOW'}") |
| 35 | + time.sleep(1.0) |
0 commit comments