ESPHome  2022.6.3
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_channel(adc1_channel_t channel) { channel_ = channel; }
23  void set_autorange(bool autorange) { autorange_ = autorange; }
24 #endif
25 
27  void update() override;
29  void setup() override;
30  void dump_config() override;
32  float get_setup_priority() const override;
33  void set_pin(InternalGPIOPin *pin) { this->pin_ = pin; }
34  void set_output_raw(bool output_raw) { output_raw_ = output_raw; }
35  float sample() override;
36 
37 #ifdef USE_ESP8266
38  std::string unique_id() override;
39 #endif
40 
41  protected:
43  bool output_raw_{false};
44 
45 #ifdef USE_ESP32
46  adc_atten_t attenuation_{ADC_ATTEN_DB_0};
47  adc1_channel_t channel_{};
48  bool autorange_{false};
49  esp_adc_cal_characteristics_t cal_characteristics_[(int) ADC_ATTEN_MAX] = {};
50 #endif
51 };
52 
53 } // namespace adc
54 } // namespace esphome
void set_channel(adc1_channel_t channel)
Definition: adc_sensor.h:22
adc_atten_t attenuation_
Definition: adc_sensor.h:46
Abstract interface for components to request voltage (usually ADC readings)
This class simplifies creating components that periodically check a state.
Definition: component.h:266
void setup() override
Setup ADc.
esp_adc_cal_characteristics_t cal_characteristics_[(int) ADC_ATTEN_MAX]
Definition: adc_sensor.h:49
InternalGPIOPin * pin_
Definition: adc_sensor.h:42
void set_output_raw(bool output_raw)
Definition: adc_sensor.h:34
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
void update() override
Update adc values.
Definition: a4988.cpp:4
void set_autorange(bool autorange)
Definition: adc_sensor.h:23
void set_pin(InternalGPIOPin *pin)
Definition: adc_sensor.h:33
adc1_channel_t channel_
Definition: adc_sensor.h:47
Base-class for all sensors.
Definition: sensor.h:47
float get_setup_priority() const override
HARDWARE_LATE setup priority.
void dump_config() override