Touch pad dimmer with ESP32 & ESPHome
My goal was to have touch control over one or more LED strips. My requirements for the system:
- A single tap should toggle the LED strip. This toggling should use the default transition. In other words, if the the dimming behaviour needs different transitions, this shouldn’t interfere with the normal toggles.
- Holding the touch pad should smoothly increase or decrease the brightness the LED strip. This stops when the LED is either fully on or fully off.
- Releasing and then holding the touch pad should change the direction, i.e. if the light was getting dimmer, release & hold should start making it brighter.
From a coding standpoint, I had two more wishes:
- To add another touch pad + LED strip, it should require as little code copying as possible.
- The amount of
lambdain the YAML file should be kept to a minimum.
This is what I came up with. The ingredients you need to make it work in the
YAML file are:
binary_sensorfor the touch pad
esp32_touch:section to enable touch support
on_boot:section with some code to tie everything together.
- And finally the
touch-dimming.hfile you can download at the bottom of this post.
Here is a minimal example YAML file:
esphome: name: name-of-your-device includes: - touch-dimming.h on_boot: priority: 500 then: # This line ties the dimming behaviour to the light and the touch # sensor. You can copy it for every light+sensor combo you have. # The name ("dimmer" in this case) is just for debug logging. - lambda: new TouchDimmer("dimmer", the_light, the_touch_sensor); # Add a light and its output: output: - platform: ledc id: the_output pin: GPIO17 light: - platform: monochromatic id: the_light output: the_output name: The LED # Add a binary touch sensor: esp32_touch: binary_sensor: - platform: esp32_touch id: the_touch_sensor pin: GPIO13 threshold: 550 # The touch dimming code needs the ESPHome scripting # component. If your YAML file has no scripts yet, # just add this dummy. script: - id: dummy mode: single then:
new TouchDimmer() line will take care of everything for you. Just pass it
a name for debug logging, the ID of the
light, and the ID of the touch sensor.
Finally, download touch-dimming.h and place it next to the YAML file for ESPHome to pick it up and include it in your project.
Last updated for ESPHome version 22.12.3.