ESPHome  2024.3.1
adc_sensor.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/hal.h"
5 #include "esphome/core/defines.h"
8 
9 #ifdef USE_ESP32
10 #include "driver/adc.h"
11 #include <esp_adc_cal.h>
12 #endif
13 
14 namespace esphome {
15 namespace adc {
16 
18  public:
19 #ifdef USE_ESP32
20  void set_attenuation(adc_atten_t attenuation) { attenuation_ = attenuation; }
22  void set_channel1(adc1_channel_t channel) {
23  channel1_ = channel;
24  channel2_ = ADC2_CHANNEL_MAX;
25  }
26  void set_channel2(adc2_channel_t channel) {
27  channel2_ = channel;
28  channel1_ = ADC1_CHANNEL_MAX;
29  }
30  void set_autorange(bool autorange) { autorange_ = autorange; }
31 #endif
32 
34  void update() override;
36  void setup() override;
37  void dump_config() override;
39  float get_setup_priority() const override;
40  void set_pin(InternalGPIOPin *pin) { this->pin_ = pin; }
41  void set_output_raw(bool output_raw) { output_raw_ = output_raw; }
42  float sample() override;
43 
44 #ifdef USE_ESP8266
45  std::string unique_id() override;
46 #endif
47 
48 #ifdef USE_RP2040
50 #endif
51 
52  protected:
54  bool output_raw_{false};
55 
56 #ifdef USE_RP2040
57  bool is_temperature_{false};
58 #endif
59 
60 #ifdef USE_ESP32
61  adc_atten_t attenuation_{ADC_ATTEN_DB_0};
62  adc1_channel_t channel1_{ADC1_CHANNEL_MAX};
63  adc2_channel_t channel2_{ADC2_CHANNEL_MAX};
64  bool autorange_{false};
65 #if ESP_IDF_VERSION_MAJOR >= 5
66  esp_adc_cal_characteristics_t cal_characteristics_[SOC_ADC_ATTEN_NUM] = {};
67 #else
68  esp_adc_cal_characteristics_t cal_characteristics_[ADC_ATTEN_MAX] = {};
69 #endif
70 #endif
71 };
72 
73 } // namespace adc
74 } // namespace esphome
adc_atten_t attenuation_
Definition: adc_sensor.h:61
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:26
void set_channel1(adc1_channel_t channel)
Definition: adc_sensor.h:22
void setup() override
Setup ADC.
InternalGPIOPin * pin_
Definition: adc_sensor.h:53
adc2_channel_t channel2_
Definition: adc_sensor.h:63
void set_output_raw(bool output_raw)
Definition: adc_sensor.h:41
void set_attenuation(adc_atten_t attenuation)
Set the attenuation for this pin. Only available on the ESP32.
Definition: adc_sensor.h:21
std::string unique_id() override
float sample() override
esp_adc_cal_characteristics_t cal_characteristics_[SOC_ADC_ATTEN_NUM]
Definition: adc_sensor.h:66
void update() override
Update ADC values.
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void set_autorange(bool autorange)
Definition: adc_sensor.h:30
void set_pin(InternalGPIOPin *pin)
Definition: adc_sensor.h:40
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:62