Skip to content

esp_as7341 esp‐idf component

Koios edited this page Nov 9, 2025 · 1 revision

AS7341 Spectrometer Driver (as7341.h)

Documentation for the ESP-IDF driver for the AMS AS7341 11-channel spectrometer as implemented in as7341.h.


Overview

This component provides an ESP-IDF compatible driver for the AMS AS7341 I2C spectrometer—an 11-channel sensor covering visible and NIR wavelengths. It has facility for spectral data acquisition, flicker detection, gain and timing adjustment, LED control, and power management.

  • Supported Device: AMS AS7341 Spectrometer
  • Platform: ESP32-S3 (ESP-IDF)
  • Protocol: I2C

License

MIT License
Copyright (c) 2024 Eric Gionet
See LICENSE for details.


Features

  • Read raw spectral measurements from 8 bands (F1–F8), plus clear and NIR channels
  • Gain and timing configuration (ATIME, ASTEP, spectral gain)
  • Power and LED control
  • Flicker detection (100 Hz / 120 Hz)
  • Sensing mode selection
  • Buffer and basic conversion between spectral data and normalized "counts" (floats)
  • Extended hardware features: SMUX, interrupt, wait time, etc.
  • Firmware version query functions

Usage Example

#include "as7341.h"

as7341_config_t config = AS7341_CONFIG_DEFAULT;
as7341_handle_t spectrometer;
esp_err_t ret = as7341_init(master_i2c_bus, &config, &spectrometer);

if (ret == ESP_OK) {
    as7341_channels_spectral_data_t raw;
    as7341_get_spectral_measurements(spectrometer, &raw);

    as7341_channels_basic_counts_data_t counts;
    as7341_get_basic_counts(spectrometer, raw, &counts);
    // Use counts.f1...counts.nir
}

Macros

  • AS7341_CONFIG_DEFAULT
    #define AS7341_CONFIG_DEFAULT { \
      .i2c_address        = I2C_AS7341_DEV_ADDR, \
      .i2c_clock_speed    = I2C_AS7341_DEV_CLK_SPD, \
      .spectral_gain      = AS7341_SPECTRAL_GAIN_32X, \
      .power_enabled      = true, \
      .atime              = 29, \
      .astep              = 599 \
    }
    • Fast-start values for gain, timing, and power.

Constants

Name Value Description
I2C_AS7341_DEV_CLK_SPD 100000 I2C SCL frequency (Hz)
I2C_AS7341_DEV_ADDR 0x39 Device I2C address

Enumerations

  • ALS Modes:

    • AS7341_ALS_SPM_MODE (default, spectral measurement mode)
    • AS7341_ALS_SYNS_MODE
    • AS7341_ALS_RESERVED_MODE
    • AS7341_ALS_SYND_MODE
  • LED Drive Strengths:

    • AS7341_LED_DRIVE_STRENGTH_4MA ... AS7341_LED_DRIVE_STRENGTH_258MA
  • Spectral Gains:

    • AS7341_SPECTRAL_GAIN_0_5X, ..., AS7341_SPECTRAL_GAIN_512X
  • Flicker Detection Gains/States:

    • AS7341_FLICKER_DETECTION_GAIN_0_5X ... 512X
    • AS7341_FLICKER_DETECTION_INVALID, UNKNOWN, SATURATED, 100HZ, 120HZ
  • SMUX Commands:

    • AS7341_SMUX_CMD_ROM_RESET, READ, WRITE

Structures

as7341_channels_spectral_data_t (Raw ADC)

Field Range (nm)
f1 405–425
f2 435–455
f3 470–490
f4 505–525
f5 545–565
f6 580–600
f7 620–640
f8 670–690
clear -
nir -

as7341_channels_basic_counts_data_t (Normalized)

Field Range (nm) Description
f1 405–425 Float
... ... ...
nir - Float

as7341_config_t

Field Type Description
i2c_address uint16_t I2C device address
i2c_clock_speed uint32_t SCL frequency (Hz)
atime uint8_t Integration step count
astep uint16_t Integration time step
spectral_gain as7341_spectral_gains_t Gain multiplier
power_enabled bool Power on/off

API Reference

Initialization & Removal

  • as7341_init(i2c_master_bus_handle_t, const as7341_config_t *, as7341_handle_t *)
  • as7341_remove(as7341_handle_t)
  • as7341_delete(as7341_handle_t)

Data Readout / Conversion

  • as7341_get_spectral_measurements(as7341_handle_t, as7341_channels_spectral_data_t *)
    • Read raw data for F1–F8, clear, and NIR.
  • as7341_get_basic_counts(as7341_handle_t, const as7341_channels_spectral_data_t, as7341_channels_basic_counts_data_t *)
    • Convert raw spectral data to normalized float counts.

Flicker Detection

  • as7341_enable_flicker_detection(as7341_handle_t), as7341_disable_flicker_detection(as7341_handle_t)
  • as7341_get_flicker_detection_status(as7341_handle_t, as7341_flicker_detection_states_t *)

Gain & Timing

  • as7341_set_spectral_gain, as7341_get_spectral_gain
  • as7341_set_atime, as7341_get_atime
  • as7341_set_astep, as7341_get_astep

Mode & Power Control

  • ALS mode: as7341_set_ambient_light_sensing_mode, as7341_get_ambient_light_sensing_mode
  • Power: as7341_enable_power, as7341_disable_power
  • LED: as7341_enable_led, as7341_disable_led
  • Spectral measurement: as7341_enable_spectral_measurement, as7341_disable_spectral_measurement
  • SMUX and wait time enable/disable

Status

  • as7341_get_data_status(as7341_handle_t, bool *ready);

Firmware Version

  • as7341_get_fw_version()
  • as7341_get_fw_version_number()

Dependencies

  • <esp_err.h>
  • <driver/i2c_master.h>
  • "as7341_version.h"
  • "type_utils.h"

References


Clone this wiki locally