ESPHome 2025.8.0b2
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
74 float get_setup_priority() const override;
75
76#ifdef USE_ZEPHYR
79 void set_adc_channel(const adc_dt_spec *channel) { this->channel_ = channel; }
80#endif
83 void set_pin(InternalGPIOPin *pin) { this->pin_ = pin; }
84
87 void set_output_raw(bool output_raw) { this->output_raw_ = output_raw; }
88
92 void set_sample_count(uint8_t sample_count);
93
102 void set_sampling_mode(SamplingMode sampling_mode);
103
107 float sample() override;
108
109#ifdef USE_ESP32
114 void set_attenuation(adc_atten_t attenuation) { this->attenuation_ = attenuation; }
115
120 void set_channel(adc_unit_t unit, adc_channel_t channel) {
121 this->adc_unit_ = unit;
122 this->channel_ = channel;
123 }
124
128 void set_autorange(bool autorange) { this->autorange_ = autorange; }
129#endif // USE_ESP32
130
131#ifdef USE_RP2040
132 void set_is_temperature() { this->is_temperature_ = true; }
133#endif // USE_RP2040
134
135 protected:
136 uint8_t sample_count_{1};
137 bool output_raw_{false};
140
141#ifdef USE_ESP32
142 float sample_autorange_();
144 bool autorange_{false};
145 adc_oneshot_unit_handle_t adc_handle_{nullptr};
146 adc_cali_handle_t calibration_handle_{nullptr};
147 adc_atten_t attenuation_{ADC_ATTEN_DB_0};
148 adc_channel_t channel_{};
149 adc_unit_t adc_unit_{};
150 struct SetupFlags {
151 uint8_t init_complete : 1;
152 uint8_t config_complete : 1;
155 uint8_t reserved : 4;
157 static adc_oneshot_unit_handle_t shared_adc_handles[2];
158#endif // USE_ESP32
159
160#ifdef USE_RP2040
161 bool is_temperature_{false};
162#endif // USE_RP2040
163
164#ifdef USE_ZEPHYR
165 const struct adc_dt_spec *channel_ = nullptr;
166#endif
167};
168
169} // namespace adc
170} // namespace esphome
BedjetMode mode
BedJet operating mode.
This class simplifies creating components that periodically check a state.
Definition component.h:425
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:120
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:128
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:83
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:114
adc_oneshot_unit_handle_t adc_handle_
Definition adc_sensor.h:145
void setup() override
Set up the ADC sensor by initializing hardware and calibration parameters.
InternalGPIOPin * pin_
Definition adc_sensor.h:138
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:139
void set_adc_channel(const adc_dt_spec *channel)
Set the ADC channel to be used by the ADC sensor.
Definition adc_sensor.h:79
static adc_oneshot_unit_handle_t shared_adc_handles[2]
Definition adc_sensor.h:157
adc_channel_t channel_
Definition adc_sensor.h:148
float get_setup_priority() const override
Return the setup priority for this component.
void set_output_raw(bool output_raw)
Enable or disable the output of raw ADC values (unprocessed data).
Definition adc_sensor.h:87
adc_cali_handle_t calibration_handle_
Definition adc_sensor.h:146
Aggregator(SamplingMode mode)
Base-class for all sensors.
Definition sensor.h:59
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