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. You might use digital, addressable and dimmable LED strips (eg WS2811) but at a few times the cost for a bright 5m strip. 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 – so I must re-do a lot. I may need a project 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 from the ESP32 to the MOSFET module.

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 might need to buy a four to one male to female adapter to power the strips from one point; some 17 gauge (1mm) 12v wire; LED strip end connectors and plugs. However it might be safer still to solder my own plugs using fat silicone wires and use WAGO connectors.
  • With 12v I notice no concerning brightness drop over 5 metres.

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 built-in MOSFET for a ‘IRLb8721 30v 62a’. You could make the wiring more beefy (eg 1mm copper) and add 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 device’ and use this configuration

Go to the ESPHome section of Home Assistant and set up a new device (green button). Here you will tell the ESP32 what you want it to do using the lines of text below. You’ll add your wifi details, and the pin/s you connected to the MOSFET/s. (btw ‘new device’ used to be called ‘new node’ which had me wondering what a ‘node’ is. A node is a device with an IP address on your network).

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 
   devicename: ledstrip
   name: ledstrip
   platform: ESP32
   board: esp32dev
   ssid: ""
   password: "
     static_ip: 192.168.1.XX
 # Enable hotspot (captive portal) in case wifi connection fails
     ssid: "ledstrip fallback hotspot"
     password: "password"
   port: 80
 platform: ledc
 pin: GPIO15
 frequency: 20000Hz
 id: ledcout 

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

  - platform: restart
    name: ${devicename} restart    
  • Customize the code above using the Esphome editor.
  • Save, Validate and then COMPILE the code and DOWNLOAD BINARY. (Update: this is now called ‘Install’)
  • 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 (as below) 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. While I was testing this configuration I created one dimmer that takes 5s to complete and another dimmer that takes 10s to complete. When I had two dimmers acting independently they got out of sync with each other. However in both examples, the dimming is pretty awesome.

15 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.

  2. Alex says:

    Hello, i hope you and your family are all in good health. Im trying to follow your tutorial. But the wiring diagram does not do much sense to me. I am not an electronics technician but i can follow instructions.
    The drawing says GND and GPIO16, but the lines end up in VCC and SIG on the mosfet board.
    Then you have V+ and Vin empty but in the photo i see you have al connectors used.
    Abusing of your kindness can you elaborate on how to wire it?

    • roger says:

      I’ve corrected that now. I’m so sorry for an error in the schematic wiring diagram which now shows correctly above. The post diagrams have now been edited – thank you for alerting me Alex.

      So as you correctly say, GND and GPIO16 on the ESP32 should be connected to GND and SIG on the MOSFET board.

      If it makes more sense (and it does) you can connect (red) V+ and Vin as the photos of my try-outs show. However to save unnecessary wiring in the ceiling, connect the red from the power supply directly to the LED strip. That’s allowed because the MOSFET dims (or switches) only the negative line.

      In retrospect, the ceiling project is a long standing success. While there’s a lot of confusing wiring in the ceiling, the lights turn on dim at night and bright during the day. I might soon adjust (in Home Assistant) that to save some energy as I’m using 40W when everything is on.

  3. Paul says:

    Hi Roger,
    Many thanks for sharing the article, it’s given me inspiration for a couple of projects. I’m no electronics expert, although I do OK hacking things together, this MOSFET though has got me stumped.

    I have a USB supply powering both the esp32 and the 5v led strip being powered from pins VIN and GND from the esp32. I’ve wired it exactly as your diagram above, with the GND from the led strip connected to GND on the MOSFET, and the PSU GND connected to V- on the MOSFET. I’ve also tried switching around the GND connections. I’ve flashed the esphome code to the device and in HA I can switch the MOSFET, the red led shows on or off as expected.

    however I try wiring it though, I just can’t get the led strip to come on!

    As a test I followed your diagram that includes unnecessary wiring, with both + and – connected to both sides of the MOSFET. This results in the led strip being on permanently!

    This is the MOSFET board I’m using

    I wondered if you had any advice as to what I may be doing wrong?

    • roger says:

      Cheers for writing in with a real conumdrum. I’m away so I have added a link which I used to get my understanding.

      I’ll write more when I return

      Dronebot workshop MOSFETs

      • roger says:

        Hello Paul
        I hope you’ve made progress. As it’s likely that you’ve wired it well I’d look first at the code (during compilation and the ESPhome log window) . Consider changing the ESP pin that you use as my board is surely different. Do see if the transition time and frequency needs changing for your setup.

        The only other difference from my project is that 5v LED strip you’ve used. I hope it’s a two wire analogue strip that lights fully when given 5v. Also I assume your usb PSU can do enough amps for the number of LEDs on the strip. In the case of the ‘unnecessary wiring setup’ which will defo work I think that if I put a voltmeter on the wires going to the LED strip I’d see 5v. See if you get other readings when the light is seemingly off.

        • Paul says:

          Hey Roger,
          Thanks so much for the link, it’s definitely given me an understanding that I was previously lacking.

          I’ve switched to a 5v 2a psu, and the led strip is 2 pin analogue, 60 leds/m, 120 total. It looks like my mis-wiring to start with actually burned the MOSFET. I was testing it last night, room was slightly darker, and I could see it turning the leds on, just very dimly.

          So thanks to amazon, I had a 10x pack of MOSFETS (why buy 2 when you can get 10 for the same price eh?), I switched it out for a new one, and wham! it worked perfectly!

          Just about to hook it up to the esp properly now, along with a touch switch, and hopefully drop in in to the 3d printer enclosure tonight. I’ve got a 3 day print coming up so I really need externally controlled lighting. I’ve currently got a pi4 running octoprint hooked up through a buck converter to the printer 24v psu, so should be good to power an esp and a small led strip too.

          Thanks for your help with this, really appreciated, you definitely set me up on the right path 🙂

          • roger says:

            Cheers Paul. Pleased to hear this is working out and that you’re working it out by having spare bits. I think we’d agree that it’s a challenge to fix when things are broken. I have more recently been playing with WLED and 5v addressable LEDs (eg for a staircase light) and I’ve similarly broken things and been stuck. Usually the failure was due to the wrong voltage. (Octoprint is awesome so enjoy that too).

      • Paul says:

        Thanks for the link Roger, that’s an excellent video for getting to grips with MOSFETs and has really helped my understanding.

        It turns out with my constant messing around to try and get it working, I broke it. Thanks to a late evening working on it, it turns out the LED’s did actually come on, just really really dimly. I switched out the MOSFET for a different one and boom! worked perfectly. Well, in my test environment anyway. Just need to incorporate it into my 3d printer setup now..

        Thanks for your help Roger, it’s definitely set me on the right path.

        • roger says:

          Qu1 I appreciate that it works and that most of the advice is overcautious, so I wondered how you decided on a 5v 2a PSU for 120 LEDs.

          Qu2 I’m also wondering how is this LED strip used in/with your 3D printer

  4. Paul says:

    Yeah, I’ve got a few WLED devices around the place. It’s a nice bit of software. Funnily enough, I’ve also just built some stairway lighting, although after careful consideration I built it in esphome rather than WLED.

    I’m still using as ws2812b strip, but only with a single colour. I also only wanted a single effect, a “wipe” that follows you up or down the stairs. I used a couple of VL53L0XV2 Time-of-Flight sensors, and even managed to get it to count how many people (or animals) are on the stairs. It took me a while to get the logic right, but so far it works really well. You can check out the code here

    The ToF sensors will need some callibrating once installed, now I just need to finish the plasterboarding so I can actually install it!

    • roger says:

      Goodness me Paul, your ToF sensor has been fully off my radar so thank you so much for that. I really will investigate that sensor. eg The water shortage is affecting our supply I want to monitor the filling of water tanks with some device. What I have at present is how much power a pump uses.

      Alas I need another lockdown to get my LED strip project written up. The ‘problem’ with WLED is having too much choice such than nothing is ever ‘done’.

      My staircase WLED / ws2812b is a bit easy: it uses one of the microphone chips recommended by WLED Sound reactive project. The mic can sense your approach to the stairs and play a sequential light effect up the LED strip. At other times (WLED has a scheduler for different effects am/pm) each foot on the staircase triggers a light effect. WLED sends the sound trigger to other strips (eg for disco effects) and in this case pulses the strip near my desk to alert me.

      Anyway, good luck and thank you for the inspiration.

  5. Paul says:

    I tried a few different sensors, from PIR (to many false positives), Microwave RCWL-0516’s (further research revealed some very erratic behaviour), and finally settled on the ToF. I’ve come across many posts of people using these for measuring fluid levels so could be a good option for you. Testing for my scenario has proven then to be very accurate. I’m interested to know how you get on with the project.

    I agree, another lockdown could be good to give some extra time to “tidy a few things up”. I’ve (mostly) finished a few different projects recently, in my spare time, and would love to find the time to write them up and post them properly. I’ve just finished hacking together the hardware setup for a whole home voice controlled audio system. Just need to get the 3 day print started for the enclosure case now!

    To answer your questions; The 2a psu was used for testing and developing. I’ve moved the ESP and the MOSFET into the printer. I’ve got an Ender 3 Max. The 24v PSU has a spare power line that I’ve tagged onto with a 3A buck converter to drop it down to a fixed 5v. This powers the pi4 with Octoprint, the ESP with the MOSFET and a touch sensor ( I may add a camera to this in the future to compliment the Pi Cam), and the LED strip. A single touch of the switch will toggle the LED strip, and a 7s touch will trigger a safe shutdown of the pi and then the smart plug that powers the whole rig. I also have a HA automation that triggers a safe shutdown of the rig if a print finishes during the night. I have the entire setup in an enclosure and the led strip is cable tied to the framework at the front, up both verticals and the top horizontal. With the inside of the enclosure being heat proof and reflective it provides a nice even distribution of light.

    You WLED project is definitely intriguing. I’m guessing you need to recompile the source for the appropriate includes? Not a path I’ve ventured down, yet 🙂

    I also dropped you a LinkedIn request as I thought it could be good to connect.

    • roger says:

      Hello Paul – you really are having fun there with the 3D printer – if not a Guinness record for print time!

      The use of Octopi with the power plus lighting are indeed cool and useful. (Use a regular webcam, which may have a more convenient mount, on Octopi).

      Your last point suggests a look at WLED Sound Reactive No compiling see link. Your need an addressable 5v LED strip and a 5v PSU, ESP32 board, and a microphone INMP441 which costs doodly.
      I will of course write up my simpler version of it – simpler than Dr Zz. It’s like your MOSFET project, uses just one pin on the ESP32 so you’re pretty much there.

      This blog of projects will be my aide-memoire and logbook. It’s another version of documenting code with comments.

Leave a Reply

Your email address will not be published.