ESPHome  2024.12.2
adc_sensor.h
Go to the documentation of this file.
1 #pragma once
2 
6 #include "esphome/core/hal.h"
7 
8 #ifdef USE_ESP32
9 #include <esp_adc_cal.h>
10 #include "driver/adc.h"
11 #endif // USE_ESP32
12 
13 namespace esphome {
14 namespace adc {
15 
16 #ifdef USE_ESP32
17 // clang-format off
18 #if (ESP_IDF_VERSION_MAJOR == 4 && ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 7)) || \
19  (ESP_IDF_VERSION_MAJOR == 5 && \
20  ((ESP_IDF_VERSION_MINOR == 0 && ESP_IDF_VERSION_PATCH >= 5) || \
21  (ESP_IDF_VERSION_MINOR == 1 && ESP_IDF_VERSION_PATCH >= 3) || \
22  (ESP_IDF_VERSION_MINOR >= 2)) \
23  )
24 // clang-format on
25 static const adc_atten_t ADC_ATTEN_DB_12_COMPAT = ADC_ATTEN_DB_12;
26 #else
27 static const adc_atten_t ADC_ATTEN_DB_12_COMPAT = ADC_ATTEN_DB_11;
28 #endif
29 #endif // USE_ESP32
30 
32  public:
33 #ifdef USE_ESP32
34  void set_attenuation(adc_atten_t attenuation) { this->attenuation_ = attenuation; }
36  void set_channel1(adc1_channel_t channel) {
37  this->channel1_ = channel;
38  this->channel2_ = ADC2_CHANNEL_MAX;
39  }
40  void set_channel2(adc2_channel_t channel) {
41  this->channel2_ = channel;
42  this->channel1_ = ADC1_CHANNEL_MAX;
43  }
44  void set_autorange(bool autorange) { this->autorange_ = autorange; }
45 #endif // USE_ESP32
46 
48  void update() override;
50  void setup() override;
51  void dump_config() override;
53  float get_setup_priority() const override;
54  void set_pin(InternalGPIOPin *pin) { this->pin_ = pin; }
55  void set_output_raw(bool output_raw) { this->output_raw_ = output_raw; }
56  void set_sample_count(uint8_t sample_count);
57  float sample() override;
58 
59 #ifdef USE_ESP8266
60  std::string unique_id() override;
61 #endif // USE_ESP8266
62 
63 #ifdef USE_RP2040
64  void set_is_temperature() { this->is_temperature_ = true; }
65 #endif // USE_RP2040
66 
67  protected:
69  bool output_raw_{false};
70  uint8_t sample_count_{1};
71 
72 #ifdef USE_RP2040
73  bool is_temperature_{false};
74 #endif // USE_RP2040
75 
76 #ifdef USE_ESP32
77  adc_atten_t attenuation_{ADC_ATTEN_DB_0};
78  adc1_channel_t channel1_{ADC1_CHANNEL_MAX};
79  adc2_channel_t channel2_{ADC2_CHANNEL_MAX};
80  bool autorange_{false};
81 #if ESP_IDF_VERSION_MAJOR >= 5
82  esp_adc_cal_characteristics_t cal_characteristics_[SOC_ADC_ATTEN_NUM] = {};
83 #else
84  esp_adc_cal_characteristics_t cal_characteristics_[ADC_ATTEN_MAX] = {};
85 #endif // ESP_IDF_VERSION_MAJOR
86 #endif // USE_ESP32
87 };
88 
89 } // namespace adc
90 } // namespace esphome
adc_atten_t attenuation_
Definition: adc_sensor.h:77
Abstract interface for components to request voltage (usually ADC readings)
This class simplifies creating components that periodically check a state.
Definition: component.h:283
void set_channel2(adc2_channel_t channel)
Definition: adc_sensor.h:40
void set_channel1(adc1_channel_t channel)
Definition: adc_sensor.h:36
void setup() override
Setup ADC.
InternalGPIOPin * pin_
Definition: adc_sensor.h:68
adc2_channel_t channel2_
Definition: adc_sensor.h:79
void set_sample_count(uint8_t sample_count)
void set_output_raw(bool output_raw)
Definition: adc_sensor.h:55
void set_attenuation(adc_atten_t attenuation)
Set the attenuation for this pin. Only available on the ESP32.
Definition: adc_sensor.h:35
std::string unique_id() override
esp_adc_cal_characteristics_t cal_characteristics_[SOC_ADC_ATTEN_NUM]
Definition: adc_sensor.h:82
void update() override
Update ADC values.
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_autorange(bool autorange)
Definition: adc_sensor.h:44
void set_pin(InternalGPIOPin *pin)
Definition: adc_sensor.h:54
Base-class for all sensors.
Definition: sensor.h:57
float get_setup_priority() const override
HARDWARE_LATE setup priority
adc1_channel_t channel1_
Definition: adc_sensor.h:78