Simple test

Ensure your device works with this simple test.

examples/as7343_simpletest.py
 1# SPDX-FileCopyrightText: Copyright (c) 2026 Tim Cocks for Adafruit Industries
 2#
 3# SPDX-License-Identifier: MIT
 4import time
 5
 6import board
 7
 8from adafruit_as7343 import AS7343
 9
10i2c = board.I2C()
11sensor = AS7343(i2c)
12
13CHANNEL_LABELS = [
14    "FZ (450nm blue)",
15    "FY (555nm yellow-green)",
16    "FXL (600nm orange)",
17    "NIR (855nm near-IR)",
18    "VIS_TL_0 (clear top-left, cycle 1)",
19    "VIS_BR_0 (clear btm-right, cycle 1)",
20    "F2 (425nm violet-blue)",
21    "F3 (475nm blue-cyan)",
22    "F4 (515nm green)",
23    "F6 (640nm red)",
24    "VIS_TL_1 (clear top-left, cycle 2)",
25    "VIS_BR_1 (clear btm-right, cycle 2)",
26    "F1 (405nm violet)",
27    "F7 (690nm deep red)",
28    "F8 (745nm near-IR edge)",
29    "F5 (550nm green-yellow)",
30    "VIS_TL_2 (clear top-left, cycle 3)",
31    "VIS_BR_2 (clear btm-right, cycle 3)",
32]
33
34while True:
35    readings = sensor.all_channels
36    print("--- AS7343 Channel Readings ---")
37    for label, value in zip(CHANNEL_LABELS, readings):
38        print(f"  {label}: {value}")
39    print()
40    time.sleep(1)

Full test

Displays chip information, configuration, and continuous spectral readings.

examples/as7343_fulltest.py
  1# SPDX-FileCopyrightText: Copyright (c) 2026 Tim Cocks for Adafruit Industries
  2#
  3# SPDX-License-Identifier: MIT
  4"""
  5Full test example for AS7343 14-Channel Multi-Spectral Sensor.
  6
  7Displays chip information, configuration, and continuous spectral readings.
  8
  9Written by Tim Cocks with assistance from Claude Code for Adafruit Industries.
 10"""
 11
 12import time
 13
 14import board
 15
 16from adafruit_as7343 import AS7343, Channel, Gain, SmuxMode
 17
 18#  Initialise sensor
 19i2c = board.I2C()
 20
 21print("AS7343 Full Test")
 22print("================")
 23
 24try:
 25    sensor = AS7343(i2c)
 26except RuntimeError as e:
 27    print(f"Couldn't find AS7343 chip: {e}")
 28    raise SystemExit
 29
 30print("AS7343 found!")
 31
 32#  Chip information
 33print("\n--- Chip Information ---")
 34print(f"Part ID:     0x{sensor.part_id:02X}")
 35print(f"Revision ID: 0x{sensor.revision_id:02X}")
 36print(f"Aux ID:      0x{sensor.aux_id:02X}")
 37
 38#  Spectral engine configuration
 39print("\n--- Spectral Configuration ---")
 40
 41sensor.gain = Gain.X64
 42gain_names = {
 43    Gain.X0_5: "0.5x",
 44    Gain.X1: "1x",
 45    Gain.X2: "2x",
 46    Gain.X4: "4x",
 47    Gain.X8: "8x",
 48    Gain.X16: "16x",
 49    Gain.X32: "32x",
 50    Gain.X64: "64x",
 51    Gain.X128: "128x",
 52    Gain.X256: "256x",
 53    Gain.X512: "512x",
 54    Gain.X1024: "1024x",
 55    Gain.X2048: "2048x",
 56}
 57print(f"Gain: {gain_names.get(sensor.gain, 'Unknown')}")
 58
 59sensor.atime = 29
 60print(f"ATIME: {sensor.atime}")
 61
 62sensor.astep = 599
 63print(f"ASTEP: {sensor.astep}")
 64
 65print(f"Integration Time: {sensor.integration_time_ms:.2f} ms")
 66
 67#  SMUX configuration
 68print("\n--- SMUX Configuration ---")
 69
 70sensor.smux_mode = SmuxMode.CH18
 71smux_names = {
 72    SmuxMode.CH6: "6 channels",
 73    SmuxMode.CH12: "12 channels (2 cycles)",
 74    SmuxMode.CH18: "18 channels (3 cycles)",
 75}
 76print(f"Mode: {smux_names.get(sensor.smux_mode, 'Unknown')}")
 77
 78#  Wait time configuration
 79print("\n--- Wait Time Configuration ---")
 80
 81sensor.wtime = 100
 82print(f"Wait Time: {sensor.wtime} (disabled by default)")
 83
 84#  Interrupt configuration
 85print("\n--- Interrupt Configuration ---")
 86
 87sensor.persistence = 4
 88print(f"Persistence: {sensor.persistence}")
 89
 90# NOTE: Threshold channel register r/w works but has no observed effect
 91# on threshold comparison - comparison is always on CH0 regardless.
 92sensor.threshold_channel = 0
 93print(f"Threshold Channel: {sensor.threshold_channel}")
 94
 95sensor.spectral_threshold_low = 100
 96print(f"Low Threshold: {sensor.spectral_threshold_low}")
 97
 98sensor.spectral_threshold_high = 60000
 99print(f"High Threshold: {sensor.spectral_threshold_high}")
100
101#  LED driver configuration
102print("\n--- LED Driver Configuration ---")
103
104sensor.led_current_ma = 20
105print(f"LED Current: {sensor.led_current_ma} mA")
106print("LED: Off (use sensor.led_enabled = True to turn on)")
107
108#  Continuous spectral readings
109print("\n--- Spectral Readings ---")
110print("Channel wavelengths: F1=405nm, F2=425nm, FZ=450nm, F3=475nm, F4=515nm, F5=550nm,")
111print("FY=555nm, FXL=600nm, F6=640nm, F7=690nm, F8=745nm, NIR=855nm\n")
112
113time.sleep(0.2)  # Let sensor stabilise
114
115while True:
116    try:
117        readings = sensor.all_channels
118    except TimeoutError:
119        print("Read failed!")
120        time.sleep(0.5)
121        continue
122
123    # Print in wavelength order for easier reading
124    print(
125        f"F1:{readings[Channel.F1]}"
126        f"\tF2:{readings[Channel.F2]}"
127        f"\tFZ:{readings[Channel.FZ]}"
128        f"\tF3:{readings[Channel.F3]}"
129        f"\tF4:{readings[Channel.F4]}"
130        f"\tF5:{readings[Channel.F5]}"
131        f"\tFY:{readings[Channel.FY]}"
132        f"\tFXL:{readings[Channel.FXL]}"
133        f"\tF6:{readings[Channel.F6]}"
134        f"\tF7:{readings[Channel.F7]}"
135        f"\tF8:{readings[Channel.F8]}"
136        f"\tNIR:{readings[Channel.NIR]}"
137    )
138
139    time.sleep(1.0)

Plotter Example

Plot spectral data with the CircuitPython web editor serial plotter.

examples/as7343_plotter.py
 1# SPDX-FileCopyrightText: Copyright (c) 2026 Tim Cocks for Adafruit Industries
 2#
 3# SPDX-License-Identifier: MIT
 4"""
 5Serial Plotter example for AS7343 14-Channel Multi-Spectral Sensor.
 6
 7Outputs spectral data in a CSV format suitable for CircuitPython
 8web-editor plotter. Channels are ordered by wavelength:
 9violet → blue → green → yellow → orange → red → NIR
10
11# Plotter data index reference:
12# 0: F1  (405nm)  | 1: F2  (425nm) - Violet/UV
13# 2: FZ  (450nm)  | 3: F3  (475nm) - Blue
14# 4: F4  (515nm)  | 5: F5  (550nm)  | 6: FY (555nm) - Green
15# 7: FXL (600nm) - Yellow/Orange
16# 8: F6  (640nm)  | 9: F7  (690nm) - Red
17# 10: F8 (745nm)  | 11: NIR (855nm) - Near-IR
18# 12: VIS_TL_0 (Clear/broadband)
19
20Written by Tim Cocks with assistance from Claude Code for Adafruit Industries.
21"""
22
23import time
24
25import board
26
27from adafruit_as7343 import AS7343, Channel, Gain, SmuxMode
28
29# Initialise sensor
30i2c = board.I2C()
31
32try:
33    sensor = AS7343(i2c)
34except RuntimeError as e:
35    print(f"AS7343 not found: {e}")
36    raise SystemExit
37
38sensor.gain = Gain.X64
39sensor.atime = 29
40sensor.astep = 599
41sensor.smux_mode = SmuxMode.CH18
42
43# Continuous plotter output
44while True:
45    try:
46        readings = sensor.all_channels
47    except TimeoutError:
48        time.sleep(0.5)
49        continue
50
51    # Output in wavelength order for spectrum visualisation.
52    print(f"{readings[Channel.F1]}", end=",")
53    print(f"{readings[Channel.F2]}", end=",")
54
55    # Blue (450–475 nm)
56    print(f"{readings[Channel.FZ]}", end=",")
57    print(f"{readings[Channel.F3]}", end=",")
58
59    # Green (515–555 nm)
60    print(f"{readings[Channel.F4]}", end=",")
61    print(f"{readings[Channel.F5]}", end=",")
62    print(f"{readings[Channel.FY]}", end=",")
63
64    # Yellow/Orange (600 nm)
65    print(f"{readings[Channel.FXL]}", end=",")
66
67    # Red (640–690 nm)
68    print(f"{readings[Channel.F6]}", end=",")
69    print(f"{readings[Channel.F7]}", end=",")
70
71    # Near-IR (745–855 nm)
72    print(f"{readings[Channel.F8]}", end=",")
73    print(f"{readings[Channel.NIR]}", end=",")
74
75    # Clear/broadband
76    print(f"{readings[Channel.VIS_TL_0]}")
77
78    time.sleep(1.0)

Flicker Detection

Demonstration of 100Mhz/120Mhz flicker detection.

examples/as7343_flicker_detection.py
 1# SPDX-FileCopyrightText: Copyright (c) 2026 Tim Cocks for Adafruit Industries
 2#
 3# SPDX-License-Identifier: MIT
 4"""
 5Flicker Detection example for AS7343 14-Channel Multi-Spectral Sensor.
 6
 7Enables the on-chip flicker detection engine and continuously reports
 8whether 100 Hz or 120 Hz mains flicker is detected in the ambient light.
 9
10Written by Tim Cocks with assistance from Claude Code for Adafruit Industries.
11"""
12
13import time
14
15import board
16
17from adafruit_as7343 import AS7343, FlickerFreq
18
19#  Initialise sensor
20i2c = board.I2C()
21
22print("AS7343 Flicker Detection Demo")
23
24try:
25    sensor = AS7343(i2c)
26except RuntimeError as e:
27    print(f"AS7343 not found: {e}")
28    raise SystemExit
29
30# Enable flicker detection
31sensor.flicker_detection_enabled = True
32print("Flicker detection enabled")
33print("Point sensor at light source...\n")
34
35#  Continuous flicker polling
36while True:
37    flicker = sensor.flicker_frequency
38    raw = sensor.flicker_status
39
40    if flicker == FlickerFreq.HZ100:
41        flicker_str = "100Hz"
42    elif flicker == FlickerFreq.HZ120:
43        flicker_str = "120Hz"
44    else:
45        flicker_str = "None"
46
47    print(f"Flicker: {flicker_str} (raw status: 0x{raw:02X} {bin(raw)})")
48
49    time.sleep(1.0)