### Install adafruit-circuitpython-as7341 system-wide Source: https://github.com/adafruit/adafruit_circuitpython_as7341/blob/main/docs/index.rst Install the driver system-wide, which may be required in some cases. ```shell sudo pip3 install adafruit-circuitpython-as7341 ``` -------------------------------- ### Install adafruit-circuitpython-as7341 in a virtual environment Source: https://github.com/adafruit/adafruit_circuitpython_as7341/blob/main/README.rst Creates a virtual environment in the current project and installs the driver inside it. ```shell mkdir project-name && cd project-name python3 -m venv .venv source .venv/bin/activate pip3 install adafruit-circuitpython-as7341 ``` -------------------------------- ### Install adafruit-circuitpython-as7341 in a virtual environment Source: https://github.com/adafruit/adafruit_circuitpython_as7341/blob/main/docs/index.rst Create a project directory, set up a virtual environment, activate it, and install the driver. ```shell mkdir project-name && cd project-name python3 -m venv .venv source .venv/bin/activate pip3 install adafruit-circuitpython-as7341 ``` -------------------------------- ### Install adafruit-circuitpython-as7341 system-wide Source: https://github.com/adafruit/adafruit_circuitpython_as7341/blob/main/README.rst Installs the driver system-wide, which may be required in some cases. ```shell sudo pip3 install adafruit-circuitpython-as7341 ``` -------------------------------- ### Install adafruit-circuitpython-as7341 from PyPI Source: https://github.com/adafruit/adafruit_circuitpython_as7341/blob/main/docs/index.rst Install the driver for the current user on supported GNU/Linux systems like the Raspberry Pi. ```shell pip3 install adafruit-circuitpython-as7341 ``` -------------------------------- ### DisplayIO Simpletest for AS7341 Source: https://github.com/adafruit/adafruit_circuitpython_as7341/blob/main/docs/examples.rst Use this example to test the AS7341 sensor on boards with a built-in display. It initializes the sensor over I2C, enables flicker detection, and shows the detected flicker frequency on the display using a bitmap label. The loop updates the label every 0.5 seconds. ```python # SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries # SPDX-FileCopyrightText: 2024 Jose D. Montoya # # SPDX-License-Identifier: MIT import time import board from adafruit_display_text.bitmap_label import Label from displayio import Group from terminalio import FONT from adafruit_as7341 import AS7341 # Simple demo of using the built-in display. # create a main_group to hold anything we want to show on the display. main_group = Group() # Initialize I2C bus and sensor. i2c = board.I2C() # uses board.SCL and board.SDA sensor = AS7341(i2c) sensor.flicker_detection_enabled = True # Create Label(s) to show the readings. If you have a very small # display you may need to change to scale=1. display_output_label = Label(FONT, text="", scale=2) # place the label(s) in the middle of the screen with anchored positioning display_output_label.anchor_point = (0, 0) display_output_label.anchored_position = ( 4, board.DISPLAY.height // 2 - 60, ) # add the label(s) to the main_group main_group.append(display_output_label) # set the main_group as the root_group of the built-in DISPLAY board.DISPLAY.root_group = main_group # begin main loop while True: # update the text of the label(s) to show the sensor readings flicker_detected = sensor.flicker_detected if flicker_detected: display_output_label.text = f"Detected a {flicker_detected} Hz flicker" else: display_output_label.text = "No flicker detected" # wait for a bit time.sleep(0.5) ``` -------------------------------- ### Install adafruit-circuitpython-as7341 from PyPI Source: https://github.com/adafruit/adafruit_circuitpython_as7341/blob/main/README.rst Installs the adafruit-circuitpython-as7341 driver for the current user on supported GNU/Linux systems like the Raspberry Pi. ```shell pip3 install adafruit-circuitpython-as7341 ``` -------------------------------- ### Initialize I2C and AS7341 sensor Source: https://github.com/adafruit/adafruit_circuitpython_as7341/blob/main/docs/api.rst Define the board.I2C object and create the sensor object. ```python i2c = board.I2C() # uses board.SCL and board.SDA sensor = AS7341(i2c) ``` -------------------------------- ### Import libraries for AS7341 Source: https://github.com/adafruit/adafruit_circuitpython_as7341/blob/main/docs/api.rst Import the board and AS7341 libraries to use the sensor. ```python import board from adafruit_as7341 import AS7341 ``` -------------------------------- ### Simple test: read all AS7341 channels and display bar graph Source: https://github.com/adafruit/adafruit_circuitpython_as7341/blob/main/docs/examples.rst Initializes the AS7341 sensor over I2C, sets gain to 0.5x, and continuously prints all channel readings (F1-F8, clear, NIR) as a bar graph. Uses board.I2C() by default; comment shows board.STEMMA_I2C() for STEMMA QT connector. ```python # 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 # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller sensor = adafruit_as7341.AS7341(i2c) sensor.gain = adafruit_as7341.Gain.GAIN_0_5X # Update the gain of the sensor def bar_graph(read_value): scaled = int(read_value / 1000) return f"[{read_value:5d}] " + (scaled * "*") while True: print(f"F1 - 415nm/Violet {bar_graph(sensor.channel_415nm)}") print(f"F2 - 445nm//Indigo {bar_graph(sensor.channel_445nm)}") print(f"F3 - 480nm//Blue {bar_graph(sensor.channel_480nm)}") print(f"F4 - 515nm//Cyan {bar_graph(sensor.channel_515nm)}") print(f"F5 - 555nm/Green {bar_graph(sensor.channel_555nm)}") print(f"F6 - 590nm/Yellow {bar_graph(sensor.channel_590nm)}") print(f"F7 - 630nm/Orange {bar_graph(sensor.channel_630nm)}") print(f"F8 - 680nm/Red {bar_graph(sensor.channel_680nm)}") print(f"Clear {bar_graph(sensor.channel_clear)}") print(f"Near-IR (NIR) {bar_graph(sensor.channel_nir)}") print("\n------------------------------------------------") sleep(1) ``` -------------------------------- ### class adafruit_as7341.AS7341 Source: https://github.com/adafruit/adafruit_circuitpython_as7341/blob/main/docs/api.rst Library for the AS7341 Sensor. Initializes the sensor over I2C with configurable address. ```APIDOC ## AS7341(i2c_bus, address=57) ### Description Library for the AS7341 Sensor. Initializes the sensor over I2C. ### Constructor **AS7341**(i2c_bus, address=57) #### Parameters - **i2c_bus** (I2C) - Required - The I2C bus the device is connected to - **address** (int) - Optional - The I2C device address. Defaults to `0x39` ### Quickstart ```python import board from adafruit_as7341 import AS7341 i2c = board.I2C() # uses board.SCL and board.SDA sensor = AS7341(i2c) ``` ### Properties - **all_channels** (Tuple[int, ...]) - The current readings for all six ADC channels - **astep** (int) - The integration time step size in 2.78 microsecond increments - **atime** (int) - The integration time step count. Total integration time will be `(ATIME + 1) * (ASTEP + 1) * 2.78µS` - **channel_415nm** (int) - The current reading for the 415nm band - **channel_445nm** (int) - The current reading for the 445nm band - **channel_480nm** (int) - The current reading for the 480nm band - **channel_515nm** (int) - The current reading for the 515nm band - **channel_555nm** (int) - The current reading for the 555nm band - **channel_590nm** (int) - The current reading for the 590nm band - **channel_630nm** (int) - The current reading for the 630nm band - **channel_680nm** (int) - The current reading for the 680nm band - **channel_clear** (int) - The current reading for the clear sensor - **channel_nir** (int) - The current reading for the NIR (near-IR) sensor - **flicker_detected** (int | None) - The flicker frequency detected in Hertz - **flicker_detection_enabled** (bool) - The flicker detection status of the sensor. True if the sensor is configured to detect flickers. Currently only 1000Hz and 1200Hz flicker detection is supported - **gain** (int) - The ADC gain multiplier. Must be a valid `adafruit_as7341.Gain` ### Methods - **initialize()** -> None - Configure the sensors with the default settings ``` -------------------------------- ### LED test: control AS7341 LED current and state Source: https://github.com/adafruit/adafruit_circuitpython_as7341/blob/main/docs/examples.rst Initializes the sensor, prints the default LED current, sets it to 50, enables the LED for 0.5 seconds, disables it, and prints the final LED status. Useful for verifying the built-in LED functionality. ```python # 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 # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller 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) ``` -------------------------------- ### Read and display AS7341 channel values Source: https://github.com/adafruit/adafruit_circuitpython_as7341/blob/main/README.rst Initializes the AS7341 sensor over I2C and continuously prints bar graphs for each channel (F1-F8, Clear, and Near-IR) every second. ```python3 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) ``` -------------------------------- ### Read and display AS7341 channel data Source: https://github.com/adafruit/adafruit_circuitpython_as7341/blob/main/docs/index.rst Initializes the AS7341 sensor using board.I2C() and continuously prints all channel readings (F1-F8, Clear, NIR) as a bar graph. The bar_graph function scales values by dividing by 1000 and repeats '*' characters. Requires the adafruit_as7341 library and a board with I2C support. ```python3 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) ``` -------------------------------- ### Read all AS7341 channels with bar graph Source: https://github.com/adafruit/adafruit_circuitpython_as7341/blob/main/docs/examples.rst Reads all eight AS7341 channel values at once using sensor.all_channels and prints them with a bar graph visualization. The bar_graph helper scales each reading by dividing by 1000. The loop prints the channels every second. ```python # 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 # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller sensor = AS7341(i2c) def bar_graph(read_value): scaled = int(read_value / 1000) return f"[{read_value:5d}] " + (scaled * "*") while True: sensor_channels = sensor.all_channels print(f"F1 - 415nm/Violet {bar_graph(sensor_channels[0])}") print(f"F2 - 445nm//Indigo {bar_graph(sensor_channels[1])}") print(f"F3 - 480nm//Blue {bar_graph(sensor_channels[2])}") print(f"F4 - 515nm//Cyan {bar_graph(sensor_channels[3])}") print(f"F5 - 555nm/Green {bar_graph(sensor_channels[4])}") print(f"F6 - 590nm/Yellow {bar_graph(sensor_channels[5])}") print(f"F7 - 630nm/Orange {bar_graph(sensor_channels[6])}") print(f"F8 - 680nm/Red {bar_graph(sensor_channels[7])}") print("\n------------------------------------------------") sleep(1) ``` -------------------------------- ### Enable flicker detection with AS7341 Source: https://github.com/adafruit/adafruit_circuitpython_as7341/blob/main/docs/examples.rst Enables flicker detection on the AS7341 sensor and prints the detected flicker frequency in Hz. Requires an I2C connection (board.I2C() or board.STEMMA_I2C()). The loop runs indefinitely, checking every 0.1 seconds. ```python # 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 # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller sensor = AS7341(i2c) sensor.flicker_detection_enabled = True while True: flicker_detected = sensor.flicker_detected if flicker_detected: print(f"Detected a {flicker_detected:d} Hz flicker") sleep(0.1) ``` -------------------------------- ### Read AS7341 channel values Source: https://github.com/adafruit/adafruit_circuitpython_as7341/blob/main/docs/api.rst Access the individual channel readings from the sensor. ```python channel_415nm = sensor.channel_415nm channel_445nm = sensor.channel_445nm channel_480nm = sensor.channel_480nm channel_515nm = sensor.channel_515nm channel_555nm = sensor.channel_555nm channel_590nm = sensor.channel_590nm channel_630nm = sensor.channel_630nm channel_680nm = sensor.channel_680nm ``` -------------------------------- ### class adafruit_as7341.Gain Source: https://github.com/adafruit/adafruit_circuitpython_as7341/blob/main/docs/api.rst Options for setting the ADC gain value of the AS7341 sensor. ```APIDOC ## Gain ### Description Options for `accelerometer_range`. ### Available Settings | Setting | Gain Value | |------------------|--------------| | `Gain.GAIN_0_5X` | 0.5 | | `Gain.GAIN_1X` | 1 | | `Gain.GAIN_2X` | 2 | | `Gain.GAIN_4X` | 4 | | `Gain.GAIN_8X` | 8 | | `Gain.GAIN_16X` | 16 | | `Gain.GAIN_32X` | 32 | | `Gain.GAIN_64X` | 64 | | `Gain.GAIN_128X` | 128 | | `Gain.GAIN_256X` | 256 | | `Gain.GAIN_512X` | 512 | ```