ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
tsl2561.cpp
Go to the documentation of this file.
1#include "tsl2561.h"
2#include "esphome/core/log.h"
3
4namespace esphome::tsl2561 {
5
6static const char *const TAG = "tsl2561";
7
8static const uint8_t TSL2561_COMMAND_BIT = 0x80;
9static const uint8_t TSL2561_WORD_BIT = 0x20;
10static const uint8_t TSL2561_REGISTER_CONTROL = 0x00;
11static const uint8_t TSL2561_REGISTER_TIMING = 0x01;
12static const uint8_t TSL2561_REGISTER_ID = 0x0A;
13static const uint8_t TSL2561_REGISTER_DATA_0 = 0x0C;
14static const uint8_t TSL2561_REGISTER_DATA_1 = 0x0E;
15
17 uint8_t id;
18 if (!this->tsl2561_read_byte(TSL2561_REGISTER_ID, &id)) {
19 this->mark_failed();
20 return;
21 }
22
23 uint8_t timing;
24 if (!this->tsl2561_read_byte(TSL2561_REGISTER_TIMING, &timing)) {
25 this->mark_failed();
26 return;
27 }
28
29 timing &= ~0b00010000;
30 timing |= this->gain_ == TSL2561_GAIN_16X ? 0b00010000 : 0;
31
32 timing &= ~0b00000011;
33 timing |= this->integration_time_ & 0b11;
34
35 this->tsl2561_write_byte(TSL2561_REGISTER_TIMING, timing);
36}
38 LOG_SENSOR("", "TSL2561", this);
39 LOG_I2C_DEVICE(this);
40
41 if (this->is_failed()) {
42 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
43 }
44
45 int gain = this->gain_ == TSL2561_GAIN_1X ? 1 : 16;
46 ESP_LOGCONFIG(TAG,
47 " Gain: %dx\n"
48 " Integration Time: %.1f ms",
50
51 LOG_UPDATE_INTERVAL(this);
52}
54 // Power on
55 if (!this->tsl2561_write_byte(TSL2561_REGISTER_CONTROL, 0b00000011)) {
56 this->status_set_warning();
57 return;
58 }
59
60 // Make sure the data is there when we will read it.
61 auto timeout = static_cast<uint32_t>(this->get_integration_time_ms_() + 20);
62
63 this->set_timeout("illuminance", timeout, [this]() { this->read_data_(); });
64}
65
66float TSL2561Sensor::calculate_lx_(uint16_t ch0, uint16_t ch1) {
67 if ((ch0 == 0xFFFF) || (ch1 == 0xFFFF)) {
68 ESP_LOGW(TAG, "Sensor is saturated");
69 return NAN;
70 }
71
72 if (ch0 == 0) {
73 ESP_LOGVV(TAG, "No light detected");
74 return 0.0f;
75 }
76 float d0 = ch0, d1 = ch1;
77 float ratio = d1 / d0;
78
79 float ms = this->get_integration_time_ms_();
80 d0 *= (402.0f / ms);
81 d1 *= (402.0f / ms);
82
83 if (this->gain_ == TSL2561_GAIN_1X) {
84 d0 *= 16;
85 d1 *= 16;
86 }
87
88 if (this->package_cs_) {
89 if (ratio < 0.52f) {
90 return 0.0315f * d0 - 0.0593f * d0 * powf(ratio, 1.4f);
91 } else if (ratio < 0.65f) {
92 return 0.0229f * d0 - 0.0291f * d1;
93 } else if (ratio < 0.80f) {
94 return 0.0157f * d0 - 0.0153f * d1;
95 } else if (ratio < 1.30f) {
96 return 0.00338f * d0 - 0.00260f * d1;
97 }
98
99 return 0.0f;
100 } else {
101 if (ratio < 0.5f) {
102 return 0.0304f * d0 - 0.062f * d0 * powf(ratio, 1.4f);
103 } else if (ratio < 0.61f) {
104 return 0.0224f * d0 - 0.031f * d1;
105 } else if (ratio < 0.80f) {
106 return 0.0128f * d0 - 0.0153f * d1;
107 } else if (ratio < 1.30f) {
108 return 0.00146f * d0 - 0.00112f * d1;
109 }
110 return 0.0f;
111 }
112}
114 uint16_t data1, data0;
115 if (!this->tsl2561_read_uint(TSL2561_WORD_BIT | TSL2561_REGISTER_DATA_1, &data1)) {
116 this->status_set_warning();
117 return;
118 }
119 if (!this->tsl2561_read_uint(TSL2561_WORD_BIT | TSL2561_REGISTER_DATA_0, &data0)) {
120 this->status_set_warning();
121 return;
122 }
123 // Power off
124 if (!this->tsl2561_write_byte(TSL2561_REGISTER_CONTROL, 0b00000000)) {
125 this->status_set_warning();
126 return;
127 }
128
129 float lx = this->calculate_lx_(data0, data1);
130 ESP_LOGD(TAG, "Got illuminance=%.1flx", lx);
131 this->publish_state(lx);
132 this->status_clear_warning();
133}
135 switch (this->integration_time_) {
137 return 13.7f;
139 return 100.0f;
141 return 402.0f;
142 }
143 return 0.0f;
144}
149void TSL2561Sensor::set_is_cs_package(bool package_cs) { this->package_cs_ = package_cs; }
150
151bool TSL2561Sensor::tsl2561_write_byte(uint8_t a_register, uint8_t value) {
152 return this->write_byte(a_register | TSL2561_COMMAND_BIT, value);
153}
154bool TSL2561Sensor::tsl2561_read_uint(uint8_t a_register, uint16_t *value) {
155 uint8_t data[2];
156 if (!this->read_bytes(a_register | TSL2561_COMMAND_BIT, data, 2))
157 return false;
158 const uint16_t hi = data[1];
159 const uint16_t lo = data[0];
160 *value = (hi << 8) | lo;
161 return true;
162}
163bool TSL2561Sensor::tsl2561_read_byte(uint8_t a_register, uint8_t *value) {
164 return this->read_byte(a_register | TSL2561_COMMAND_BIT, value);
165}
166
167} // namespace esphome::tsl2561
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:272
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:493
void status_clear_warning()
Definition component.h:289
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:265
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:240
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition i2c.h:217
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
TSL2561IntegrationTime integration_time_
Definition tsl2561.h:79
bool tsl2561_write_byte(uint8_t a_register, uint8_t value)
Definition tsl2561.cpp:151
bool tsl2561_read_uint(uint8_t a_register, uint16_t *value)
Definition tsl2561.cpp:154
float calculate_lx_(uint16_t ch0, uint16_t ch1)
Definition tsl2561.cpp:66
bool tsl2561_read_byte(uint8_t a_register, uint8_t *value)
Definition tsl2561.cpp:163
void set_gain(TSL2561Gain gain)
Set the internal gain of the sensor.
Definition tsl2561.cpp:148
void set_is_cs_package(bool package_cs)
The "CS" package of this sensor has a slightly different formula for converting the raw values.
Definition tsl2561.cpp:149
void set_integration_time(TSL2561IntegrationTime integration_time)
Set the time that sensor values should be accumulated for.
Definition tsl2561.cpp:145
uint16_t id
AlsGain501 gain
IntegrationTime501 integration_time
TSL2561IntegrationTime
Enum listing all conversion/integration time settings for the TSL2561.
Definition tsl2561.h:13
@ TSL2561_INTEGRATION_14MS
Definition tsl2561.h:14
@ TSL2561_INTEGRATION_402MS
Definition tsl2561.h:16
@ TSL2561_INTEGRATION_101MS
Definition tsl2561.h:15
TSL2561Gain
Enum listing all gain settings for the TSL2561.
Definition tsl2561.h:23
static void uint32_t