ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
veml6040.cpp
Go to the documentation of this file.
1#include "veml6040.h"
2#include "esphome/core/log.h"
3
4#include <cinttypes>
5#include <cmath>
6
8
9ESPHOME_LOG_TAG(TAG, "veml6040");
10
11static constexpr uint8_t CONFIG_REG = 0x00;
12static constexpr uint8_t R_DATA_REG = 0x08;
13static constexpr uint8_t G_DATA_REG = 0x09;
14static constexpr uint8_t B_DATA_REG = 0x0A;
15static constexpr uint8_t W_DATA_REG = 0x0B;
16
17static constexpr uint8_t CONFIG_REG_IT_BIT = 4;
18
19static constexpr uint16_t MAX_COUNTS = 0xFFFF;
20
21// Lux per green count at 40 ms integration time; halves for each longer integration time step
22static constexpr float LUX_PER_COUNT_40MS = 0.25168f;
23
24// Correlated colour temperature fit from the Vishay application note "Designing the VEML6040 into an
25// Application": CCT = A * ((R - B) / G + offset) ^ EXPONENT. The note gives offset = 0.5 in open air and says it
26// changes behind cover glass, so it is configurable.
27static constexpr float CCT_A = 4278.6f;
28static constexpr float CCT_EXPONENT = -1.2455f;
29
31 // Auto mode (AF = 0, TRIG = 0) with shutdown bit cleared: the device measures continuously.
32 // The high byte of the configuration register is reserved and must be written as zero.
33 const uint8_t config[] = {static_cast<uint8_t>(this->integration_time_ << CONFIG_REG_IT_BIT), 0x00};
34 if (this->write_register(CONFIG_REG, config, sizeof config) != i2c::ERROR_OK) {
35 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
36 return;
37 }
38 // The data registers hold zeros until the first integration period has finished; publish as soon as it has
39 this->set_timeout(this->integration_time_ms_(), [this]() {
40 this->data_ready_ = true;
41 this->update();
42 });
43}
44
46 ESP_LOGCONFIG(TAG,
47 "VEML6040:\n"
48 " Integration time: %" PRIu32 " ms\n"
49 " Glass attenuation factor: %.2f\n"
50 " Color temperature offset: %.3f",
51 this->integration_time_ms_(), this->glass_attenuation_factor_, this->color_temperature_offset_);
52 LOG_I2C_DEVICE(this);
53 LOG_UPDATE_INTERVAL(this);
54 LOG_SENSOR(" ", "Red channel", this->red_sensor_);
55 LOG_SENSOR(" ", "Green channel", this->green_sensor_);
56 LOG_SENSOR(" ", "Blue channel", this->blue_sensor_);
57 LOG_SENSOR(" ", "White channel", this->white_sensor_);
58 LOG_SENSOR(" ", "Illuminance", this->illuminance_sensor_);
59 LOG_SENSOR(" ", "Color temperature", this->color_temperature_sensor_);
60}
61
63 if (!this->data_ready_)
64 return;
65
66 uint16_t red{0}, green{0}, blue{0}, white{0};
67 if (!this->status_comm_result(this->read_byte_16_le(R_DATA_REG, &red) && this->read_byte_16_le(G_DATA_REG, &green) &&
68 this->read_byte_16_le(B_DATA_REG, &blue) && this->read_byte_16_le(W_DATA_REG, &white)))
69 return;
70
71 ESP_LOGV(TAG, "Counts: red=%u green=%u blue=%u white=%u", red, green, blue, white);
72
73 PUBLISH_STATE(this->red_sensor_, red);
74 PUBLISH_STATE(this->green_sensor_, green);
75 PUBLISH_STATE(this->blue_sensor_, blue);
76 PUBLISH_STATE(this->white_sensor_, white);
77
78 const bool saturated = red == MAX_COUNTS || green == MAX_COUNTS || blue == MAX_COUNTS;
79 if (saturated) {
80 ESP_LOGW(TAG, "Sensor saturated; use a shorter integration time");
81 }
82
83 if (this->illuminance_sensor_ != nullptr) {
84 float lux = NAN;
85 if (green != MAX_COUNTS) {
86 const float lux_per_count = LUX_PER_COUNT_40MS / static_cast<float>(1u << this->integration_time_);
87 lux = green * lux_per_count * this->glass_attenuation_factor_;
88 }
89 this->illuminance_sensor_->publish_state(lux);
90 }
91
92 if (this->color_temperature_sensor_ != nullptr) {
93 float cct = NAN;
94 if (!saturated && green != 0) {
95 const float index =
96 (static_cast<float>(red) - static_cast<float>(blue)) / green + this->color_temperature_offset_;
97 if (index > 0.0f)
98 cct = CCT_A * powf(index, CCT_EXPONENT);
99 }
100 this->color_temperature_sensor_->publish_state(cct);
101 }
102}
103
104} // namespace esphome::veml6040
void mark_failed()
Mark this component as failed.
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
Definition i2c.cpp:54
bool read_byte_16_le(uint8_t a_register, uint16_t *data)
Read a 16-bit value stored least significant byte first.
Definition i2c.h:296
SUB_SENSOR(red) SUB_SENSOR(green) SUB_SENSOR(blue) SUB_SENSOR(white) SUB_SENSOR(illuminance) SUB_SENSOR(color_temperature) protected const float color_temperature_offset_
Definition veml6040.h:32
const VEML6040IntegrationTime integration_time_
Definition veml6040.h:44
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
ESPHOME_LOG_TAG(TAG, "veml6040")