2018-05-17 21:40:19 +02:00
|
|
|
Rotary Encoder Sensor
|
|
|
|
=====================
|
|
|
|
|
2018-11-14 22:12:27 +01:00
|
|
|
.. seo::
|
|
|
|
:description: Instructions for setting up rotary encoders.
|
|
|
|
:image: rotary_encoder.jpg
|
|
|
|
|
2018-05-17 21:40:19 +02:00
|
|
|
The ``rotary_encoder`` sensor platform allows you to use any continuous-rotation
|
2019-02-16 23:25:23 +01:00
|
|
|
rotary encoders with ESPHome. These devices usually have two pins with which
|
2018-05-17 21:40:19 +02:00
|
|
|
they encode the rotation. Every time the knob of the rotary encoder is turned, the
|
|
|
|
signals of the two pins go HIGH and LOW in turn. See
|
|
|
|
`this Arduino article <https://playground.arduino.cc/Main/RotaryEncoders>`__ to gain
|
|
|
|
a better understanding of these sensors.
|
|
|
|
|
2018-06-01 18:10:00 +02:00
|
|
|
.. figure:: images/rotary_encoder.jpg
|
2018-05-17 21:40:19 +02:00
|
|
|
:align: center
|
|
|
|
:width: 75.0%
|
|
|
|
|
|
|
|
Example of a continuous rotary encoder. Pin ``+`` is connected to ``3.3V``,
|
|
|
|
``GND`` is connected to ``GND``, and ``CLK`` & ``DT`` are A & B.
|
|
|
|
|
2019-02-07 13:54:45 +01:00
|
|
|
.. figure:: /components/sensor/images/rotary_encoder-ui.png
|
2018-05-17 21:40:19 +02:00
|
|
|
:align: center
|
|
|
|
:width: 75.0%
|
|
|
|
|
2022-09-26 14:40:47 +02:00
|
|
|
To use rotary encoders in ESPHome, first identify the two pins encoding the step value.
|
2018-05-17 21:40:19 +02:00
|
|
|
These are often called ``CLK`` and ``DT`` as in above image. Note if the values this sensor
|
|
|
|
outputs go in the wrong direction, you can just swap these two pins.
|
|
|
|
|
2018-11-19 18:32:16 +01:00
|
|
|
.. code-block:: yaml
|
2018-05-17 21:40:19 +02:00
|
|
|
|
|
|
|
# Example configuration entry
|
|
|
|
sensor:
|
|
|
|
- platform: rotary_encoder
|
|
|
|
name: "Rotary Encoder"
|
|
|
|
pin_a: D1
|
|
|
|
pin_b: D2
|
|
|
|
|
2021-12-26 20:41:59 +01:00
|
|
|
To modify additional parameters of pins like active state or pull-ups, you may add extra options.
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
# Example of advanced pin configuration
|
2022-04-03 09:30:25 +02:00
|
|
|
pin_a:
|
2021-12-26 20:41:59 +01:00
|
|
|
number: D5
|
|
|
|
inverted: true
|
|
|
|
mode:
|
|
|
|
input: true
|
|
|
|
pullup: true
|
|
|
|
|
2018-05-17 21:40:19 +02:00
|
|
|
Configuration variables:
|
2018-08-24 22:44:01 +02:00
|
|
|
------------------------
|
2018-05-17 21:40:19 +02:00
|
|
|
|
2018-06-01 18:10:00 +02:00
|
|
|
- **pin_a** (**Required**, :ref:`Pin Schema <config-pin_schema>`):
|
|
|
|
The first pin for determining the step value. Must not be a pin from an external I/O expander.
|
|
|
|
- **pin_b** (**Required**, :ref:`Pin Schema <config-pin_schema>`):
|
|
|
|
The second pin for determining the step value. Must not be a pin from an external I/O expander.
|
|
|
|
- **name** (**Required**, string): The name of the rotary encoder sensor.
|
|
|
|
- **pin_reset** (*Optional*, :ref:`Pin Schema <config-pin_schema>`):
|
2022-09-26 14:40:47 +02:00
|
|
|
An optional pin that resets the step value. This is useful with rotary encoders that have a
|
2018-06-01 18:10:00 +02:00
|
|
|
third pin. Defaults to no reset pin.
|
2019-02-26 21:14:23 +01:00
|
|
|
- **resolution** (*Optional*, string): The resolution of the sensor, this controls how many
|
|
|
|
pulses are generated by one step:
|
|
|
|
|
2022-04-03 09:30:25 +02:00
|
|
|
- ``1`` - (Default)
|
|
|
|
- ``2``
|
|
|
|
- ``4``
|
2019-02-26 21:14:23 +01:00
|
|
|
|
2018-06-01 18:10:00 +02:00
|
|
|
- **id** (*Optional*, :ref:`config-id`): Manually specify the ID used for code generation.
|
2019-03-14 16:41:37 +01:00
|
|
|
- **min_value** (*Optional*, int): The minimum value this rotary encoder will go to, turning
|
|
|
|
the knob further will not decrease the number. Defaults to no minimum.
|
|
|
|
- **max_value** (*Optional*, int): The maximum value this rotary encoder will go to, turning
|
|
|
|
the knob further will not increase the number. Defaults to no maximum.
|
2021-10-27 21:09:39 +02:00
|
|
|
- **publish_initial_value** (*Optional*, boolean): Controls whether the value is published
|
2022-09-26 14:40:47 +02:00
|
|
|
upon start of ESPHome. By default, the value is only published when it changes, causing an
|
2021-10-27 21:09:39 +02:00
|
|
|
"unknown" value at first. If you set this option to true, the value is published once after
|
|
|
|
boot and when it changes. Defaults to ``false``.
|
2021-11-02 19:32:30 +01:00
|
|
|
- **restore_mode** (*Optional*): Control how the Rotary Encoder attempts to restore state on bootup.
|
|
|
|
For restoring on ESP8266s, also see ``esp8266_restore_from_flash`` in the
|
|
|
|
:doc:`esphome section </components/esphome>`.
|
|
|
|
|
2022-04-03 09:30:25 +02:00
|
|
|
- ``RESTORE_DEFAULT_ZERO`` - (Default) Attempt to restore state and default to zero (0) if not possible to restore.
|
2021-11-02 19:32:30 +01:00
|
|
|
- ``ALWAYS_ZERO`` - Always initialize the counter with value zero (0).
|
|
|
|
|
2020-11-01 18:24:40 +01:00
|
|
|
- **on_clockwise** (*Optional*, :ref:`Automation <automation>`): Actions to be performed when
|
|
|
|
the knob is turned clockwise. See :ref:`sensor-rotary_encoder-triggers`.
|
|
|
|
- **on_anticlockwise** (*Optional*, :ref:`Automation <automation>`): Actions to be performed when
|
|
|
|
the knob is turned anticlockwise. See :ref:`sensor-rotary_encoder-triggers`.
|
2019-02-17 12:28:17 +01:00
|
|
|
- All other options from :ref:`Sensor <config-sensor>`.
|
2018-05-17 21:40:19 +02:00
|
|
|
|
2019-11-01 18:28:26 +01:00
|
|
|
.. _sensor-rotary_encoder-set_value_action:
|
|
|
|
|
2019-10-19 21:45:01 +02:00
|
|
|
``sensor.rotary_encoder.set_value`` Action
|
|
|
|
------------------------------------------
|
|
|
|
|
|
|
|
The internal state of the rotary encoder can be manually changed to any value with this action.
|
|
|
|
After executing this action, rotating the encoder further will increase/decrease the state relative
|
|
|
|
to the newly set internal value.
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
# Example configuration entry
|
|
|
|
sensor:
|
|
|
|
- platform: rotary_encoder
|
|
|
|
id: my_rotary_encoder
|
|
|
|
# ...
|
|
|
|
|
|
|
|
# in some trigger
|
|
|
|
on_...:
|
|
|
|
- sensor.rotary_encoder.set_value:
|
|
|
|
id: my_rotary_encoder
|
|
|
|
value: 10
|
|
|
|
|
|
|
|
# Templated
|
2023-01-05 00:34:00 +01:00
|
|
|
- sensor.rotary_encoder.set_value:
|
2019-10-19 21:45:01 +02:00
|
|
|
id: my_rotary_encoder
|
|
|
|
value: !lambda 'return -1;'
|
|
|
|
|
|
|
|
Configuration options:
|
|
|
|
|
|
|
|
- **id** (**Required**, :ref:`config-id`): The ID of the rotary encoder.
|
|
|
|
- **value** (**Required**, int, :ref:`templatable <config-templatable>`):
|
|
|
|
The value to set the internal counter to.
|
|
|
|
|
2020-11-01 18:24:40 +01:00
|
|
|
.. _sensor-rotary_encoder-triggers:
|
|
|
|
|
|
|
|
``on_clockwise`` and ``on_anticlockwise`` Triggers
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
With these configuration options, you can run automations based on the direction
|
|
|
|
that the encoder has been turned, and not the value that it currently holds.
|
|
|
|
These triggers ignore the min and max values and will trigger on every step.
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
on_clockwise:
|
|
|
|
- logger.log: "Turned Clockwise"
|
|
|
|
on_anticlockwise:
|
|
|
|
- logger.log: "Turned Anticlockwise"
|
|
|
|
|
|
|
|
|
2018-06-01 18:10:00 +02:00
|
|
|
See Also
|
2018-08-24 22:44:01 +02:00
|
|
|
--------
|
2018-06-01 18:10:00 +02:00
|
|
|
|
|
|
|
- :ref:`sensor-filters`
|
|
|
|
- :doc:`pulse_counter`
|
|
|
|
- :doc:`template`
|
2018-08-24 22:44:01 +02:00
|
|
|
- `Mechanical Input Library <https://github.com/jkDesignDE/MechInputs>`__ by `Jochen Krapf <https://github.com/JK-de>`__
|
2019-05-12 22:44:59 +02:00
|
|
|
- :apiref:`rotary_encoder/rotary_encoder.h`
|
2019-02-07 13:54:45 +01:00
|
|
|
- :ghedit:`Edit`
|