Touch pad dimmer with ESP32 & ESPHome

Recently I’ve been toying a lot with ESPHome in combination with Home Assistant. In this post I’ll show how I made a touch-pad LED dimmer.

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 lambda in 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:

  • A light with its output
  • A binary_sensor for the touch pad
  • An esp32_touch: section to enable touch support
  • An on_boot: section with some code to tie everything together.
  • And finally the touch-dimming.h file you can download at the bottom of this post.

Here is a minimal example YAML file:

  name: name-of-your-device
    - touch-dimming.h
    priority: 500
      # 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:
  - platform: ledc
    id: the_output
    pin: GPIO17

  - platform: monochromatic
    id: the_light
    output: the_output
    name: The LED

# Add a binary touch 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.
  - id: dummy
    mode: single

The 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.

Download touch-dimming.h

Last updated for ESPHome version 22.12.3.

dr. Sybren A. Stüvel
dr. Sybren A. Stüvel
Open Source software developer, photographer, drummer, and electronics tinkerer