/ #ESP32 #Home Assistant 

Control RGB LEDs with ESP32 and Home Assistant Automations

Control RGB LEDs with ESP32 and Home Assistant Automations

Ever wanted to add some pizzazz to your smart home with colorful, dynamic lighting? I recently tackled this project myself—using an ESP32 to control addressable RGB LEDs and integrating them seamlessly into Home Assistant. The result? A lighting setup that responds to automations, time of day, and even sensor inputs. Here’s how you can do it too.

Why This Project?

Addressable RGB LEDs (like WS2812B strips) are incredibly versatile. You can create everything from subtle mood lighting to eye-catching effects. Pairing them with an ESP32 and Home Assistant unlocks automation potential—imagine lights that fade in with your morning alarm or change color based on room temperature.

What You’ll Need

Before diving in, gather these components:

  • ESP32 development board (I used the ESP32-WROOM-32)
  • Addressable RGB LED strip (WS2812B or SK6812 work great)
  • 5V power supply (match the current requirements of your LED strip)
  • Breadboard and jumper wires (for prototyping)
  • Micro-USB cable (to flash the ESP32)
  • Home Assistant instance (running on a Raspberry Pi, server, or NAS)
Pro Tip: If your LED strip is long, inject power at multiple points to avoid voltage drop and color inconsistencies.

Step 1: Wiring the ESP32 and LED Strip

First, let’s connect the ESP32 to the LED strip:

  1. Power Connections:

    • Connect the 5V pin of the ESP32 to the 5V input of the LED strip.
    • Link the GND pin of the ESP32 to the GND of the LED strip.
  2. Data Line:

    • Connect the GPIO pin (I used GPIO 4) to the DIN (Data In) pin of the LED strip.
  3. Power Supply:

    • If using an external power supply for the LEDs, connect its 5V and GND to the strip, and ensure the GND is shared with the ESP32.
Warning: Don’t power long LED strips directly from the ESP32! Use an external power supply to avoid frying your board.

Step 2: Flashing the ESP32 with ESPHome

I prefer ESPHome for ESP32 projects because it integrates effortlessly with Home Assistant. Here’s the configuration:

# esphome/rgb_leds.yaml
substitutions:
  device_name: "living_room_leds"

esphome:
  name: ${device_name}
  platform: ESP32
  board: esp32dev

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

api:
  password: !secret api_password

ota:
  password: !secret ota_password

light:
  - platform: fastled_spi
    chipset: WS2812B
    data_pin: GPIO4
    num_leds: 60
    name: "${device_name}_strip"
    effects:
      - pulse:
      - random:
      - strobe:

Flash this to your ESP32 using:

esphome run rgb_leds.yaml

Step 3: Integrating with Home Assistant

Once flashed, the LED strip should appear in Home Assistant. Now, let’s create automations!

Example Automation: Sunrise Alarm

Here’s a YAML snippet for a gradual sunrise effect:

# configuration.yaml
automation:
  - alias: "Sunrise Alarm"
    trigger:
      platform: time
      at: "06:30:00"
    action:
      - service: light.turn_on
        target:
          entity_id: light.living_room_leds_strip
        data:
          brightness: 1
          effect: "Pulse"
          rgb_color: [255, 100, 10]
      - delay: "00:05:00"
      - service: light.turn_on
        data:
          brightness: 100
          rgb_color: [255, 200, 150]

Example Automation: Temperature-Based Colors

Want lights that change with room temperature? Here’s how:

# configuration.yaml
automation:
  - alias: "LEDs by Temperature"
    trigger:
      platform: numeric_state
      entity_id: sensor.living_room_temperature
      above: 25
    action:
      - service: light.turn_on
        target:
          entity_id: light.living_room_leds_strip
        data:
          rgb_color: [255, 0, 0]  # Red for hot

Troubleshooting Tips

  1. LEDs Not Lighting Up?

    • Check the data pin connection.
    • Ensure the num_leds in ESPHome matches your strip.
  2. Flickering or Weird Colors?

    • Add a capacitor (1000µF) across the power supply.
    • Use a logic level shifter if the ESP32’s 3.3V data line struggles.
  3. Home Assistant Not Seeing the Device?

    • Verify the api: section in ESPHome.
    • Restart Home Assistant after adding the device.

Going Further

  • Add Motion Sensing: Trigger lights when motion is detected.
  • Sync with Music: Use WLED for audio-reactive effects.
  • Voice Control: Integrate with Alexa or Google Assistant.

FAQ

Q: Can I use an ESP8266 instead of an ESP32?
A: Yes, but ESP32 is recommended for longer LED strips due to its faster processing.

Q: How do I calculate the power supply size?
A: Multiply the number of LEDs by 0.06A (per LED at full brightness). For 60 LEDs: 60 × 0.06 = 3.6A.

Q: Why are my LEDs stuck on one color?
A: Check your ESPHome effect settings. Some effects override static colors.


Ready to brighten up your smart home? With ESP32 and Home Assistant, the possibilities are endless. For more projects, check out my guide on monitoring home temperature with ESP32. Happy tinkering!