ESPHome 2026.5.0-dev
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 >= 6 || \
26 (ESP_IDF_VERSION_MAJOR == 5 && \
27 ((ESP_IDF_VERSION_MINOR == 0 && ESP_IDF_VERSION_PATCH >= 5) || \
28 (ESP_IDF_VERSION_MINOR == 1 && ESP_IDF_VERSION_PATCH >= 3) || \
29 (ESP_IDF_VERSION_MINOR >= 2)) \
30 )
31// clang-format on
32static const adc_atten_t ADC_ATTEN_DB_12_COMPAT = ADC_ATTEN_DB_12;
33#else
34static const adc_atten_t ADC_ATTEN_DB_12_COMPAT = ADC_ATTEN_DB_11;
35#endif
36#endif // USE_ESP32
37
38enum class SamplingMode : uint8_t {
39 AVG = 0,
40 MIN = 1,
41 MAX = 2,
42};
43
44const LogString *sampling_mode_to_str(SamplingMode mode);
45
46template<typename T> class Aggregator {
47 public:
49 void add_sample(T value);
50 T aggregate();
51
52 protected:
53 T aggr_{0};
54 uint8_t samples_{0};
56};
57
59 public:
62 void update() override;
63
66 void setup() override;
67
70 void dump_config() override;
71
72#ifdef USE_ZEPHYR
75 void set_adc_channel(const adc_dt_spec *channel) { this->channel_ = channel; }
76#endif
79 void set_pin(InternalGPIOPin *pin) { this->pin_ = pin; }
80
83 void set_output_raw(bool output_raw) { this->output_raw_ = output_raw; }
84
88 void set_sample_count(uint8_t sample_count);
89
98 void set_sampling_mode(SamplingMode sampling_mode);
99
103 float sample() override;
104
105#ifdef USE_ESP32
110 void set_attenuation(adc_atten_t attenuation) { this->attenuation_ = attenuation; }
111
116 void set_channel(adc_unit_t unit, adc_channel_t channel) {
117 this->adc_unit_ = unit;
118 this->channel_ = channel;
119 }
120
124 void set_autorange(bool autorange) { this->autorange_ = autorange; }
125#endif // USE_ESP32
126
127#ifdef USE_RP2040
128 void set_is_temperature() { this->is_temperature_ = true; }
129#endif // USE_RP2040
130
131 protected:
132 uint8_t sample_count_{1};
133 bool output_raw_{false};
136
137#ifdef USE_ESP32
138 float sample_autorange_();
140 bool autorange_{false};
141 adc_oneshot_unit_handle_t adc_handle_{nullptr};
142 adc_cali_handle_t calibration_handle_{nullptr};
143 adc_atten_t attenuation_{ADC_ATTEN_DB_0};
144 adc_channel_t channel_{};
145 adc_unit_t adc_unit_{};
146 struct SetupFlags {
147 uint8_t init_complete : 1;
148 uint8_t config_complete : 1;
151 uint8_t reserved : 4;
153 static adc_oneshot_unit_handle_t shared_adc_handles[2];
154#endif // USE_ESP32
155
156#ifdef USE_RP2040
157 bool is_temperature_{false};
158#endif // USE_RP2040
159
160#ifdef USE_ZEPHYR
161 const struct adc_dt_spec *channel_ = nullptr;
162#endif
163};
164
165} // namespace adc
166} // namespace esphome
BedjetMode mode
BedJet operating mode.
This class simplifies creating components that periodically check a state.
Definition component.h:602
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:116
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:124
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:79
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:110
adc_oneshot_unit_handle_t adc_handle_
Definition adc_sensor.h:141
void setup() override
Set up the ADC sensor by initializing hardware and calibration parameters.
InternalGPIOPin * pin_
Definition adc_sensor.h:134
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:135
void set_adc_channel(const adc_dt_spec *channel)
Set the ADC channel to be used by the ADC sensor.
Definition adc_sensor.h:75
static adc_oneshot_unit_handle_t shared_adc_handles[2]
Definition adc_sensor.h:153
adc_channel_t channel_
Definition adc_sensor.h:144
void set_output_raw(bool output_raw)
Enable or disable the output of raw ADC values (unprocessed data).
Definition adc_sensor.h:83
adc_cali_handle_t calibration_handle_
Definition adc_sensor.h:142
Aggregator(SamplingMode mode)
Base-class for all sensors.
Definition sensor.h:47
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