ESPHome 2025.6.3
Loading...
Searching...
No Matches
ads1115.cpp
Go to the documentation of this file.
1#include "ads1115.h"
2#include "esphome/core/hal.h"
3#include "esphome/core/log.h"
4
5namespace esphome {
6namespace ads1115 {
7
8static const char *const TAG = "ads1115";
9static const uint8_t ADS1115_REGISTER_CONVERSION = 0x00;
10static const uint8_t ADS1115_REGISTER_CONFIG = 0x01;
11
13 ESP_LOGCONFIG(TAG, "Running setup");
14 uint16_t value;
15 if (!this->read_byte_16(ADS1115_REGISTER_CONVERSION, &value)) {
16 this->mark_failed();
17 return;
18 }
19
20 uint16_t config = 0;
21 // Clear single-shot bit
22 // 0b0xxxxxxxxxxxxxxx
23 config |= 0b0000000000000000;
24 // Setup multiplexer
25 // 0bx000xxxxxxxxxxxx
26 config |= ADS1115_MULTIPLEXER_P0_N1 << 12;
27
28 // Setup Gain
29 // 0bxxxx000xxxxxxxxx
30 config |= ADS1115_GAIN_6P144 << 9;
31
32 if (this->continuous_mode_) {
33 // Set continuous mode
34 // 0bxxxxxxx0xxxxxxxx
35 config |= 0b0000000000000000;
36 } else {
37 // Set singleshot mode
38 // 0bxxxxxxx1xxxxxxxx
39 config |= 0b0000000100000000;
40 }
41
42 // Set data rate - 860 samples per second
43 // 0bxxxxxxxx100xxxxx
44 config |= ADS1115_860SPS << 5;
45
46 // Set comparator mode - hysteresis
47 // 0bxxxxxxxxxxx0xxxx
48 config |= 0b0000000000000000;
49
50 // Set comparator polarity - active low
51 // 0bxxxxxxxxxxxx0xxx
52 config |= 0b0000000000000000;
53
54 // Set comparator latch enabled - false
55 // 0bxxxxxxxxxxxxx0xx
56 config |= 0b0000000000000000;
57
58 // Set comparator que mode - disabled
59 // 0bxxxxxxxxxxxxxx11
60 config |= 0b0000000000000011;
61
62 if (!this->write_byte_16(ADS1115_REGISTER_CONFIG, config)) {
63 this->mark_failed();
64 return;
65 }
66 this->prev_config_ = config;
67}
69 ESP_LOGCONFIG(TAG, "ADS1115:");
70 LOG_I2C_DEVICE(this);
71 if (this->is_failed()) {
72 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
73 }
74}
77 uint16_t config = this->prev_config_;
78 // Multiplexer
79 // 0bxBBBxxxxxxxxxxxx
80 config &= 0b1000111111111111;
81 config |= (multiplexer & 0b111) << 12;
82
83 // Gain
84 // 0bxxxxBBBxxxxxxxxx
85 config &= 0b1111000111111111;
86 config |= (gain & 0b111) << 9;
87
88 // Sample rate
89 // 0bxxxxxxxxBBBxxxxx
90 config &= 0b1111111100011111;
91 config |= (samplerate & 0b111) << 5;
92
93 if (!this->continuous_mode_) {
94 // Start conversion
95 config |= 0b1000000000000000;
96 }
97
98 if (!this->continuous_mode_ || this->prev_config_ != config) {
99 if (!this->write_byte_16(ADS1115_REGISTER_CONFIG, config)) {
100 this->status_set_warning();
101 return NAN;
102 }
103 this->prev_config_ = config;
104
105 // Delay calculated as: ceil((1000/SPS)+.5)
107 switch (samplerate) {
108 case ADS1115_8SPS:
109 delay(9);
110 break;
111 case ADS1115_16SPS:
112 delay(5);
113 break;
114 case ADS1115_32SPS:
115 delay(3);
116 break;
117 case ADS1115_64SPS:
118 case ADS1115_128SPS:
119 delay(2);
120 break;
121 default:
122 delay(1);
123 break;
124 }
125 } else {
126 switch (samplerate) {
127 case ADS1115_8SPS:
128 delay(126); // NOLINT
129 break;
130 case ADS1115_16SPS:
131 delay(63); // NOLINT
132 break;
133 case ADS1115_32SPS:
134 delay(32);
135 break;
136 case ADS1115_64SPS:
137 delay(17);
138 break;
139 case ADS1115_128SPS:
140 delay(9);
141 break;
142 case ADS1115_250SPS:
143 delay(5);
144 break;
145 case ADS1115_475SPS:
146 delay(3);
147 break;
148 case ADS1115_860SPS:
149 delay(2);
150 break;
151 }
152 }
153
154 // in continuous mode, conversion will always be running, rely on the delay
155 // to ensure conversion is taking place with the correct settings
156 // can we use the rdy pin to trigger when a conversion is done?
157 if (!this->continuous_mode_) {
158 uint32_t start = millis();
159 while (this->read_byte_16(ADS1115_REGISTER_CONFIG, &config) && (config >> 15) == 0) {
160 if (millis() - start > 100) {
161 ESP_LOGW(TAG, "Reading ADS1115 timed out");
162 this->status_set_warning();
163 return NAN;
164 }
165 yield();
166 }
167 }
168 }
169
170 uint16_t raw_conversion;
171 if (!this->read_byte_16(ADS1115_REGISTER_CONVERSION, &raw_conversion)) {
172 this->status_set_warning();
173 return NAN;
174 }
175
177 bool negative = (raw_conversion >> 15) == 1;
178
179 // shift raw_conversion as it's only 12-bits, left justified
180 raw_conversion = raw_conversion >> (16 - ADS1015_12_BITS);
181
182 // check if number was negative in order to keep the sign
183 if (negative) {
184 // the number was negative
185 // 1) set the negative bit back
186 raw_conversion |= 0x8000;
187 // 2) reset the former (shifted) negative bit
188 raw_conversion &= 0xF7FF;
189 }
190 }
191
192 auto signed_conversion = static_cast<int16_t>(raw_conversion);
193
194 float millivolts;
195 float divider = (resolution == ADS1115_16_BITS) ? 32768.0f : 2048.0f;
196 switch (gain) {
198 millivolts = (signed_conversion * 6144) / divider;
199 break;
201 millivolts = (signed_conversion * 4096) / divider;
202 break;
204 millivolts = (signed_conversion * 2048) / divider;
205 break;
207 millivolts = (signed_conversion * 1024) / divider;
208 break;
210 millivolts = (signed_conversion * 512) / divider;
211 break;
213 millivolts = (signed_conversion * 256) / divider;
214 break;
215 default:
216 millivolts = NAN;
217 }
218
219 this->status_clear_warning();
220 return millivolts / 1e3f;
221}
222
223} // namespace ads1115
224} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message="unspecified")
void status_clear_warning()
float request_measurement(ADS1115Multiplexer multiplexer, ADS1115Gain gain, ADS1115Resolution resolution, ADS1115Samplerate samplerate)
Helper method to request a measurement from a sensor.
Definition ads1115.cpp:75
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:250
bool write_byte_16(uint8_t a_register, uint16_t data)
Definition i2c.h:270
AlsGain501 gain
Resolution resolution
Definition msa3xx.h:1
@ ADS1115_MULTIPLEXER_P0_N1
Definition ads1115.h:12
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT yield()
Definition core.cpp:27
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:28