Simple test

Ensure your device works with this simple test.

examples/as7341_simpletest.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# SPDX-FileCopyrightText: 2020 Bryan Siepert, written for Adafruit Industries
# SPDX-License-Identifier: MIT
from time import sleep
import board
from adafruit_as7341 import AS7341

i2c = board.I2C()  # uses board.SCL and board.SDA
sensor = AS7341(i2c)


def bar_graph(read_value):
    scaled = int(read_value / 1000)
    return "[%5d] " % read_value + (scaled * "*")


while True:

    print("F1 - 415nm/Violet  %s" % bar_graph(sensor.channel_415nm))
    print("F2 - 445nm//Indigo %s" % bar_graph(sensor.channel_445nm))
    print("F3 - 480nm//Blue   %s" % bar_graph(sensor.channel_480nm))
    print("F4 - 515nm//Cyan   %s" % bar_graph(sensor.channel_515nm))
    print("F5 - 555nm/Green   %s" % bar_graph(sensor.channel_555nm))
    print("F6 - 590nm/Yellow  %s" % bar_graph(sensor.channel_590nm))
    print("F7 - 630nm/Orange  %s" % bar_graph(sensor.channel_630nm))
    print("F8 - 680nm/Red     %s" % bar_graph(sensor.channel_680nm))
    print("Clear              %s" % bar_graph(sensor.channel_clear))
    print("Near-IR (NIR)      %s" % bar_graph(sensor.channel_nir))
    print("\n------------------------------------------------")
    sleep(1)

LED test

Testing the LED

examples/as7341_led_test.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# SPDX-FileCopyrightText: 2020 Bryan Siepert, written for Adafruit Industries
#
# SPDX-License-Identifier: MIT
from time import sleep
import board
import adafruit_as7341

i2c = board.I2C()  # uses board.SCL and board.SDA
sensor = adafruit_as7341.AS7341(i2c)

print("out of init!")
print("Current current is")
print(sensor.led_current)
print("Setting current")
sensor.led_current = 50
print("enabling led")
sensor.led = True
sleep(0.5)
print("disabling LED")
sensor.led = False

print("led status:", sensor.led)

Flicker Detection

Showing how to use flicker detection

examples/as7341_flicker_detection.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# SPDX-FileCopyrightText: 2020 Bryan Siepert, written for Adafruit Industries
# SPDX-License-Identifier: MIT
from time import sleep
import board
from adafruit_as7341 import AS7341

i2c = board.I2C()  # uses board.SCL and board.SDA
sensor = AS7341(i2c)
sensor.flicker_detection_enabled = True

while True:

    flicker_detected = sensor.flicker_detected
    if flicker_detected:
        print("Detected a %d Hz flicker" % flicker_detected)

    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
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# SPDX-FileCopyrightText: 2020 Bryan Siepert, written for Adafruit Industries
# SPDX-License-Identifier: MIT
from time import sleep
import board
from adafruit_as7341 import AS7341

i2c = board.I2C()  # uses board.SCL and board.SDA
sensor = AS7341(i2c)


def bar_graph(read_value):
    scaled = int(read_value / 1000)
    return "[%5d] " % read_value + (scaled * "*")


while True:
    sensor_channels = sensor.all_channels
    print("F1 - 415nm/Violet  %s" % bar_graph(sensor_channels[0]))
    print("F2 - 445nm//Indigo %s" % bar_graph(sensor_channels[1]))
    print("F3 - 480nm//Blue   %s" % bar_graph(sensor_channels[2]))
    print("F4 - 515nm//Cyan   %s" % bar_graph(sensor_channels[3]))
    print("F5 - 555nm/Green   %s" % bar_graph(sensor_channels[4]))
    print("F6 - 590nm/Yellow  %s" % bar_graph(sensor_channels[5]))
    print("F7 - 630nm/Orange  %s" % bar_graph(sensor_channels[6]))
    print("F8 - 680nm/Red     %s" % bar_graph(sensor_channels[7]))
    print("\n------------------------------------------------")

    sleep(1)