PWM Based LED Driver

** Updated code for better performance and stability [26 Feb 2015, rev.3]

It has been quite some time since my last post – as often happens, life intervenes and this time just a bit too much hospital time both for myself and one of my kids. Things are good now and normal can resume…

Black-Decker-FSL18FL-Firestorm-18-volt-FlashlightFor years I have had this Black&Decker FireStorm FSL18 flashlight, but hardly ever used it due to its anemic light quality. The fixture is powered with an 18v battery pack and uses a KPR18v0.3A bulb putting out 7 candlepower when running at 18 volts. 1 candlepower = 1 lumen. Compared with the brightness of the 12v power LEDs I have been working with lately, the brightness from this incandescent bulb is not much better than a glassed candle.

The trigger for upgrading this flashlight came one day when the wife complained an LED lightbulb had failed in her workshop. I replaced the bulb, but rather than throw it away, I took it down to my workshop to disassemble and identify what had broken.

It Fits!

The LEDs themselves were fine, but the power supply had fried. As I was turning the bulb’s heatsink in my hands, I happened to glance at the flashlight sitting on my workbench, and within 5 minutes of realizing the heatsink will probably fit in the case, the flashlight was already being disassembled. Within an hour, I had one of my 900 lumen LEDs mounted on the heatsink and with the help of my Dremel, had the reflector’s neck trimmed down to fit the LED perfectly.

Prototype PWM circuit

Since the LED cannot handle the 18v (20v when fresh from the charger) power from the battery, I considered using my analog LED driver circuit. I quickly gave up on the idea once I realized the incredible heat the FET and power resistors will need to dissipate inside the case. Great hand warmer in winter, but quite uncomfortable in summer. So I redesigned the circuit, this time using an ATtiny85 as the power controller.

The ATtiny85 is running an Arduino compatible core, and mostly relies on the PID library to calculate the proper PWM value to drive the FET. The input measures the voltage across the 1/2 ohm sense resistor. For better accuracy, I set the code to use the internal 1.1v reference in the ATtiny85 (Also available on the Arduino UNO), and passed the signal through a 2 stage RC low pass filter.

The low pass filter is needed to even out the square wave electrical flow to a relatively flat voltage the ATtiny85 ADC can use reliably measure the voltage differential across the sense resistor and accurately gauge the current.

Although the LED can easily handle up to 1A, the heat sink is not nearly large enough to keep the LED temps less than 80C in the enclosed space. Also the battery pack stores only 1AH of energy when new. No point in having a powerful flashlight if it goes dark in an hour! In the end I decided to keep to the 300mA load the original bulb draws. This keeps the heatsink to a cool 60C in the open and 70C when in the flashlight’s case – and I can easily get over 3 hours of constantly bright light. According to the LED datasheet, at 300mA, the LED delivers about 400 lumens – 57x more light than the original bulb!


The principle of operation is quite simple. The code takes two inputs, the first a reference voltage (setpoint) from a precision potentiometer on analog pin3, and on analog pin2 it reads the voltage across the sense resistor. These inputs are normalized with the map function and fed in to the PID. The PID function generates an appropriate value that is then fed into digital pin1 to produce a PWM output. The 5V PWM output from the ATtiny85 is not sufficient to drive the FET rail-to-rail, so the signal is fed into an LM358 op-amp to amplify the output to full VCC.

You are welcome to grab a copy of the code here: Arduino LED Driver

A small 78L05 voltage regulator is sufficient to drive the ATtiny85 and is a sufficiently stable power source for the setpoint. A 3.9K Ohm pull down resistor also had to be added to the PWM signal line to keep the light from briefly flashing at full power when the circuit is energized. I also added a 5 ohm resistor in series with the circuit to lower the input voltage from the max 20v from the battery. This helps the ATtiny85 maintain a more stable light output, when trying to control output when faced with 20v input. One PWM stepping change is quite visible in the LED output. When the battery voltage drops below 18v, the issue becomes less significant.

Boom. Blown regulator and op-amp

The only real problem I ran into during thebuild came at the very end. I tested everything with my bench power supply, but as soon as I connected the battery, the voltage regulator and op-amp would blow. At first I thought this was due to a power surge – the batteries being able to drive a much stronger initial power pulse than my bench power supply. After the op-amp and regulator had been replaced for the third time, I thought to check the battery polarity. Sigh. Whoever soldered the wires in this flashlight, reversed the red and black wires on the flashlight’s battery connector. Surprisingly, the ATtiny survived all of the reverse voltage events!

Eagle files for the dirver board are available here:


6 thoughts on “PWM Based LED Driver

  1. Pingback: PWM Based LED Driver - RLabs

  2. Pingback: PWM Based LED Driver -Use Arduino for Projects

    1. Elmars Post author

      The bulbs listed in the ebay ad are rated for 220 lumens, Based on the current I am running mine, the datasheet indicates I should be getting about 400 lumens. I doubt the cree bulb can deliver full 5W for an extended period of time, as there is no heat sink in these flashlights, so is most likely limiting current to maintain thermal maximums. Runtime for the cree wil be much better though, and effort to upgrade is minimal.

    2. tony nonya

      I loved your project but just seemed like I was going to invest too much time for this poor overworked light….so I took the battery apart, rewired batteries to parallel and now I have a 3.5 volt output for my LED and lasts for days….not as strong of a light as yours but still 100 times better than the old light

      1. Elmars Post author

        Your solution solves the problem with the worklight, but makes the battery useless with the other tools and cannot be charged with the regular charger. The module I made was to see if I can make a buck converter myself, but it is way cheaper to order one from china via ebay. Probably more efficient too.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s