# Vishay VEML6040 Sensor
[](/LICENSE)
[](https://visualstudio.microsoft.com)
[](https://platformio.org/)
[](https://registry.platformio.org/libraries/k0i05/esp_veml6040)
[](https://components.espressif.com/components/k0i05/esp_veml6040)
This ESP32 espressif IoT development framework (esp-idf) i2c peripheral driver was developed for the Vishay VEML6040 4-channel colour I2C sensor.  Information on features and functionality are documented and can be found in the `veml6040.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_veml6040>
## General Usage
To get started, simply copy the component to your project's `components` folder and reference the `veml6040.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.
The auto-calibrate algorithms aren't consistent and are still being worked on.
```text
components
└── esp_veml6040
    ├── CMakeLists.txt
    ├── README.md
    ├── LICENSE
    ├── idf_component.yml
    ├── library.json
    ├── documentation
    │   └── datasheets, etc.
    ├── include
    │   └── veml6040_version.h
    │   └── veml6040.h
    └── veml6040.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.
```c
#include <veml6040.h>
void i2c0_veml6040_task( void *pvParameters ) {
    // initialize the xLastWakeTime variable with the current time.
    TickType_t          last_wake_time  = xTaskGetTickCount ();
    //
    // initialize i2c device configuration
    veml6040_config_t dev_cfg       = I2C_VEML6040_CONFIG_DEFAULT;
    veml6040_handle_t dev_hdl;
    //
    // init device
    veml6040_init(i2c0_bus_hdl, &dev_cfg, &dev_hdl);
    if (dev_hdl == NULL) {
        ESP_LOGE(APP_TAG, "veml6040 handle init failed");
        assert(dev_hdl);
    }
    //
    veml6040_config_register_t cfg_reg;
    //
    veml6040_get_configuration_register(dev_hdl, &cfg_reg);
    //
    ESP_LOGI(APP_TAG, "Configuration Register: 0x%02x (%s)", cfg_reg.reg, uint16_to_binary(cfg_reg.reg));
    // 
    // task loop entry point
    for ( ;; ) {
        ESP_LOGI(APP_TAG, "######################## VEML6040 - START #########################");
        //
        // handle sensor
        float red_als, green_als, blue_als, white_als;
        esp_err_t result = veml6040_get_als(dev_hdl, &red_als, &green_als, &blue_als, &white_als);
        if(result != ESP_OK) {
            ESP_LOGE(APP_TAG, "veml6040 device read failed (%s)", esp_err_to_name(result));
        } else {
            ESP_LOGI(APP_TAG, "Red ALS: %.2f Lux | Green ALS: %.2f Lux | Blue ALS: %.2f Lux | White ALS: %.2f Lux", red_als, green_als, blue_als, white_als);
        }
        //
        ESP_LOGI(APP_TAG, "######################## VEML6040 - END ###########################");
        //
        //
        // pause the task per defined wait period
        vTaskDelaySecUntil( &last_wake_time, I2C0_TASK_SAMPLING_RATE );
    }
    //
    // free resources
    veml6040_delete( dev_hdl );
    vTaskDelete( NULL );
}
```
Copyright (c) 2024 Eric Gionet (<gionet.c.eric@gmail.com>)
                            
                        
                    
                
            idf.py add-dependency "k0i05/esp_veml6040^1.2.1"