esphome-docs/components/sensor/adc.rst

131 lines
5.5 KiB
ReStructuredText
Raw Normal View History

2018-05-13 11:37:02 +02:00
Analog To Digital Sensor
========================
2018-11-14 22:12:27 +01:00
.. seo::
:description: Instructions for setting up built-in analog voltage sensors.
:image: flash.svg
2018-11-14 22:12:27 +01:00
2018-05-13 11:37:02 +02:00
The Analog To Digital (``adc``) Sensor allows you to use the built-in
ADC in your device to measure a voltage on certain pins. On the ESP8266
only pin A0 (GPIO17) supports this. On the ESP32 pins GPIO32 through
GPIO39 can be used.
2018-06-01 18:10:00 +02:00
.. figure:: images/adc-ui.png
:align: center
:width: 80.0%
2018-05-13 11:37:02 +02:00
.. code-block:: yaml
2018-05-13 11:37:02 +02:00
# Example configuration entry
sensor:
- platform: adc
pin: A0
name: "Living Room Brightness"
2019-01-06 18:56:14 +01:00
update_interval: 60s
2018-05-13 11:37:02 +02:00
Configuration variables:
2018-08-24 22:44:01 +02:00
------------------------
2018-05-13 11:37:02 +02:00
2018-06-01 18:10:00 +02:00
- **pin** (**Required**, :ref:`config-pin`): The pin to measure the voltage on.
2018-06-02 13:42:51 +02:00
Or on the ESP8266 alternatively also ``VCC``, see :ref:`adc-esp8266_vcc`.
2018-06-01 18:10:00 +02:00
- **name** (**Required**, string): The name of the voltage sensor.
- **attenuation** (*Optional*): Only on ESP32. Specify the ADC
attenuation to use. See :ref:`adc-esp32_attenuation`. Defaults to ``0db``.
- **raw** (*Optional*): Allows to read the raw ADC output without any conversion or calibration. See :ref:`adc-raw`. Defaults to ``false``.
2018-06-01 18:10:00 +02:00
- **update_interval** (*Optional*, :ref:`config-time`): The interval
2019-01-06 18:56:14 +01:00
to check the sensor. Defaults to ``60s``.
2018-06-01 18:10:00 +02:00
- **id** (*Optional*, :ref:`config-id`): Manually specify the ID used for code generation.
2019-02-17 12:28:17 +01:00
- All other options from :ref:`Sensor <config-sensor>`.
2018-06-01 18:10:00 +02:00
.. note::
This component prints the voltage as seen by the chip pin. On the ESP8266, this is always 0.0V to 1.0V
Some development boards like the Wemos D1 mini include external voltage divider circuitry to scale down
a 3.3V input signal to the chip-internal 1.0V. If your board has this circuitry, add a multiply filter to
get correct values:
.. code-block:: yaml
sensor:
- platform: adc
# ...
filters:
- multiply: 3.3
2018-06-01 18:10:00 +02:00
2018-06-01 18:10:00 +02:00
.. _adc-esp32_attenuation:
2018-05-14 21:15:49 +02:00
ESP32 Attenuation
2018-08-24 22:44:01 +02:00
-----------------
2018-05-14 21:15:49 +02:00
On the ESP32 the voltage measured with the ADC caps out at ~1.1V by default as the sensing range (attenuation of the ADC) is set to ``0db`` by default.
Measuring higher voltages requires setting ``attenuation`` to one of the following values: ``0db``, ``2.5db``, ``6db``, ``11db``.
There's more information `at the manufacturer's website <https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/adc.html#_CPPv425adc1_config_channel_atten14adc1_channel_t11adc_atten_t>`__.
To simplify this, we provide the setting ``attenuation: auto`` for an automatic/seamless transition among scales. `Our implementation
<https://github.com/esphome/esphome/blob/dev/esphome/components/adc/adc_sensor.cpp>`__ combines all available ranges to allow the best resolution without having to compromise on a specific attenuation.
.. note::
2018-05-14 21:15:49 +02:00
In our tests, the usable ADC range was from ~0.075V to ~3.12V (with the ``attenuation: auto`` setting), and anything outside that range capped out at either end.
Even though the measurements are calibrated, the range *limits* are variable among chips due to differences in the internal voltage reference.
2018-06-01 18:10:00 +02:00
.. _adc-raw:
Different ESP32-ADC behavior since 2021.11
------------------------------------------
The ADC output reads voltage very accurately since 2021.11 where manufacturer calibration was incorporated. Before this every ESP32 would read different voltages and be largely innacurate/nonlinear. Users with a manually calibrated setup are encouraged to check their the installations to ensure proper output.
For users that don't need a precise voltage reading, the "raw" output option allows to have the raw ADC values (0-4095 for ESP32) prior to manufacturer calibration. It is possible to get the old uncalibrated measurements with a filter multiplier:
.. code-block:: yaml
# To replicate old uncalibrated output, set raw:true and keep only one of the multiplier lines.
raw: true
filters:
- multiply: 0.00026862 # 1.1/4095, for attenuation 0db
- multiply: 0.00036630 # 1.5/4095, for attenuation 2.5db
- multiply: 0.00053724 # 2.2/4095, for attenuation 6db
- multiply: 0.00095238 # 3.9/4095, for attenuation 11db
# your existing filters would go here
Note we don't recommend this method as it will change between chips, and newer ESP32 modules have different ranges (i.e. 0-8191); it is better to use the new calibrated voltages and update any existing filters accordingly.
2018-06-02 13:42:51 +02:00
.. _adc-esp8266_vcc:
ESP8266 Measuring VCC
2018-08-24 22:44:01 +02:00
---------------------
2018-06-02 13:42:51 +02:00
2020-12-21 00:48:42 +01:00
On the ESP8266 you can even measure the voltage the *chip is getting*. This can be useful in situations
2018-06-02 13:42:51 +02:00
where you want to shut down the chip if the voltage is low when using a battery.
To measure the VCC voltage, set ``pin:`` to ``VCC`` and make sure nothing is connected to the ``A0`` pin.
2020-12-21 00:48:42 +01:00
.. note::
To avoid confusion: It measures the voltage at the chip, and not at the VCC pin of the board. It should usually be around 3.3V.
.. code-block:: yaml
2018-06-02 13:42:51 +02:00
sensor:
- platform: adc
pin: VCC
name: "VCC Voltage"
Multiple ADC Sensors
---------------------
You can only use as many ADC sensors as your device can support. The ESP8266 only has one ADC and can only handle one sensor at a time. For example, on the ESP8266, you can measure the value of an analog pin (A0 on ESP8266) or VCC (see above) but NOT both simultaneously. Using both at the same time will result in incorrect sensor values.
2018-06-01 18:10:00 +02:00
See Also
2018-08-24 22:44:01 +02:00
--------
2018-05-14 21:15:49 +02:00
2018-06-01 18:10:00 +02:00
- :ref:`sensor-filters`
- :doc:`ads1115`
- :doc:`max6675`
2019-05-12 22:44:59 +02:00
- :apiref:`adc/adc_sensor.h`
- :ghedit:`Edit`