8 static const char *
const TAG =
"ads1115";
9 static const uint8_t ADS1115_REGISTER_CONVERSION = 0x00;
10 static const uint8_t ADS1115_REGISTER_CONFIG = 0x01;
12 static const uint8_t ADS1115_DATA_RATE_860_SPS = 0b111;
15 ESP_LOGCONFIG(TAG,
"Setting up ADS1115...");
17 if (!this->
read_byte_16(ADS1115_REGISTER_CONVERSION, &value)) {
22 ESP_LOGCONFIG(TAG,
"Configuring ADS1115...");
27 config |= 0b0000000000000000;
39 config |= 0b0000000000000000;
43 config |= 0b0000000100000000;
48 config |= ADS1115_DATA_RATE_860_SPS << 5;
52 config |= 0b0000000000000000;
56 config |= 0b0000000000000000;
60 config |= 0b0000000000000000;
64 config |= 0b0000000000000011;
73 ESP_LOGCONFIG(TAG,
"Setting up ADS1115...");
76 ESP_LOGE(TAG,
"Communication with ADS1115 failed!");
84 config &= 0b1000111111111111;
85 config |= (multiplexer & 0b111) << 12;
89 config &= 0b1111000111111111;
90 config |= (gain & 0b111) << 9;
94 config |= 0b1000000000000000;
111 uint32_t start =
millis();
112 while (this->
read_byte_16(ADS1115_REGISTER_CONFIG, &config) && (config >> 15) == 0) {
113 if (
millis() - start > 100) {
114 ESP_LOGW(TAG,
"Reading ADS1115 timed out");
123 uint16_t raw_conversion;
124 if (!this->
read_byte_16(ADS1115_REGISTER_CONVERSION, &raw_conversion)) {
130 bool negative = (raw_conversion >> 15) == 1;
139 raw_conversion |= 0x8000;
141 raw_conversion &= 0xF7FF;
145 auto signed_conversion =
static_cast<int16_t
>(raw_conversion);
151 millivolts = (signed_conversion * 6144) / divider;
154 millivolts = (signed_conversion * 4096) / divider;
157 millivolts = (signed_conversion * 2048) / divider;
160 millivolts = (signed_conversion * 1024) / divider;
163 millivolts = (signed_conversion * 512) / divider;
166 millivolts = (signed_conversion * 256) / divider;
173 return millivolts / 1e3f;
bool read_byte_16(uint8_t a_register, uint16_t *data)
void status_set_warning(const char *message="unspecified")
float request_measurement(ADS1115Multiplexer multiplexer, ADS1115Gain gain, ADS1115Resolution resolution)
Helper method to request a measurement from a sensor.
uint32_t IRAM_ATTR HOT millis()
void dump_config() override
void status_clear_warning()
void IRAM_ATTR HOT yield()
virtual void mark_failed()
Mark this component as failed.
Implementation of SPI Controller mode.
bool write_byte_16(uint8_t a_register, uint16_t data)
void IRAM_ATTR HOT delay(uint32_t ms)