Projects / ESPHome smart switch for a 12 V load

ESPHome smart switch for a 12 V load

Beginner1.5 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. ESP8266LM2596

    Set the LM2596 to 5 V

    Check with a meter before connecting.

  2. ESP8266

    Write secrets.yaml

    Your Wi-Fi and keys.

  3. ESP826610.3 mA

    Flash by USB

    esphome run workbench-strip.yaml.

  4. ESP8266

    Add to Home Assistant

    It appears as a new device.

  5. ESP8266ON10.3 mA

    Wire the relay and strip

    12 V only.

  6. ESP8266

    Test the button

    Works without Wi-Fi.

  7. ESP8266

    Add an automation

    On at sunset, off at 11 pm.

In short

How do you build an ESPHome smart switch for a 12 V load?

An ESP8266 with a relay, flashed with ESPHome, appears in Home Assistant as a switch and can also be pressed by hand. You need Wemos D1 Mini ESP8266, Relay Module 5V, Tactile push button 6 mm, 12 V LED strip warm white (5 m, 2835), 12 V 2 A DC adapter and LM2596 Buck Module. It takes 7 steps and about 90 minutes.

Level
Beginner
Time
About 90 min
Parts
6

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
12 V adapter plusLM2596 IN plus and the relay COMSupply for the strip and the buck module
LM2596 OUT (set to 5.0 V first)D1 mini 5V and relay VCCLogic power
Adapter minusLM2596 minus, D1 mini G, relay GND, strip minusCommon ground
Relay IND1 mini D1Switch signal
Relay NOLED strip plus12 V to the strip when on
ButtonD1 mini D2 and GManual switch (internal pull-up)

Code

ESPHome turns a small board into a Home Assistant device from a short YAML file instead of code: the file says which pin is a switch and which is a button, and ESPHome builds and flashes the firmware. Here the relay switches only a 12 V LED strip, which keeps everything low voltage; do not use this relay module to switch mains unless a qualified electrician builds that part in a proper enclosure. Set the LM2596 to 5.0 V with a meter before connecting the D1 mini, because the module's default output may be higher. Create a secrets.yaml file next to the YAML with your own wifi_ssid, wifi_password, api_key, ota_password and fallback_password, and keep it out of any public repository. The first flash is by USB; after that updates go over Wi-Fi. The button toggles the relay by hand even if Wi-Fi is down, and restore_mode makes sure the strip does not come on after a power cut. If the relay clicks on at boot, your module is active-high, so remove the inverted line. ESPHome is installed with pip or as a Home Assistant add-on.

# ESPHome config for the 12 V strip switch (ESPHome 2024.6 or newer). Put real values in secrets.yaml, never in this file.
esphome:
  name: workbench-strip
esp8266:
  board: d1_mini
logger:
api:
  encryption:
    key: !secret api_key
ota:
  - platform: esphome
    password: !secret ota_password
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  ap:
    ssid: "Workbench Strip Fallback"
    password: !secret fallback_password
captive_portal:
switch:
  - platform: gpio
    pin: D1
    name: "Workbench strip"
    id: strip_relay
    inverted: true              # most relay modules switch on with a LOW input; remove this line if yours does not
    restore_mode: ALWAYS_OFF    # starts off after a power cut
binary_sensor:
  - platform: gpio
    pin:
      number: D2
      mode:
        input: true
        pullup: true
      inverted: true
    name: "Workbench button"
    filters:
      - delayed_on: 30ms
    on_press:
      - switch.toggle: strip_relay

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Press the buttonStrip toggles
Toggle in Home AssistantStrip toggles
Power cut and restoreStrip starts off
Wi-Fi unpluggedButton still works

If it does not work

Relay clicks at boot

Remove or add the inverted line.

Not found in Home Assistant

Check the api key and that both are on one network.

Flash over USB fails

Hold the board in flash mode or try another cable.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the ESPHome smart switch for a 12 V load?

Wemos D1 Mini ESP8266, Relay Module 5V, Tactile push button 6 mm, 12 V LED strip warm white (5 m, 2835), 12 V 2 A DC adapter and LM2596 Buck Module.

How long does the ESPHome smart switch for a 12 V load take to build?

About 90 minutes for a beginner maker, in 7 steps.

How do I know the ESPHome smart switch for a 12 V load works?

Press the button: Strip toggles. Toggle in Home Assistant: Strip toggles. Power cut and restore: Strip starts off.

Relay clicks at boot: what should I check?

Remove or add the inverted line.

Not found in Home Assistant: what should I check?

Check the api key and that both are on one network.

Flash over USB fails: what should I check?

Hold the board in flash mode or try another cable.