Simple test

Ensure your device works with this simple test.

examples/as7341_simpletest.py
 1# SPDX-FileCopyrightText: 2020 Bryan Siepert, written for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3from time import sleep
 4import board
 5from adafruit_as7341 import AS7341
 6
 7i2c = board.I2C()  # uses board.SCL and board.SDA
 8# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
 9sensor = AS7341(i2c)
10
11
12def bar_graph(read_value):
13    scaled = int(read_value / 1000)
14    return "[%5d] " % read_value + (scaled * "*")
15
16
17while True:
18    print("F1 - 415nm/Violet  %s" % bar_graph(sensor.channel_415nm))
19    print("F2 - 445nm//Indigo %s" % bar_graph(sensor.channel_445nm))
20    print("F3 - 480nm//Blue   %s" % bar_graph(sensor.channel_480nm))
21    print("F4 - 515nm//Cyan   %s" % bar_graph(sensor.channel_515nm))
22    print("F5 - 555nm/Green   %s" % bar_graph(sensor.channel_555nm))
23    print("F6 - 590nm/Yellow  %s" % bar_graph(sensor.channel_590nm))
24    print("F7 - 630nm/Orange  %s" % bar_graph(sensor.channel_630nm))
25    print("F8 - 680nm/Red     %s" % bar_graph(sensor.channel_680nm))
26    print("Clear              %s" % bar_graph(sensor.channel_clear))
27    print("Near-IR (NIR)      %s" % bar_graph(sensor.channel_nir))
28    print("\n------------------------------------------------")
29    sleep(1)

LED test

Testing the LED

examples/as7341_led_test.py
 1# SPDX-FileCopyrightText: 2020 Bryan Siepert, written for Adafruit Industries
 2#
 3# SPDX-License-Identifier: MIT
 4from time import sleep
 5import board
 6import adafruit_as7341
 7
 8i2c = board.I2C()  # uses board.SCL and board.SDA
 9# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
10sensor = adafruit_as7341.AS7341(i2c)
11
12print("out of init!")
13print("Current current is")
14print(sensor.led_current)
15print("Setting current")
16sensor.led_current = 50
17print("enabling led")
18sensor.led = True
19sleep(0.5)
20print("disabling LED")
21sensor.led = False
22
23print("led status:", sensor.led)

Flicker Detection

Showing how to use flicker detection

examples/as7341_flicker_detection.py
 1# SPDX-FileCopyrightText: 2020 Bryan Siepert, written for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3from time import sleep
 4import board
 5from adafruit_as7341 import AS7341
 6
 7i2c = board.I2C()  # uses board.SCL and board.SDA
 8# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
 9sensor = AS7341(i2c)
10sensor.flicker_detection_enabled = True
11
12while True:
13    flicker_detected = sensor.flicker_detected
14    if flicker_detected:
15        print("Detected a %d Hz flicker" % flicker_detected)
16
17    sleep(0.1)

Batched Readings Example

Example in how to get all the channel readings at the same time

examples/as7341_batched_readings.py
 1# SPDX-FileCopyrightText: 2020 Bryan Siepert, written for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3from time import sleep
 4import board
 5from adafruit_as7341 import AS7341
 6
 7i2c = board.I2C()  # uses board.SCL and board.SDA
 8# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
 9sensor = AS7341(i2c)
10
11
12def bar_graph(read_value):
13    scaled = int(read_value / 1000)
14    return "[%5d] " % read_value + (scaled * "*")
15
16
17while True:
18    sensor_channels = sensor.all_channels
19    print("F1 - 415nm/Violet  %s" % bar_graph(sensor_channels[0]))
20    print("F2 - 445nm//Indigo %s" % bar_graph(sensor_channels[1]))
21    print("F3 - 480nm//Blue   %s" % bar_graph(sensor_channels[2]))
22    print("F4 - 515nm//Cyan   %s" % bar_graph(sensor_channels[3]))
23    print("F5 - 555nm/Green   %s" % bar_graph(sensor_channels[4]))
24    print("F6 - 590nm/Yellow  %s" % bar_graph(sensor_channels[5]))
25    print("F7 - 630nm/Orange  %s" % bar_graph(sensor_channels[6]))
26    print("F8 - 680nm/Red     %s" % bar_graph(sensor_channels[7]))
27    print("\n------------------------------------------------")
28
29    sleep(1)