ESPHome 2026.2.4
Loading...
Searching...
No Matches
adc_sensor.h
Go to the documentation of this file.
1#pragma once
2
7#include "esphome/core/hal.h"
8
9#ifdef USE_ESP32
10#include "esp_adc/adc_cali.h"
11#include "esp_adc/adc_cali_scheme.h"
12#include "esp_adc/adc_oneshot.h"
13#include "hal/adc_types.h" // This defines ADC_CHANNEL_MAX
14#endif // USE_ESP32
15
16#ifdef USE_ZEPHYR
17#include <zephyr/drivers/adc.h>
18#endif
19
20namespace esphome {
21namespace adc {
22
23#ifdef USE_ESP32
24// clang-format off
25#if (ESP_IDF_VERSION_MAJOR == 5 && \
26 ((ESP_IDF_VERSION_MINOR == 0 && ESP_IDF_VERSION_PATCH >= 5) || \
27 (ESP_IDF_VERSION_MINOR == 1 && ESP_IDF_VERSION_PATCH >= 3) || \
28 (ESP_IDF_VERSION_MINOR >= 2)) \
29 )
30// clang-format on
31static const adc_atten_t ADC_ATTEN_DB_12_COMPAT = ADC_ATTEN_DB_12;
32#else
33static const adc_atten_t ADC_ATTEN_DB_12_COMPAT = ADC_ATTEN_DB_11;
34#endif
35#endif // USE_ESP32
36
37enum class SamplingMode : uint8_t {
38 AVG = 0,
39 MIN = 1,
40 MAX = 2,
41};
42
43const LogString *sampling_mode_to_str(SamplingMode mode);
44
45template<typename T> class Aggregator {
46 public:
48 void add_sample(T value);
49 T aggregate();
50
51 protected:
52 T aggr_{0};
53 uint8_t samples_{0};
55};
56
58 public:
61 void update() override;
62
65 void setup() override;
66
69 void dump_config() override;
70
71#ifdef USE_ZEPHYR
74 void set_adc_channel(const adc_dt_spec *channel) { this->channel_ = channel; }
75#endif
78 void set_pin(InternalGPIOPin *pin) { this->pin_ = pin; }
79
82 void set_output_raw(bool output_raw) { this->output_raw_ = output_raw; }
83
87 void set_sample_count(uint8_t sample_count);
88
97 void set_sampling_mode(SamplingMode sampling_mode);
98
102 float sample() override;
103
104#ifdef USE_ESP32
109 void set_attenuation(adc_atten_t attenuation) { this->attenuation_ = attenuation; }
110
115 void set_channel(adc_unit_t unit, adc_channel_t channel) {
116 this->adc_unit_ = unit;
117 this->channel_ = channel;
118 }
119
123 void set_autorange(bool autorange) { this->autorange_ = autorange; }
124#endif // USE_ESP32
125
126#ifdef USE_RP2040
127 void set_is_temperature() { this->is_temperature_ = true; }
128#endif // USE_RP2040
129
130 protected:
131 uint8_t sample_count_{1};
132 bool output_raw_{false};
135
136#ifdef USE_ESP32
137 float sample_autorange_();
139 bool autorange_{false};
140 adc_oneshot_unit_handle_t adc_handle_{nullptr};
141 adc_cali_handle_t calibration_handle_{nullptr};
142 adc_atten_t attenuation_{ADC_ATTEN_DB_0};
143 adc_channel_t channel_{};
144 adc_unit_t adc_unit_{};
145 struct SetupFlags {
146 uint8_t init_complete : 1;
147 uint8_t config_complete : 1;
150 uint8_t reserved : 4;
152 static adc_oneshot_unit_handle_t shared_adc_handles[2];
153#endif // USE_ESP32
154
155#ifdef USE_RP2040
156 bool is_temperature_{false};
157#endif // USE_RP2040
158
159#ifdef USE_ZEPHYR
160 const struct adc_dt_spec *channel_ = nullptr;
161#endif
162};
163
164} // namespace adc
165} // namespace esphome
BedjetMode mode
BedJet operating mode.
This class simplifies creating components that periodically check a state.
Definition component.h:512
struct esphome::adc::ADCSensor::SetupFlags setup_flags_
void set_channel(adc_unit_t unit, adc_channel_t channel)
Configure the ADC to use a specific channel on a specific ADC unit.
Definition adc_sensor.h:115
void set_sampling_mode(SamplingMode sampling_mode)
Set the sampling mode for how multiple ADC samples are combined into a single measurement.
void set_autorange(bool autorange)
Set whether autoranging should be enabled for the ADC.
Definition adc_sensor.h:123
void set_sample_count(uint8_t sample_count)
Set the number of samples to be taken for ADC readings to improve accuracy.
void set_pin(InternalGPIOPin *pin)
Set the GPIO pin to be used by the ADC sensor.
Definition adc_sensor.h:78
float sample() override
Perform a single ADC sampling operation and return the measured value.
void set_attenuation(adc_atten_t attenuation)
Set the ADC attenuation level to adjust the input voltage range.
Definition adc_sensor.h:109
adc_oneshot_unit_handle_t adc_handle_
Definition adc_sensor.h:140
void setup() override
Set up the ADC sensor by initializing hardware and calibration parameters.
InternalGPIOPin * pin_
Definition adc_sensor.h:133
void dump_config() override
Output the configuration details of the ADC sensor for debugging purposes.
void update() override
Update the sensor's state by reading the current ADC value.
SamplingMode sampling_mode_
Definition adc_sensor.h:134
void set_adc_channel(const adc_dt_spec *channel)
Set the ADC channel to be used by the ADC sensor.
Definition adc_sensor.h:74
static adc_oneshot_unit_handle_t shared_adc_handles[2]
Definition adc_sensor.h:152
adc_channel_t channel_
Definition adc_sensor.h:143
void set_output_raw(bool output_raw)
Enable or disable the output of raw ADC values (unprocessed data).
Definition adc_sensor.h:82
adc_cali_handle_t calibration_handle_
Definition adc_sensor.h:141
Aggregator(SamplingMode mode)
Base-class for all sensors.
Definition sensor.h:43
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