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
 4
 5import board
 6
 7import adafruit_as7341
 8
 9i2c = board.I2C()  # uses board.SCL and board.SDA
10# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
11sensor = adafruit_as7341.AS7341(i2c)
12
13sensor.gain = adafruit_as7341.Gain.GAIN_0_5X  # Update the gain of the sensor
14
15
16def bar_graph(read_value):
17    scaled = int(read_value / 1000)
18    return f"[{read_value:5d}] " + (scaled * "*")
19
20
21while True:
22    print(f"F1 - 415nm/Violet  {bar_graph(sensor.channel_415nm)}")
23    print(f"F2 - 445nm//Indigo {bar_graph(sensor.channel_445nm)}")
24    print(f"F3 - 480nm//Blue   {bar_graph(sensor.channel_480nm)}")
25    print(f"F4 - 515nm//Cyan   {bar_graph(sensor.channel_515nm)}")
26    print(f"F5 - 555nm/Green   {bar_graph(sensor.channel_555nm)}")
27    print(f"F6 - 590nm/Yellow  {bar_graph(sensor.channel_590nm)}")
28    print(f"F7 - 630nm/Orange  {bar_graph(sensor.channel_630nm)}")
29    print(f"F8 - 680nm/Red     {bar_graph(sensor.channel_680nm)}")
30    print(f"Clear              {bar_graph(sensor.channel_clear)}")
31    print(f"Near-IR (NIR)      {bar_graph(sensor.channel_nir)}")
32    print("\n------------------------------------------------")
33    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
 5
 6import board
 7
 8import adafruit_as7341
 9
10i2c = board.I2C()  # uses board.SCL and board.SDA
11# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
12sensor = adafruit_as7341.AS7341(i2c)
13
14print("out of init!")
15print("Current current is")
16print(sensor.led_current)
17print("Setting current")
18sensor.led_current = 50
19print("enabling led")
20sensor.led = True
21sleep(0.5)
22print("disabling LED")
23sensor.led = False
24
25print("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
 4
 5import board
 6
 7from adafruit_as7341 import AS7341
 8
 9i2c = board.I2C()  # uses board.SCL and board.SDA
10# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
11sensor = AS7341(i2c)
12sensor.flicker_detection_enabled = True
13
14while True:
15    flicker_detected = sensor.flicker_detected
16    if flicker_detected:
17        print(f"Detected a {flicker_detected:d} Hz flicker")
18
19    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
 4
 5import board
 6
 7from adafruit_as7341 import AS7341
 8
 9i2c = board.I2C()  # uses board.SCL and board.SDA
10# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
11sensor = AS7341(i2c)
12
13
14def bar_graph(read_value):
15    scaled = int(read_value / 1000)
16    return f"[{read_value:5d}] " + (scaled * "*")
17
18
19while True:
20    sensor_channels = sensor.all_channels
21    print(f"F1 - 415nm/Violet  {bar_graph(sensor_channels[0])}")
22    print(f"F2 - 445nm//Indigo {bar_graph(sensor_channels[1])}")
23    print(f"F3 - 480nm//Blue   {bar_graph(sensor_channels[2])}")
24    print(f"F4 - 515nm//Cyan   {bar_graph(sensor_channels[3])}")
25    print(f"F5 - 555nm/Green   {bar_graph(sensor_channels[4])}")
26    print(f"F6 - 590nm/Yellow  {bar_graph(sensor_channels[5])}")
27    print(f"F7 - 630nm/Orange  {bar_graph(sensor_channels[6])}")
28    print(f"F8 - 680nm/Red     {bar_graph(sensor_channels[7])}")
29    print("\n------------------------------------------------")
30
31    sleep(1)

DisplayIO Simpletest

This is a simple test for boards with built-in display.

examples/as3741_displayio_simpletest.py
 1# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
 2# SPDX-FileCopyrightText: 2024 Jose D. Montoya
 3#
 4# SPDX-License-Identifier: MIT
 5
 6import time
 7
 8import board
 9from adafruit_display_text.bitmap_label import Label
10from displayio import Group
11from terminalio import FONT
12
13from adafruit_as7341 import AS7341
14
15# Simple demo of using the built-in display.
16# create a main_group to hold anything we want to show on the display.
17main_group = Group()
18# Initialize I2C bus and sensor.
19i2c = board.I2C()  # uses board.SCL and board.SDA
20sensor = AS7341(i2c)
21sensor.flicker_detection_enabled = True
22
23# Create Label(s) to show the readings. If you have a very small
24# display you may need to change to scale=1.
25display_output_label = Label(FONT, text="", scale=2)
26
27# place the label(s) in the middle of the screen with anchored positioning
28display_output_label.anchor_point = (0, 0)
29display_output_label.anchored_position = (
30    4,
31    board.DISPLAY.height // 2 - 60,
32)
33
34# add the label(s) to the main_group
35main_group.append(display_output_label)
36
37# set the main_group as the root_group of the built-in DISPLAY
38board.DISPLAY.root_group = main_group
39
40# begin main loop
41while True:
42    # update the text of the label(s) to show the sensor readings
43    flicker_detected = sensor.flicker_detected
44    if flicker_detected:
45        display_output_label.text = f"Detected a {flicker_detected} Hz flicker"
46    else:
47        display_output_label.text = "No flicker detected"
48    # wait for a bit
49    time.sleep(0.5)