ADC Sensor

Example Usage

// Basic
App.make_adc_sensor("Analog Voltage", 13);
// Custom update interval 30 seconds
App.make_adc_sensor("Analog Voltage", 13, 30000);
// Custom pinMode
App.make_adc_sensor("Analog Voltage", GPIOInputPin(13, INPUT_PULLUP));
// ESP32: Attenuation
auto adc = App.make_adc_sensor("Analog Voltage", 13);
adc.adc.set_attenuation(ADC_11db);

See Application::make_adc_sensor().

API Reference

class ADCSensorComponent : public sensor::PollingSensorComponent

This class allows using the integrated Analog to Digital converts of the ESP32 and ESP8266.

Internally it uses the existing analogRead methods for doing this.

The ESP8266 only has one pin where this can be used: A0

The ESP32 has multiple pins that can be used with this component: GPIO32-GPIO39 Note you can’t use the ADC2 here on the ESP32 because that’s already used by WiFi internally. Additionally on the ESP32 you can set an using set_attenuation.

Public Functions

ADCSensorComponent(const std::string &name, GPIOInputPin pin, uint32_t update_interval = 15000)

Construct the ADCSensor with the provided pin and update interval in ms.

void set_attenuation(adc_attenuation_t attenuation)

Set the attenuation for this pin. Only available on the ESP32.

adc_attenuation_t get_attenuation() const

Get the pin used for this ADC sensor.

Get the attenuation used for this sensor.

void update()

Update adc values.

void setup()

Setup ADc.

void dump_config()
std::string unit_of_measurement()

Unit of measurement: “V”.

std::string icon()

Icon: “mdi:flash”.

int8_t accuracy_decimals()

Accuracy decimals: 2.

float get_setup_priority() const

HARDWARE_LATE setup priority.

std::string unique_id()

A unique ID for this sensor, empty for no unique id.

See unique ID requirements: https://developers.home-assistant.io/docs/en/entity_registry_index.html#unique-id-requirements

Return
The unique id as a string.

Protected Attributes

GPIOInputPin pin_
adc_attenuation_t attenuation_ = {ADC_0db}