uploaded 3 days ago
ESP32 espressif IoT development framework (esp-idf) compatible component for OSRAM AS7341 11-channel multi-spectral digital I2C sensor.

readme

[![License: MIT](https://cdn.prod.website-files.com/5e0f1144930a8bc8aace526c/65dd9eb5aaca434fac4f1c34_License-MIT-blue.svg)](/LICENSE)
[![Build with PlatformIO](https://img.shields.io/badge/build%20with-PlatformIO-orange?logo=data%3Aimage%2Fsvg%2Bxml%3Bbase64%2CPHN2ZyB3aWR0aD0iMjUwMCIgaGVpZ2h0PSIyNTAwIiB2aWV3Qm94PSIwIDAgMjU2IDI1NiIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiBwcmVzZXJ2ZUFzcGVjdFJhdGlvPSJ4TWlkWU1pZCI+PHBhdGggZD0iTTEyOCAwQzkzLjgxIDAgNjEuNjY2IDEzLjMxNCAzNy40OSAzNy40OSAxMy4zMTQgNjEuNjY2IDAgOTMuODEgMCAxMjhjMCAzNC4xOSAxMy4zMTQgNjYuMzM0IDM3LjQ5IDkwLjUxQzYxLjY2NiAyNDIuNjg2IDkzLjgxIDI1NiAxMjggMjU2YzM0LjE5IDAgNjYuMzM0LTEzLjMxNCA5MC41MS0zNy40OUMyNDIuNjg2IDE5NC4zMzQgMjU2IDE2Mi4xOSAyNTYgMTI4YzAtMzQuMTktMTMuMzE0LTY2LjMzNC0zNy40OS05MC41MUMxOTQuMzM0IDEzLjMxNCAxNjIuMTkgMCAxMjggMCIgZmlsbD0iI0ZGN0YwMCIvPjxwYXRoIGQ9Ik0yNDkuMzg2IDEyOGMwIDY3LjA0LTU0LjM0NyAxMjEuMzg2LTEyMS4zODYgMTIxLjM4NkM2MC45NiAyNDkuMzg2IDYuNjEzIDE5NS4wNCA2LjYxMyAxMjggNi42MTMgNjAuOTYgNjAuOTYgNi42MTQgMTI4IDYuNjE0YzY3LjA0IDAgMTIxLjM4NiA1NC4zNDYgMTIxLjM4NiAxMjEuMzg2IiBmaWxsPSIjRkZGIi8+PHBhdGggZD0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xMC4yODkgMTAuMjY3IDEwLjMxN0w5NS44IDc0LjM3OGMtMTkuNjA5IDYuNTEtNDAuODg1IDIwLjc0Mi00MC44ODUgNTEuODguNDM2IDQ1LjAxIDU5LjU3MiA4NS4yNjcgNzMuMTg2IDg1LjI2N1Y2OC44OTJzLTEyLjI1Mi0uMDYyLTI2LjcyOSAzLjgxN3ptMTAuMzk1IDkyLjA5Yy04LjEzOCAxMC4yLTUyLjczNS0yNS44OC00NS4xNTQtNTEuNjQ1IDMuMDAyLTEyLjE0NSAxMy4xOS0xNy42MTIgMjEuNTExLTE2LjI4IDMwLjM1IDYuMTc1IDM1LjI2IDQ5LjM2OSAyMy42NDMgNjcuOTI2em0tMTguODItMzkuNDZhOS4zOTkgOS4zOTkgMCAwIDAtOS4zOTkgOS4zOTggOS4zOTkgOS4zOTkgMCAwIDAgOS40IDkuNCA5LjM5OSA5LjM5OSAwIDAgMCA5LjM5OC05LjQgOS4zOTkgOS4zOTkgMCAwIDAtOS4zOTktOS4zOTl6bS0yLjgxIDguNjcxYTIuMzc0IDIuMzc0IDAgMSAxIDAtNC43NDggMi4zNzQgMi4zNzQgMCAwIDEgMCA0Ljc0OHoiIGZpbGw9IiNGRjdGMDAiLz48L3N2Zz4=)]()
[![Component Registry](https://components.espressif.com/components/k0i05/esp_as7341/badge.svg)](https://components.espressif.com/components/k0i05/esp_as7341)
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/k0i05/library/esp_as7341.svg)](https://registry.platformio.org/libraries/k0i05/esp_as7341)

# OSRAM AS7341 Sensor
This ESP32 espressif IoT development framework (esp-idf) i2c peripheral driver was developed for the OSRAM AS7341 11-channel multi-spectral digital sensor.  Information on features and functionality are documented and can be found in the `as7341.h` header file and in the `documentation` folder.

## Repository
The component is hosted on github and is located here: https://github.com/K0I05/ESP32-S3_ESP-IDF_COMPONENTS/tree/main/components/peripherals/i2c/esp_as7341

## General Usage
To get started, simply copy the component to your project's `components` folder and reference the `as7341.h` header file as an include.  The component includes documentation for the peripheral such as the datasheet, application notes, and/or user manual where applicable.

```
components
└── esp_as7341
    ├── CMakeLists.txt
    ├── README.md
    ├── LICENSE
    ├── idf_component.yml
    ├── library.json
    ├── documentation
    │   └── datasheets, etc.
    ├── include
    │   └── as7341_version.h
    │   └── as7341.h
    └── as7341.c
```

## Basic Example
Once a driver instance is instantiated the sensor is ready for usage as shown in the below example.   This basic implementation of the driver utilizes default configuration settings and makes a measurement request from the sensor at user defined interval and prints the results.

```
#include <as7341.h>

void i2c0_as7341_task( void *pvParameters ) {
    // initialize the xLastWakeTime variable with the current time.
    TickType_t          last_wake_time   = xTaskGetTickCount ();
    //
    // initialize i2c device configuration
    as7341_config_t dev_cfg          = I2C_AS7341_CONFIG_DEFAULT;
    as7341_handle_t dev_hdl;
    bool                flicker_completed = false;
    uint8_t             flicker_cycles = 0;
    //
    // init device
    as7341_init(i2c0_bus_hdl, &dev_cfg, &dev_hdl);
    if (dev_hdl == NULL) {
        ESP_LOGE(APP_TAG, "as7341 handle init failed");
        assert(dev_hdl);
    }
    //
    //as7341_disable_led(dev_hdl);
    //
    // task loop entry point
    for ( ;; ) {
        ESP_LOGI(APP_TAG, "######################## AS7341 - START #########################");
        //
        if(flicker_completed == true) {
            // handle sensor
            as7341_channels_spectral_data_t adc_data;
            esp_err_t result = as7341_get_spectral_measurements(dev_hdl, &adc_data);
            if(result != ESP_OK) {
                ESP_LOGE(APP_TAG, "spectral measurement failed (%s)", esp_err_to_name(result));
            } else {
                ESP_LOGW(APP_TAG, "F1    %d", adc_data.f1);
                ESP_LOGW(APP_TAG, "F2    %d", adc_data.f2);
                ESP_LOGW(APP_TAG, "F3    %d", adc_data.f3);
                ESP_LOGW(APP_TAG, "F4    %d", adc_data.f4);
                ESP_LOGW(APP_TAG, "F5    %d", adc_data.f5);
                ESP_LOGW(APP_TAG, "F6    %d", adc_data.f6);
                ESP_LOGW(APP_TAG, "F7    %d", adc_data.f7);
                ESP_LOGW(APP_TAG, "F8    %d", adc_data.f8);
                ESP_LOGW(APP_TAG, "NIR   %d", adc_data.nir);
                ESP_LOGW(APP_TAG, "CLEAR %d", adc_data.clear);
            }

            as7341_channels_basic_counts_data_t basic_counts_data;
            result = as7341_get_basic_counts(dev_hdl, adc_data, &basic_counts_data);
            if(result != ESP_OK) {
                ESP_LOGE(APP_TAG, "basic counts conversion failed (%s)", esp_err_to_name(result));
            } else {
                ESP_LOGW(APP_TAG, "F1    %f", basic_counts_data.f1);
                ESP_LOGW(APP_TAG, "F2    %f", basic_counts_data.f2);
                ESP_LOGW(APP_TAG, "F3    %f", basic_counts_data.f3);
                ESP_LOGW(APP_TAG, "F4    %f", basic_counts_data.f4);
                ESP_LOGW(APP_TAG, "F5    %f", basic_counts_data.f5);
                ESP_LOGW(APP_TAG, "F6    %f", basic_counts_data.f6);
                ESP_LOGW(APP_TAG, "F7    %f", basic_counts_data.f7);
                ESP_LOGW(APP_TAG, "F8    %f", basic_counts_data.f8);
                ESP_LOGW(APP_TAG, "NIR   %f", basic_counts_data.nir);
                ESP_LOGW(APP_TAG, "CLEAR %f", basic_counts_data.clear);
            }
        } else {
            if(flicker_cycles < 5) {
                as7341_flicker_detection_states_t flicker_state;
                esp_err_t result = as7341_get_flicker_detection_status(dev_hdl, &flicker_state);
                if(result != ESP_OK) {
                    ESP_LOGE(APP_TAG, "flicker detection failed (%s)", esp_err_to_name(result));
                } else {
                    switch(flicker_state) {
                        case AS7341_FLICKER_DETECTION_INVALID:
                            ESP_LOGW(APP_TAG, "Flicker Detection: Invalid");
                            break;
                        case AS7341_FLICKER_DETECTION_UNKNOWN:
                            ESP_LOGW(APP_TAG, "Flicker Detection: Unknown");
                            break;
                        case AS7341_FLICKER_DETECTION_SATURATED:
                            ESP_LOGW(APP_TAG, "Flicker Detection: Saturated");
                            break;
                        case AS7341_FLICKER_DETECTION_100HZ:
                            ESP_LOGW(APP_TAG, "Flicker Detection: 100 Hz");
                            break;
                        case AS7341_FLICKER_DETECTION_120HZ:
                            ESP_LOGW(APP_TAG, "Flicker Detection: 120 Hz");
                            break;
                    }
                }
                as7341_clear_flicker_detection_status_register(dev_hdl);
                ++flicker_cycles;
            } else {
                flicker_completed = true;
            }
        }
        //
        ESP_LOGI(APP_TAG, "######################## AS7341 - END ###########################");
        //
        //
        // pause the task per defined wait period
        vTaskDelaySecUntil( &last_wake_time, I2C0_TASK_SAMPLING_RATE );
    }
    //
    // free resources
    as7341_delete( dev_hdl );
    vTaskDelete( NULL );
}
```



Copyright (c) 2024 Eric Gionet (gionet.c.eric@gmail.com)

Links

Supports all targets

Maintainer

  • Eric Gionet <gionet.c.eric@gmail.com>

License: MIT

To add this component to your project, run:

idf.py add-dependency "k0i05/esp_as7341^1.1.6"

or download archive

Stats

  • Archive size
    Archive size ~ 4.61 MB
  • Downloaded in total
    Downloaded in total 0 times
  • Downloaded this version
    This version: 0 times

Badge

k0i05/esp_as7341 version: 1.1.6
|