ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
veml3235.h
Go to the documentation of this file.
1#pragma once
2
6
7namespace esphome::veml3235 {
8
9// Register IDs/locations
10//
11static const uint8_t CONFIG_REG = 0x00;
12static const uint8_t W_REG = 0x04;
13static const uint8_t ALS_REG = 0x05;
14static const uint8_t ID_REG = 0x09;
15
16// Bit offsets within CONFIG_REG
17//
18static const uint8_t CONFIG_REG_IT_BIT = 4;
19static const uint8_t CONFIG_REG_DG_BIT = 13;
20static const uint8_t CONFIG_REG_G_BIT = 11;
21
22// Other important constants
23//
24static const uint8_t DEVICE_ID = 0x35;
25
26// Resolution (lx/count) at maximum sensitivity (integration time 800 ms, gain 4x, digital gain 2x)
27//
28static const float LUX_MULTIPLIER_BASE = 0.00213f;
29
30// Enum for conversion/integration time settings for the VEML3235.
31//
32// Specific values of the enum constants are register values taken from the VEML3235 datasheet.
33// Longer times mean more accurate results, but will take more energy/more time.
34//
42
43// Enum for digital gain settings for the VEML3235.
44// Higher values are better for low light situations, but can increase noise.
45//
50
51// Enum for gain settings for the VEML3235.
52// Higher values are better for low light situations, but can increase noise.
53//
59
60class VEML3235Sensor final : public sensor::Sensor, public PollingComponent, public i2c::I2CDevice {
61 public:
62 void setup() override;
63 void dump_config() override;
64 void update() override;
65
66 // Used by ESPHome framework. Does NOT actually set the value on the device.
79
80 bool auto_gain() { return this->auto_gain_; }
84 VEML3235ComponentGain gain() { return this->gain_; }
86
87 // Updates the configuration register on the device
88 bool refresh_config_reg();
89
90 protected:
91 // One measurement pass: reads the ALS counts, possibly adjusts the sensitivity and schedules a re-read,
92 // otherwise publishes the result
93 void read_and_publish_(uint8_t adjustments_left);
94 // Chooses a new sensitivity for the given ALS reading and writes it to the device.
95 // Returns true only if the device configuration was changed.
96 bool adjust_sensitivity_(uint16_t counts);
97 float counts_to_lux_(uint16_t counts) const;
98
99 // Overall sensitivity multiplier (1x-128x, always a power of two) relative to the least sensitive
100 // configuration (integration time 50 ms, gain 1x, digital gain 1x). ALS counts scale linearly with it.
101 uint16_t sensitivity_factor_() const;
102 void set_sensitivity_factor_(uint16_t factor);
103 uint8_t gain_factor_() const;
104 uint16_t integration_time_ms_() const { return 50 << this->integration_time_; }
105
106 // Members are ordered largest to smallest to minimize padding
112 bool auto_gain_{true};
114};
115
116} // namespace esphome::veml3235
This class simplifies creating components that periodically check a state.
Definition component.h:532
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:144
Base-class for all sensors.
Definition sensor.h:48
VEML3235ComponentIntegrationTime integration_time_
Definition veml3235.h:111
void set_digital_gain(VEML3235ComponentDigitalGain digital_gain)
Definition veml3235.h:74
float counts_to_lux_(uint16_t counts) const
Definition veml3235.cpp:85
VEML3235ComponentGain gain()
Definition veml3235.h:84
void set_sensitivity_factor_(uint16_t factor)
Definition veml3235.cpp:106
uint16_t integration_time_ms_() const
Definition veml3235.h:104
void set_integration_time(VEML3235ComponentIntegrationTime integration_time)
Definition veml3235.h:76
bool adjust_sensitivity_(uint16_t counts)
Definition veml3235.cpp:129
void set_auto_gain(bool auto_gain)
Definition veml3235.h:67
void read_and_publish_(uint8_t adjustments_left)
Definition veml3235.cpp:55
void set_auto_gain_threshold_high(float auto_gain_threshold_high)
Definition veml3235.h:68
VEML3235ComponentIntegrationTime integration_time()
Definition veml3235.h:85
void set_gain(VEML3235ComponentGain gain)
Definition veml3235.h:75
VEML3235ComponentGain gain_
Definition veml3235.h:110
uint16_t sensitivity_factor_() const
Definition veml3235.cpp:101
void set_auto_gain_threshold_low(float auto_gain_threshold_low)
Definition veml3235.h:71
VEML3235ComponentDigitalGain digital_gain()
Definition veml3235.h:83
VEML3235ComponentDigitalGain digital_gain_
Definition veml3235.h:109
VEML3235ComponentIntegrationTime
Definition veml3235.h:35
@ VEML3235_INTEGRATION_TIME_100MS
Definition veml3235.h:37
@ VEML3235_INTEGRATION_TIME_200MS
Definition veml3235.h:38
@ VEML3235_INTEGRATION_TIME_50MS
Definition veml3235.h:36
@ VEML3235_INTEGRATION_TIME_800MS
Definition veml3235.h:40
@ VEML3235_INTEGRATION_TIME_400MS
Definition veml3235.h:39