Step 5: Firmware

The main function of the firmware is to monitor the brightness control (via VR1, serial port, or IR remote) and control the output. At start, the brightness control via the potentiometer is read via A/D(analog-to-digital) converter and used as initial brightness/dimming level.

Main loop
The controller creates reference voltage V-REF (see "Reference Voltage Generator" circuit of the schematic), which connects to one of the built in comparator's positive input. Current sense voltage C-SENSE is compared against this voltage by the comparator. The microcontroller turns on the Q1 for a quick moment, then see if the inductor current was high enough to "trip" the comparator (which means that L1 current reached or exceeded the desired level). If the comparator did not trip, the "on-time" is increased and the cycle is repeated until the comparator trips. If the comparator trips, then the "on-time" is reduced a bit, then the loop continues. This forms a simple feedback-loop controller. Essentialy the output level is set by the reference voltage.

Reference voltage is created by outputting (software) PWM signal of duty cycle proportional to the desired voltage. The PWM output from the microcontroller is voltage clamped by a diode (to the same scale/level as the current sense voltage), then smoothed by R15 and C8 (RC filter). This reference voltage stays between 0 to 0.6V.

A/D converter is not suitable for reading the inductor current because of the (lack of) speed. Inductor gets short bursts of current, in microsecond intervals. Comparator can respond to a peak current like this, while A/D converter requires the input voltage to be stable while sampling.

The "on-time" signal is generated by the hardware PWM module. It is configured to generate PWM frequency between 32kHz to 175kHz (configurable). The frequency changes according to the dimming level. The lower the brightness the lower the frequency. By changing the frequency, the effective duty cycle of the output also changes. Combining the duty cycle change to the current change via the switch mode converter, much better dimming curve is achieved.

Output over voltage protection
When the output is open (i.e. nothing is connected, bad connection or dead LED) or too many LEDs are connected in serial, the output voltage can get too high for the MOSFET to handle. MOSFET used here can handle up to 60V between Drain and Source. Higher voltage can destroy the device (rather quick smoking death as I experienced a few times during development).
The output voltage is attenuated so that the voltage is safe for the microcontroller (below 5V), then fed to the comparator module. Internal reference voltage is set so that the comparator trips at about 59V (configurable in about 3V steps). The comparator is connected internally via hardware to "shutdown" PWM output when trips. So this output over voltage protection works instantaneously as it should. (Another reason I needed comparators built-in) When the protection kicks in (via hardware), the firmware detects it, and stops the operation, then blinks the status LED. Only power cycling can reset this condition.

Supply Voltage Sense
Supply voltage is read by A/D converter and the value is used to compensate for the output level, so that the output power is consistent over a range of supply voltages. Also, supply over voltage protection kicks in at xxV (configurable in firmware).

IR Receiver
IR remote signal is detected by the sensor and generates interrupts. The signal is decoded by the ISR (interrupt service routine).
You can control the brightness/dimming level, output on and off via the IR remote. When the brightness is changed via IR remote, the potentiometer is disabled until it moves again.
Only Sony remote protocol is supported at this time (of course you can always add other protocols).

Button switches are polled about every 4 millisecond and debounced in firmware. Brightness up/down function is implemented for now. (but they can do anything you want)
When the brightness is changed via the buttons, the potentiometer is disabled until it is moved again.
You can also connect external buttons via SPI port for quick & easy remote control. (see the schematic)

Bi-directional Serial/SPI Control
SPI style serial can be used to control this controller. Only brightness/dimming change is supported now. The 8 bit brightness data is sent out via the same port when you move the potentiometer. So if you connect two or more of this controllers via SPI port, all of the controller can be controlled by just moving one of the potentiometers (gang dimming). This comes in handy when you have a bunch of controllers to light a large room.
SPI port and the button switches share the same I/O ports, so the firmware determines the source of the signal by the duration of the pulse. Since humans can only push buttons so fast, pulses that are longer than about 47 microseconds are considered button push, and shorter ones are decoded as SPI signal.
The data format is straightforward - just send the brightness level in 8 bit format. That's it for now - maybe expanded to do other things...

Open Source
You can download the source code as well as the HEX file to program the microcontroller. I'd love to see someone extending my code.

<p>Have you measured the current through the LEDs? It looks like a rectifier and output capacitors should be used. When the MOSFET turns on, all of the inductor's current should flow through the MOSFET and none through the LEDs. The LEDs receive current when the MOSFET turns off. If the duty cycle is 50% and you want to use 700 mA, the LEDs would receive 350 mA. Great Instructable by the way.</p>
how much did everything cost you?
Hi. <br>I have a 96W LED chip I am trying to power. It requires 70-90VDC at 1.5A. I am having trouble finding a driver that can supply that kind of voltage (it's a new COB chip with 9 colors making it a full spectrum light for plant growth). Anyway any suggestions as to how I can supply this kind of voltage (I want to use an AC adapter). <br><br>Thanks!!<br>Matt
<p>SPEA-1 from Embuilt can be used to check the full THD and harmonic spectrum of any LED driver</p>
<p>Hi,<br><br>I have a 1m RGB (3x3W at max) 5050 light strip, and a 12V PSU, the chinese controller had 3x(smd fet and small resistors) in it.<br>Do you have any practical ideas how to make the driver stage more compact for 3ch? <br><br>Do I need V-sense for each channel?<br>At the bare minimal I can't go under 3 comparators?<br><br>it is not recommended to drive light in the 30-40 Khz range, because we use that for IR communication.<br><br>Thanks,<br>Arpad</p>
<p>Very nice design ! I'm curious if you recommend this pwm led driver for a photo studio light ?</p>
<p>Any Chance you still have some one or two if the PCBs? The Shop link returns an 404 </p>
<p>Great looking project. It seems you created this a while ago, have there been any alterations? Is this still considered a efficient universal method of driving leds? I have been looking at this and your poormans buck and they are both awesome looking projects, I already have components on order to build the poormans buck. Here is my problem though, there are some Higher Power leds from LEDENGIN that I'd like to use (30W, 10W) that run on 42V and 14.4/15V respectively. In boost mode, will this driver be able to power those? Otherwise I am limited to 3W bulbs which don't seem to emit enough light to replace incandescents around the house. Also, if I don't have a 3D printer, is there a website that will make them, kind of like a OSH Park but for 3D prints? </p>
<p>I forgot to mention, I have a lot of 12V power lying around and anything else above that would be a pain, that's why I'm particularly interested in the boost mode.</p>
I've been sitting here looking at the schematic, and I can't for the life of me figure out how it measures the current in buck/boost mode during the off time of the transistor. The sense resistor is effectively out of circuit.
I was thinking about using a DAC to generate the reference voltage on a laser diode driver I'm working on. The design is based on the PIC12F752. Have you ever tried doing it like that?
I haven't done it, but using the DAC would make the software a bit simpler. But the problem with the DAC contained in 8 bit PIC is that its low resolution. If I remember correctly the DAC only has 5 bit or so. Some PIC24 series have 8 bit DAC and that would be perfect. <br>
Yeah, it is a 5-bit DAC. 31 dimming levels probably aren't enough for fading effects, etc, but it's more than enough for modes in a flashlight or laser pointer. It looks like the PIC16F1704 and 1705 have an 8-bit DAC. The 16F753 has a 9-bit DAC. Getting more bits than that would force us to use packages with more than 14 pins.
Hi Aki! <br>Can You help me: <br>If i use your original firmware the light not absolutely dimming off (the LED does not go to the end(OFF)). Wich parametrs i can change to set dimming to lowest(off-min) and increase brightnes level(set max)? <br>
Dear Aki! <br>Thank You for answer.Error is the same. I want sent to You log file. How i can do it? <br>Thanks!
You can email me the log at aki@theledart.com.<br><br>
Dear author! <br>I make some changes in driver firmware. But if i build your own original or the modified project i get the error message : Build Filed! <br>I use: MP LAB IDE 8.90 <br>Thank You for answer!
There's an update to the firmware:&nbsp;https://www.instructables.com/id/Universal-High-Power-LED-Driver-Firmware-update/<br> <br> Please try that version. (Microchip changed the header file a bit around ver 8.70 that created the error, but I fixed that one.)<br> <br> Aki
Wow! Great work and thanks for this instructable! <br>i wanna order some PCB&acute;s, but first i want to know if i can strobe the PWM Signal without causing any problems. <br>Means i wanna build a dimmable LED Strobo. <br>I want a Frequency up to 1kHz with 0-100% brightness. <br>Thats why i can&acute;t use normal PWM drivers... <br>Is it possible with your PCB? <br>I guess with some editing of your code and connecting another potentiometer it should be even possible without an external uC - right?
Hi, can it work with led strip, like this for example: <br>http://www.ebay.com/itm/5M-5630-NON-Waterproof-Cool-White-SMD-Flexible-LED-Strip-Light-300-Leds-12V-/111000486427?pt=US_Car_Lighting&amp;hash=item19d825021b
Those LED strips are voltage driven (usually 12V or 24V), so you don't need a special driver. If you want to dim those, there are many PWM dimmers available. <br>
Dear author! <br>Thank you for great led driver! <br>Can you provide any information about PCB size. If i use provided (&quot;Universal-driver-rev2-gb.zip&quot;) PCB in result i I get such a size 102 mm x x25 mm by default. 102 mm x 25 mm is correct? Seems too small...... <br>Thank You!
That's the correct dimension. It is compact.<br> <br> Aki
Thank you for such a quick and teach-full reply. <br>You have helped me learn, and I appreciate your generosity. <br>Cheers Aki
Hi Aki <br>Awesome work ...I love this design. <br>Just one question (for a die-hard DIY'er), does the 10k ohm Potentiometer (VR1) need to be linear or logarithmic? <br>Many thanks friend. <br> <br>
Linear pot. The controller (firmware) does the logarithmic function, so the output brightness appears linear.<br> <br> Thanks!
Thanks for the great instructable! I have the parts on order with hopes of building this great LED driver. But I do have a question...I'm a beginner hobbyist in electronics so please take that into consideration. <br> <br>I see on the schematic that there's an ICSP header, and the pins match my Pickit3 so I'm good with that....but in the images of the PCB it looks like the ICSP header is used for connecting GND/+12V/BOOST-/LED+/BUCK-. Is this header also used to program the chip in circuit or is it programmed out of the circuit and put in? <br> <br>I'm sure that the information is in the instructable somewhere, and I've read it through and through, but my novice eyes just aren't finding it. I just don't understand how the same header could be used for both functions. <br> <br>Also if the header does indeed serve both functions, then does MCLR match up to GND or BUCK- when connecting the power supply &amp; LED strings? <br> <br>Thanks! <br>Charlie
ICSP is by its own, right under the PIC (around the middle on the PCB). Nothing connects to that port under normal use. It's used only when you upload the firmware. The power in and LED connections are on the right side, and the remote control connector is on the left.<br> <br> ICSP port on the PCB are just 5 holes. No headers are soldered. I just stick a 5 pin header into those holes and hold it to program the PIC, because it only takes less than 30 seconds to program. Hope this makes sense.<br> <br>
Yep, that makes sense! I also just noticed that on the schematic, &quot;Buck-Boost&quot;, &quot;Boost&quot;, and &quot;LED+&quot; are shown as individual pins. I assumed that the header pins on the right side of the PCB were ICSP when in fact they represent the individual power &amp; LED pins on the schematic. <br> <br>I understand it now. Like I said before, the info was there...I just wasn't seeing it. <br> <br>Thanks again! <br> <br>Charlie
Never mind! After some more looking at the PCB, it looks like an ICSP header wasn't populated, and I'm guessing that the header pins on the PCB for GND/+12V/BOOST-/LED+/BUCK- aren't technically shown on the schematic (unless I misunderstand something which is quite likely!). <br> <br>Thanks! <br>Charlie
Thanks for a great instructable and the inspiring ideas shared. I wanted to ask you why did you decide upon the reference voltage generation circuit instead on a voltage divider? It seems that the reference voltage is to be kept stable throughout the work of the driver and not dependent on other conditions. But I am no expert in PIC ASM and cannot be sure I've read the code and the writeup correctly.
Reference voltage needs to be stable against the power supply voltage. Because the&nbsp;purpose&nbsp;of this driver to supply LEDs constant current, and the reference voltage is used to compare against the current sense output.<br> By controlling the output so that current sense voltage matches the reference voltage, this driver keeps the output current constant.<br> <br> Using diode (more often zener diode) to create reference voltage is very common practice.<br> <br> Aki
Thank you for your answer. I am not very good at electronics and I have no formal education in the field. <br>I intend to start experimenting recreating part of this with an AVR and this is why I am trying to understand some of the logic before starting to prototype. <br>I just noticed that you use 7805 to provide stable 5v to the microcontroller. This way you already have somewhat stable voltage in your system. Unless the leds draw so much power that the power supply dips drastically and the voltage from the regulator falls, but in that case the current over the leds cannot be increased anyways. <br> <br>
Ok, in this&nbsp;particular&nbsp;circuit, I'm trying to create low voltage reference (0 - 0.6V) by&nbsp;clamping&nbsp;the voltage out of the PIC output. This output is a PWM signal, so the PIC can control the reference voltage from 0.6V down to almost 0V.<br> I wanted to allow 5V&nbsp;operation, and 7805 drops 1V or so - which is ok, but the reference needs to be&nbsp;steady.&nbsp;<br> <br>
Great driver. Can you provide single side PCB for the circuit for the simple DIY people.
Sorry but it's not that easy to turn this PCB into single layer. I always try to make my PCBs to have least number of layers, but this one kind of needs to be two layer to work well.<br> My &quot;Poorman's Buck&quot; PCB is single layer design. You might want to try that one.<br> <br> Aki
Hi, <br> i have seen your Poorman's Buck that one is also great. Thank for you great work. Keep it up.
Hello congratulations by Brazilian and design is great to have some doubt that would be very grateful if you can answer me. <br>I do not think the first component NTD5867NL N-ch MOSFET on my parents would be possible to replace it would have some similar example of this component. <br>segunto and did not quite understand how it would link several modules together to control pwm from just one. <br>thank you in advance for the immense contribution in providing the project.
If you can't find NTD5867NL, you can use many other N-channel MOSFET with following specs.<br> <br> Drain-Source voltage: 60V or higher<br> Maximum Drain current: 5A or higher<br> <br> Other specs are not that important. Form factor should be TO-251AA(I-Pak) or TO-220.<br> <br> I know those should work; <ul> <li> IRLI520NPbF <li> IRLU120NPbF <li> HUF75637P3 </ul> Good luck!<br> <br>
Hi Aki, <br> <br>Thank your very to share your projects. It really brings a lot of fun to my life. I made your universal High-Power LED Driver and it is working great. I bought a PICKIT3 from Microchip and tried to add some code for the IR remote control function. Now I could use '0-9' button to adjust the brightness to the pre-set value and turning on the light by cliking the 'Power' button. <br> <br>I am a novice for the PIC micro-controller, I am not quite clear about how the A/D and comparator usage. I want to use 2 or 3 18650 lithium battery to provide the power. I want to add the low voltage shutdown function to prevent battery overdischage. Could help to give some code example about this function? Say I want to shutdown the light when the supply voltage is under 6V(2 18650) or 9V(3 18650). <br> <br>I saw the 'shutdown_vol2 equ 24' in your code, but I did not figure out how to check the supply voltage and make action to turndown the light when the supply voltage is too high.
Look for a variable named &quot;sp_voltage&quot;. This variable holds A/D value of the S-SENSE input. 0xFF (255) corresponds to supply voltage of about 28.5V, so you can scale it to the number you need.<br> Just check this variable at the end of the main loop (note that it takes some time for the first conversion to be done), and shut down accordingly.<br> <br> Now the tricky part. A/D conversion uses PIC's supply voltage as a reference. So if it goes under 5V (happens when you give lower than about 6.5V to the circuit, because the 5V regulator needs some voltage) the A/D value changes. For us to know the exact voltage on S-SENSE, we need to have a known voltage supplied to the PIC.<br> PIC has an internal reference voltage you can use to determine the supply voltage (by calculation). You might want to check into that.<br>
Hi Aki, Thank your very to share your projects. It really brings a lot of fun to my life. I made your universal High-Power LED Driver and it is working great. I bought a PICKIT3 from Microchip and tried to add some code for the IR remote control function. Now I could use '0-9' button to adjust the brightness to the pre-set value and turning on the light by cliking the 'Power' button. <br> <br>I am a novice for the PIC micro-controller, I am not quite clear about how the A/D and comparator usage. I want to use 2 or 3 18650 lithium battery to provide the power. I want to add the low voltage shutdown function to prevent battery overdischage. Could help to give some code example about this function? Say I want to shutdown the light when the supply voltage is under 6V(2 18650) or 9V(3 18650). <br> <br>I saw the 'shutdown_vol2 equ 24' in your code, but I did not figure out how to check the supply voltage and make action to turndown the light when the supply voltage is too high.
Very nice!
Excellent instructable! As someone who is designing a low current higher voltage (into the 100s of V) boost converter it is interesting to read someone going through similar things that I am. That being said, your design makes mine look like crap! Yours is much nicer and much more fancy. Mine is very basic to solve a simple problem. <br><br>The only additional thing that would be nice to see is you do a follow up project for converting mains to either 12V (for this) or do a mains powered version of this. Obviously, it would b significantly bigger but it would have a bit more application for this. Either that or since this is OpenSource I may build off your design. I guess its based on what time/funds will allow.<br><br>
Thanks your comments!<br> <br> I'd imagine that voltage that high would be hard to do as a boost converter, since finding MOSFETs with breakdown voltage higher than 100V seems to be hard. Are you using a transformer?<br> <br> Anyway, 12 - 24V DC power supply is easy to find. Most people have a few wall warts laying around I think. Battery operation is nice too. Add to that as long as I've been playing with electronics, and I even have a electrician license, developing high voltage circuits still makes me nervous...<br> <br> Please do use my design partially or as whole, to build your projects. Do not forget to share that here on instructables!<br>
Its not much above 100V actually (around 120-130V) I just realized I worded that kinda wrong. No transformer, just an inductor.Otherwise pretty much a standard constant current boost circuit. Nothing too special, just designed to work on 8-24V.<br><br>Yeah, if I do modify your design to work on household voltages I def will post it on here.
Can it be used with voltage variable dimmer (1~10V DC) that Lutron sells?
Not sure what that dimmer does. Is it a dimmer or a controller that outputs DC voltage?<br>
It is just a simple 0-10 VDC lighting control dimmer, voltage variable lighting control signaling system.
Ok, just like old dimming system, pre DMX days.<br> No this circuit doesn't support such a system, but very easy mod to connect 0-10V dimming voltage to where the pot is. All you need is a resistor voltage divider to reduce the voltage within 5V.<br>

About This Instructable




Bio: I am an electronic artist living in Brooklyn, NY. I work with LEDs and microcontrollers to create beautiful objects.
More by ledartist:T962A SMD Reflow Oven Fix/Hack Color Organ Triple Deluxe II Aurora 48 - 48 RGB LED Sequencer 
Add instructable to: