# ESPHome config for one motorized roller blind — ESP32 + 28BYJ-48 stepper + ULN2003.
# Companion to the MCU Atlas page /projects/motorized-roller-blinds-esp32-ball-chain-stepper/
# (the eg321 web-UI firmware route needs none of this — this file is the Home Assistant route).
# Replace MyNetwork / MyWifiPassword and the api encryption key before flashing.
# Compile-verified with ESPHome 2026.11.0 (board esp32dev, arduino framework).

substitutions:
  name: roller-blind-1
  friendly_name: Roller blind 1
  # Steps for one full open-to-close travel. Calibrate once: drive the blind fully
  # open, note the position counter, drive it fully closed, and set the difference.
  # 4096 half-steps is a typical starting guess for a geared 28BYJ-48 build.
  travel_steps: "4096"
  # 28BYJ-48 + ULN2003 runs reliably at or below ~250 steps/s (ESPHome's own guidance);
  # lower it if the motor skips under load.
  step_speed: "200 steps/s"

esphome:
  name: ${name}
  friendly_name: ${friendly_name}

esp32:
  board: esp32dev
  framework:
    type: arduino

logger:

api:
  encryption:
    key: "PM6X8++VY/FTLnmDTMj9IVKv1aBUzXf4l6FmlgjT2Nw="

ota:
  - platform: esphome

wifi:
  ssid: "MyNetwork"
  password: "MyWifiPassword"
  ap:
    ssid: "${friendly_name} Fallback"
    password: "12345678"

# IN1-IN4 of the ULN2003 board on GPIO 16-19 (any four free GPIOs work; swap the
# pins here if your blind runs the wrong direction).
stepper:
  - platform: uln2003
    id: blind_stepper
    pin_a: GPIO16
    pin_b: GPIO17
    pin_c: GPIO18
    pin_d: GPIO19
    max_speed: ${step_speed}
    acceleration: 100 steps/s^2
    sleep_when_done: true

# The stepper alone never appears in Home Assistant — pair it with a cover, and
# the blind becomes a real cover entity you can automate (open at sunrise, close
# when the afternoon sun hits).
cover:
  - platform: template
    name: ${friendly_name}
    id: blind_cover
    has_position: true
    lambda: |-
      if (id(blind_stepper).current_position >= ${travel_steps}) {
        return COVER_OPEN;
      } else if (id(blind_stepper).current_position <= 0) {
        return COVER_CLOSED;
      } else {
        return (float) id(blind_stepper).current_position / ${travel_steps};
      }
    open_action:
      - stepper.set_target:
          id: blind_stepper
          target: ${travel_steps}
    close_action:
      - stepper.set_target:
          id: blind_stepper
          target: 0
    position_action:
      - stepper.set_target:
          id: blind_stepper
          target: !lambda "return (int) (pos * ${travel_steps});"
    stop_action:
      - stepper.set_target:
          id: blind_stepper
          target: !lambda "return id(blind_stepper).current_position;"

# Note: unlike the eg321 firmware, this config does not save the blind position
# across power cuts — after a reboot it starts from closed. Re-calibrate (or just
# drive it fully closed) after any power loss.
