automated fade up dimmer for analogue LED Strips – using a MOSFET and ESPhome

This ceiling ‘coffer’ is lit by four 5m LED strips cut down to size fitted in 2017. If you wanted really, really bright light, these analogue white LED strips were the thing to get. At £8 for 5m at 150 LED/m and high lumens I could not complain. Today you might use digital, addressable and dimmable LED strips but at several times the cost. My analogue LED lights didn’t do anything clever except that they turn on when someone is in the kitchen (use a RCWL-0516 microwave motion detector and ESP32). A Home Assistant automation ensures that they don’t turn on at night when that would surprise someone just out of bed, grabbing a late snack.

This project aims to make the lights fade up and fade out gently whenever motion triggers them. Surveying the market I found Lutron equipment but to my mind it was too expensive for so trivial a task as dimming. I also found an inline remote controlled dimmer (as cheap as £6 – below) with 5.5mm plug – but it doesn’t make the lights fade up when turned on.

I wanted my dimmer to work in Home Assistant so that a sensor would control it and I knew that an ESP32 dev board could trigger an event on one of its pins. Using ESPHome in Home Assistant, an ESP32 pin is set so it can pulse the 12v supply to the LED strip and control its brightness. I needed a MOSFET they tell me – a MOSFET is a sort-of relay. If I attach a MOSFET to that pulsing pin I’d be able to switch the power to the LED strip and have a dimmer.

And it works as below! I have however included the snags that I met on the way to a solution.

inexpensive dimmer – but it doesn’t fade up or down when turned on

finding a MOSFET that handles lots of current and is triggered by a tiny signal

For the initial experiment I used an Arduino IRF520 Mosfet module for the convenience of the connections below. The IRF520 MOSFET’s physical shape is described as a ‘TO-220 package’. It can handle the 2a amps and 12 volts needed to light and dim one of the light strips. There are several problems to solve:

  • as per the spec, the MOSFET does indeed get warm. If I used this MOSFET I would need to bolt a heatsink to its metal tab for long term use.
  • the light output during the fading and dimming is not linear. It’s not a big deal but the slow fade up appears to do nothing for a good while before we notice the light. A possible solution will be to make the first part of the fade-up run fast and the last part of the fade up run slower.
  • the IRF520 Mosfet isn’t really a logic level device. It needs more volts on the signal pin to fully open the MOSFET gate. In other words, the LED strip gets only 10v of the 12v available, and consequently it is slightly less bright. The solution might be to solder and swap in a more suitable logic level MOSFET – a IRL520 has been suggested. I used a IRLb8721 30v 62a.
  • as I have four LED strips in the ceiling I must insert a MOSFET for each or else control everything in one switch. Annoying I’ve already invested in tidy 5.5mm male and female plugs for all my connections between the LED strip and power supply. I may need a box where I can put everything and connect it up.
There’s unnecessary wiring here. The red 12v live from the power supply can be connected directly to the LED strip as it’s the GND that is switched by the MOSFET. Strangely the ESP32 doesn’t need to feed the red module with 5v + … we just need a GPIO16 and GND.

BTW about these extra bright analogue LED lights

  • The LED light strips were branded LEDMO and they came either with or without a 2A 12V power adapter (that’s <24W per strip). Because I need four LED strips I bought an aluminium cased mains powered transformer that delivers 150W at 12V.
  • Each strip has 600 x 2835 LED chips giving 2700K white. At 120 LEDs per metre we get possibly up to 9,000 lumens per 5 metres. Each strip cost GBP £15 in 2017 via Amazon. The total cost of the project was 4 x £15 + £40 power + £20 smart plug + £20 cables and connectors.
  • The strips come with the 5.5mm x 2.1mm power sockets/plugs you’ll find on CCTV cameras, routers and LED strips. I needed to buy a four to one male to female adapter to power the strips from one point; some 17 gauge (1mm) 12v wire; strip end connectors and plugs.
  • With 12v I notice no concerning brightness drop over 5 metres where the strip is dimmer.

wire the the ESP32, power supply and the LED strip light

The MOSFET module saves some of the effort of wiring it all up. One ESP32 has enough output pins to control all four LED strips via four MOSFETs. I considered the need for a heatsink if the light use 2a. Instead I soldered and swapped the MOSFET for a I used a ‘IRLb8721 30v 62a’. Consider making the wiring more beefy (eg 1mm copper) and adding a jump lead across the skinny module track.

the result of the setup described on this page.

go to ESPhome in Home Assistant, add a ‘new node’ and use this configuration

Go to the ESPHome section of Home Assistant and create a new node (+ sign). A ‘node’ is the place where you tell the ESP32 what you want it to do – you do that in lines of text below, adding for example your wifi details.

The following code can replace the code in a newly created ‘node’.

# Create a new node in ESPHome. Add these lines to the yaml file 
substitutions:
   devicename: ledstrip
 esphome:
   name: ledstrip
   platform: ESP32
   board: esp32dev
 wifi:
   ssid: ""
   password: "
   manual_ip:
     static_ip: 192.168.1.XX
     gateway: 192.168.1.1
     subnet: 255.255.255.0
 # Enable hotspot (captive portal) in case wifi connection fails
   ap:
     ssid: "ledstrip fallback hotspot"
     password: "password"
 captive_portal:
 logger:
 api:
 ota:
 
web_server:
   port: 80
 
output:
 platform: ledc
 pin: GPIO15
 frequency: 20000Hz
 id: ledcout 

light:
  - platform: monochromatic
    output: ledcout
    name: ${devicename} light20s
    default_transition_length: 20s
 
  - platform: monochromatic
    output: ledcout
    name: ${devicename} light5s
    default_transition_length: 5s 

switch:
  - platform: restart
    name: ${devicename} restart    
  • Customize the code above using the Esphome editor.
  • Save, Validate and then COMPILE the code and DOWNLOAD BINARY.
  • Get the app called ESPflasher > if the ESP32 board doesn’t have USB connect it via a 5v FTDI adapter to your PC with a USB lead.
  • In ESPflasher, choose the COM port that appears, choose the binary file you just made and click Flash firmware.
  • If nothing happens click View the logs. After pressing one of the buttons on the ESP32 you might see ‘waiting for download’ which suggests now is the time to click Flash firmware.

how to configure the ESPhome code

  • All of the above was adapted from the ESPhome guide to LEDC
  • I don’t recommend that you have two dimming transition buttons in Home Assistant. Test the system and pick whether you prefer say, a 5 second or 20 second dim transition duration.

go to Home Assistant > Integrations to add your dimmable light

  • Go to the Home Assistant Configuration. Choose Integrations. If the LED hasn’t already been discovered automatically, add ESPhome and enter the IP address you put in the code above.
  • Back at the Home Assistant frontend add a ‘light card’ for your dimmer entity/s. In this case the configuration created one dimmer that takes 5s to complete and another dimmer that takes 10s to complete. When I had two dimmers acting independently they get out of sync with each other. However in both examples, the dimming is pretty awesome.

2 Responses

  1. Louis-Philippe Descamps says:

    looking pretty cool.
    i m thinking to do the same for Christmas lights where they would slowly fade in and out.
    do you think that would work?

    • roger says:

      It would work. You may get more mileage and colour and fun using ws2815 and similar light strips and wled + esp32 as a controller.

Leave a Reply

Your email address will not be published. Required fields are marked *