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
8sensor = AS7341(i2c)
9
10
11def bar_graph(read_value):
12 scaled = int(read_value / 1000)
13 return "[%5d] " % read_value + (scaled * "*")
14
15
16while True:
17
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
9sensor = adafruit_as7341.AS7341(i2c)
10
11print("out of init!")
12print("Current current is")
13print(sensor.led_current)
14print("Setting current")
15sensor.led_current = 50
16print("enabling led")
17sensor.led = True
18sleep(0.5)
19print("disabling LED")
20sensor.led = False
21
22print("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
8sensor = AS7341(i2c)
9sensor.flicker_detection_enabled = True
10
11while True:
12
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
8sensor = AS7341(i2c)
9
10
11def bar_graph(read_value):
12 scaled = int(read_value / 1000)
13 return "[%5d] " % read_value + (scaled * "*")
14
15
16while True:
17 sensor_channels = sensor.all_channels
18 print("F1 - 415nm/Violet %s" % bar_graph(sensor_channels[0]))
19 print("F2 - 445nm//Indigo %s" % bar_graph(sensor_channels[1]))
20 print("F3 - 480nm//Blue %s" % bar_graph(sensor_channels[2]))
21 print("F4 - 515nm//Cyan %s" % bar_graph(sensor_channels[3]))
22 print("F5 - 555nm/Green %s" % bar_graph(sensor_channels[4]))
23 print("F6 - 590nm/Yellow %s" % bar_graph(sensor_channels[5]))
24 print("F7 - 630nm/Orange %s" % bar_graph(sensor_channels[6]))
25 print("F8 - 680nm/Red %s" % bar_graph(sensor_channels[7]))
26 print("\n------------------------------------------------")
27
28 sleep(1)