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