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

This ceiling ‘coffer’ is lit by four 5m LED strips cut slightly down to size. In 2017 digital LED strips were rare. If you wanted really, really bright light, analogue white LED strips were the thing to get. The lights don’t do anything clever except turn on when someone’s 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 stuff but to my mind it was too expensive for so trivial a task as dimming. I found an inline remote controlled dimmer (as cheap as £5 – below) with 5.5mm plug – but found no way to 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, an add-on, the ESP32 pin is set as a PWM output 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.

As of 2021 the concept is proven but read on to see the remaining snags.

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 MOSFET, described as a ‘TO-220 package’ can handle the 2a amps and 12 volts needed to light and dim the lights. There are several problems to solve:

  • as per the spec, the MOSFET does indeed get warm and thus a heatsink will need to be screwed to its metal tab for long term use.
  • the light output during the fading and dimming is not linear. The result is that a slow fade up appears to do nothing for too long. I think the solution will be an ‘automation routine’ where the first part of the fade-up runs 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. As you see it here the LED strip gets only 10v of the 12v available, and consequently it is slightly less bright. The solution might be to solder in a more suitable logic level MOSFET – a IRL520 has been suggested.
  • 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. Is there 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 voltage drop to cause the strip end to be dimmer.
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’.

  • In short, add and customize the code below using the Esphome editor. See below for the options available.
  • Save, Validate and then COMPILE the code and DOWNLOAD BINARY.
  • Get the app called ESPflasher > connect the ESP32 to a 5v FTDI adapter and then your PC with a USB lead.
  • In ESPflasher, choose the COM port that’s just appeared, 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.
# 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    

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 frontend or 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. The dimmers act independently and get out of sync with each other. However in both examples, the dimming is pretty awesome.

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. Before you implement this remember my earlier points about the need for a heatsink at 2a; Consider also making the wiring more beefy (eg 1mm copper) and adding a jump lead across the skinny module track.

Leave a Reply

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