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