Skip to content

Commit 1a7c0ec

Browse files
committed
simplify simpletest. move interrupt reg test to new file, rename interrupt pin test.
1 parent 8870750 commit 1a7c0ec

File tree

3 files changed

+106
-80
lines changed

3 files changed

+106
-80
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 Tim Cocks for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
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."""
9+
10+
import time
11+
12+
import board
13+
import digitalio
14+
15+
from adafruit_tcs3430 import TCS3430, ALSGain, InterruptPersistence
16+
17+
INT_PIN = board.D8
18+
19+
i2c = board.I2C()
20+
tcs = TCS3430(i2c)
21+
22+
print("TCS3430 Interrupt Test")
23+
print(f"Connect sensor INT to {INT_PIN}")
24+
print()
25+
print("TCS3430 found!")
26+
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()
53+
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
61+
tcs.als_interrupt_enabled = True
62+
tcs.als_enabled = False
63+
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+
70+
while True:
71+
# Check INT pin state
72+
if int_pin.value:
73+
# Read channels to see what triggered it
74+
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
79+
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+
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'}")

examples/tcs3430_interrupt_test.py

Lines changed: 12 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,35 @@
11
# SPDX-FileCopyrightText: Copyright (c) 2026 Tim Cocks for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

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."""
96

107
import time
118

129
import board
13-
import digitalio
1410

1511
from adafruit_tcs3430 import TCS3430, ALSGain, InterruptPersistence
1612

17-
INT_PIN = board.D8
18-
1913
i2c = board.I2C()
2014
tcs = TCS3430(i2c)
2115

22-
print("TCS3430 Interrupt Test")
23-
print(f"Connect sensor INT to {INT_PIN}")
24-
print()
16+
print("TCS3430 Basic Test")
2517
print("TCS3430 found!")
2618

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
5322

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
6124
tcs.als_interrupt_enabled = True
62-
tcs.als_enabled = False
25+
tcs.interrupt_persistence = InterruptPersistence.EVERY
6326
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()
6927

7028
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:
7431
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}")
7933
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
8534

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)

examples/tcs3430_simpletest.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# SPDX-FileCopyrightText: Copyright (c) 2026 Tim Cocks for Adafruit Industries
22
# SPDX-License-Identifier: MIT
33

4-
"""Basic test for TCS3430 XYZ Tristimulus Color Sensor.
5-
Polls the ALS interrupt flag to know when new data is ready."""
4+
"""Basic test for TCS3430 XYZ Tristimulus Color Sensor."""
65

76
import time
87

@@ -20,16 +19,8 @@
2019
tcs.als_gain = ALSGain.GAIN_64X # 1X, 4X, 16X, 64X, or 128X
2120
tcs.integration_time = 100.0 # 2.78ms to 711ms
2221

23-
# Enable ALS interrupt so we can poll AINT for data ready
24-
tcs.als_interrupt_enabled = True
25-
tcs.interrupt_persistence = InterruptPersistence.EVERY
26-
tcs.clear_als_interrupt()
27-
2822
while True:
29-
# Wait for new data
30-
if tcs.als_interrupt:
31-
x, y, z, ir1 = tcs.channels
32-
print(f"X: {x} Y: {y} Z: {z} IR1: {ir1}")
33-
tcs.clear_als_interrupt()
23+
x, y, z, ir1 = tcs.channels
24+
print(f"X: {x} Y: {y} Z: {z} IR1: {ir1}")
3425

3526
time.sleep(1.0)

0 commit comments

Comments
 (0)