ESPHome 2026.8.0-dev
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//
18// The device expects the low data byte first while write_byte_16() sends the high byte first, so the 16-bit
19// configuration word used here is byte-swapped relative to the datasheet: datasheet low-byte bits are word
20// bits 15:8 here and datasheet high-byte bits are word bits 7:0.
21//
22static const uint8_t CONFIG_REG_IT_BIT = 12;
23static const uint8_t CONFIG_REG_DG_BIT = 5;
24static const uint8_t CONFIG_REG_G_BIT = 3;
25
26// Other important constants
27//
28static const uint8_t DEVICE_ID = 0x35;
29
30// Resolution (lx/count) at maximum sensitivity (integration time 800 ms, gain 4x, digital gain 2x)
31//
32static const float LUX_MULTIPLIER_BASE = 0.00213f;
33
34// Enum for conversion/integration time settings for the VEML3235.
35//
36// Specific values of the enum constants are register values taken from the VEML3235 datasheet.
37// Longer times mean more accurate results, but will take more energy/more time.
38//
46
47// Enum for digital gain settings for the VEML3235.
48// Higher values are better for low light situations, but can increase noise.
49//
54
55// Enum for gain settings for the VEML3235.
56// Higher values are better for low light situations, but can increase noise.
57//
63
64class VEML3235Sensor final : public sensor::Sensor, public PollingComponent, public i2c::I2CDevice {
65 public:
66 void setup() override;
67 void dump_config() override;
68 void update() override;
69
70 // Used by ESPHome framework. Does NOT actually set the value on the device.
83
84 bool auto_gain() { return this->auto_gain_; }
88 VEML3235ComponentGain gain() { return this->gain_; }
90
91 // Updates the configuration register on the device
92 bool refresh_config_reg();
93
94 protected:
95 // One measurement pass: reads the ALS counts, possibly adjusts the sensitivity and schedules a re-read,
96 // otherwise publishes the result
97 void read_and_publish_(uint8_t adjustments_left);
98 // Chooses a new sensitivity for the given ALS reading and writes it to the device.
99 // Returns true only if the device configuration was changed.
100 bool adjust_sensitivity_(uint16_t counts);
101 float counts_to_lux_(uint16_t counts) const;
102
103 // Overall sensitivity multiplier (1x-128x, always a power of two) relative to the least sensitive
104 // configuration (integration time 50 ms, gain 1x, digital gain 1x). ALS counts scale linearly with it.
105 uint16_t sensitivity_factor_() const;
106 void set_sensitivity_factor_(uint16_t factor);
107 uint8_t gain_factor_() const;
108 uint16_t integration_time_ms_() const { return 50 << this->integration_time_; }
109
110 // Members are ordered largest to smallest to minimize padding
116 bool auto_gain_{true};
118};
119
120} // namespace esphome::veml3235
This class simplifies creating components that periodically check a state.
Definition component.h:510
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:132
Base-class for all sensors.
Definition sensor.h:47
VEML3235ComponentIntegrationTime integration_time_
Definition veml3235.h:115
void set_digital_gain(VEML3235ComponentDigitalGain digital_gain)
Definition veml3235.h:78
float counts_to_lux_(uint16_t counts) const
Definition veml3235.cpp:88
VEML3235ComponentGain gain()
Definition veml3235.h:88
void set_sensitivity_factor_(uint16_t factor)
Definition veml3235.cpp:109
uint16_t integration_time_ms_() const
Definition veml3235.h:108
void set_integration_time(VEML3235ComponentIntegrationTime integration_time)
Definition veml3235.h:80
bool adjust_sensitivity_(uint16_t counts)
Definition veml3235.cpp:132
void set_auto_gain(bool auto_gain)
Definition veml3235.h:71
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:72
VEML3235ComponentIntegrationTime integration_time()
Definition veml3235.h:89
void set_gain(VEML3235ComponentGain gain)
Definition veml3235.h:79
VEML3235ComponentGain gain_
Definition veml3235.h:114
uint16_t sensitivity_factor_() const
Definition veml3235.cpp:104
void set_auto_gain_threshold_low(float auto_gain_threshold_low)
Definition veml3235.h:75
VEML3235ComponentDigitalGain digital_gain()
Definition veml3235.h:87
VEML3235ComponentDigitalGain digital_gain_
Definition veml3235.h:113
const uint8_t CONFIG_REG
Definition pca9554.cpp:10
VEML3235ComponentIntegrationTime
Definition veml3235.h:39
@ VEML3235_INTEGRATION_TIME_100MS
Definition veml3235.h:41
@ VEML3235_INTEGRATION_TIME_200MS
Definition veml3235.h:42
@ VEML3235_INTEGRATION_TIME_50MS
Definition veml3235.h:40
@ VEML3235_INTEGRATION_TIME_800MS
Definition veml3235.h:44
@ VEML3235_INTEGRATION_TIME_400MS
Definition veml3235.h:43