ESPHome 2025.5.0
Loading...
Searching...
No Matches
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
13namespace esphome {
14namespace 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
25static const adc_atten_t ADC_ATTEN_DB_12_COMPAT = ADC_ATTEN_DB_12;
26#else
27static const adc_atten_t ADC_ATTEN_DB_12_COMPAT = ADC_ATTEN_DB_11;
28#endif
29#endif // USE_ESP32
30
31enum class SamplingMode : uint8_t { AVG = 0, MIN = 1, MAX = 2 };
32const LogString *sampling_mode_to_str(SamplingMode mode);
33
35 public:
36 void add_sample(uint32_t value);
37 uint32_t aggregate();
39
40 protected:
42 uint32_t aggr_{0};
43 uint32_t samples_{0};
44};
45
47 public:
48#ifdef USE_ESP32
50 void set_attenuation(adc_atten_t attenuation) { this->attenuation_ = attenuation; }
51 void set_channel1(adc1_channel_t channel) {
52 this->channel1_ = channel;
53 this->channel2_ = ADC2_CHANNEL_MAX;
54 }
55 void set_channel2(adc2_channel_t channel) {
56 this->channel2_ = channel;
57 this->channel1_ = ADC1_CHANNEL_MAX;
58 }
59 void set_autorange(bool autorange) { this->autorange_ = autorange; }
60#endif // USE_ESP32
61
63 void update() override;
65 void setup() override;
66 void dump_config() override;
68 float get_setup_priority() const override;
69 void set_pin(InternalGPIOPin *pin) { this->pin_ = pin; }
70 void set_output_raw(bool output_raw) { this->output_raw_ = output_raw; }
71 void set_sample_count(uint8_t sample_count);
72 void set_sampling_mode(SamplingMode sampling_mode);
73 float sample() override;
74
75#ifdef USE_ESP8266
76 std::string unique_id() override;
77#endif // USE_ESP8266
78
79#ifdef USE_RP2040
80 void set_is_temperature() { this->is_temperature_ = true; }
81#endif // USE_RP2040
82
83 protected:
85 bool output_raw_{false};
86 uint8_t sample_count_{1};
88
89#ifdef USE_RP2040
90 bool is_temperature_{false};
91#endif // USE_RP2040
92
93#ifdef USE_ESP32
94 adc_atten_t attenuation_{ADC_ATTEN_DB_0};
95 adc1_channel_t channel1_{ADC1_CHANNEL_MAX};
96 adc2_channel_t channel2_{ADC2_CHANNEL_MAX};
97 bool autorange_{false};
98#if ESP_IDF_VERSION_MAJOR >= 5
99 esp_adc_cal_characteristics_t cal_characteristics_[SOC_ADC_ATTEN_NUM] = {};
100#else
101 esp_adc_cal_characteristics_t cal_characteristics_[ADC_ATTEN_MAX] = {};
102#endif // ESP_IDF_VERSION_MAJOR
103#endif // USE_ESP32
104};
105
106} // namespace adc
107} // namespace esphome
BedjetMode mode
BedJet operating mode.
This class simplifies creating components that periodically check a state.
Definition component.h:301
void set_sampling_mode(SamplingMode sampling_mode)
std::string unique_id() override
void set_autorange(bool autorange)
Definition adc_sensor.h:59
esp_adc_cal_characteristics_t cal_characteristics_[SOC_ADC_ATTEN_NUM]
Definition adc_sensor.h:99
void set_sample_count(uint8_t sample_count)
void set_pin(InternalGPIOPin *pin)
Definition adc_sensor.h:69
void set_attenuation(adc_atten_t attenuation)
Set the attenuation for this pin. Only available on the ESP32.
Definition adc_sensor.h:50
adc1_channel_t channel1_
Definition adc_sensor.h:95
void setup() override
Setup ADC.
void set_channel1(adc1_channel_t channel)
Definition adc_sensor.h:51
void set_channel2(adc2_channel_t channel)
Definition adc_sensor.h:55
InternalGPIOPin * pin_
Definition adc_sensor.h:84
void update() override
Update ADC values.
adc2_channel_t channel2_
Definition adc_sensor.h:96
SamplingMode sampling_mode_
Definition adc_sensor.h:87
float get_setup_priority() const override
HARDWARE_LATE setup priority
void set_output_raw(bool output_raw)
Definition adc_sensor.h:70
adc_atten_t attenuation_
Definition adc_sensor.h:94
void add_sample(uint32_t value)
Aggregator(SamplingMode mode)
Base-class for all sensors.
Definition sensor.h:57
Abstract interface for components to request voltage (usually ADC readings)
const LogString * sampling_mode_to_str(SamplingMode mode)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7