Skip to content

Commit 93230fa

Browse files
committed
interrupt pin example
1 parent 8a4bd09 commit 93230fa

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

docs/examples.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ Output color data over serial for use with webserial page
1616
:caption: examples/opt4048_webserial.py
1717
:linenos:
1818

19+
Interrupt Pin example
20+
---------------------
21+
22+
Wait for the interrupt pin to be triggered, then read data.
23+
24+
.. literalinclude:: ../examples/opt4048_interruptpin.py
25+
:caption: examples/opt4048_interruptpin.py
26+
:linenos:
27+
1928
Full test
2029
---------
2130

examples/opt4048_interruptpin.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025 Tim C for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
A basic interrupt pin demo for using the OPT4048 tristimulus XYZ color sensor
5+
6+
This example waits for the interrupt pin to be triggered,
7+
then reads and displays the sensor values.
8+
"""
9+
10+
import time
11+
12+
import board
13+
import countio
14+
from digitalio import Pull
15+
16+
from adafruit_opt4048 import (
17+
OPT4048,
18+
ConversionTime,
19+
Mode,
20+
Range,
21+
)
22+
23+
i2c = board.I2C() # uses board.SCL and board.SDA
24+
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
25+
sensor = OPT4048(i2c)
26+
27+
sensor.range = Range.AUTO
28+
sensor.conversion_time = ConversionTime.TIME_100MS
29+
sensor.mode = Mode.CONTINUOUS
30+
31+
# counter that will track the pulses on the interrupt pin
32+
pin_counter = countio.Counter(board.D5, edge=countio.Edge.RISE, pull=Pull.UP)
33+
34+
last_read_time = 0
35+
while True:
36+
try:
37+
if pin_counter.count > 0:
38+
pin_counter.reset()
39+
x, y, lux = sensor.get_cie()
40+
print(f"CIE x:{x}, y:{y}, lux: {lux}", end=" ")
41+
print(f"K: {sensor.calculate_color_temperature(x, y)}", end=" ")
42+
print(f"Read Delay: {time.monotonic() - last_read_time} sec")
43+
last_read_time = time.monotonic()
44+
45+
except RuntimeError:
46+
# error reading data
47+
pass

0 commit comments

Comments
 (0)