The following article describes LED light dimmer, based on software PWM (Pulse Width Modulation) generator with variable duty ratio. The microcontroller used for this project is PIC12F629 by Microchip.This is low end microcontroller which does not have internal hardware PWM module. That is way a software one has been built. The dimmer schematic is shown on the drawing below.
The dimmer is powered by one 12V voltage source. The microcontroller needs 5V to operate so a 7805 voltage regulator is used – U2. The control is represented by a sensor button – SW1, which is made by a conductive plate or other metal object with good conductivity (any shape could be used). Diodes D13 and D14 are connected in such way to protect the microcontroller from high positive or negative voltage coming from the sensor. If the voltage coming from the sensor is not in the range of 0÷5V it will be sent to ground or the voltage regulator. The sensor button is capacitive type. The microcontroller sets high level (5V) on the pin where the sensor is connected and charges the users finger with small amount of electricity. After very short period of time this pin is reconfigured as input and the microcontroller check the voltage. If the user’s finger has touched the sensor it has been charged and for small period of time it has been discharged through R1. During this interval the microcontroller perceive the high level as pressed button. For better disturbance protection R1 should have lower value (no less than 10кΩ). The voltage which is applied to the user’s finger is absolutely harmless. In order to guarantee the user’s safety a power supply with galvanic separation from the mains must be used (transformer or switching power supply with pulse transformer at its output for example). If another type power supply is being used an additional safety measures must be takes for user protection. The output signal comes from three of the microcontroller’s pins but only one of them could be used also. The increased number of outputs helps the schematic to continue functioning if one output has malfunctioned for some reason (overload for example). If all outputs are connected then in case of emergency all could be damaged. The signal flows through R2 to the base of Q1. The transistor is BC547C – low power bipolar transistor. Its current rating is 100mA and the maximum power dissipation is 0.5W. β=500 (also known as h21). This means higher energy efficiency and lower current through the microcontroller’s pins. The LEDs are connected in 4 parallel lines each containing 3 serially connected LEDs and a ballast resistor (200Ω). This resistor limits the current flowing through the LEDs and its value depends on their parameters. Because of that one should calculate the resistors’ value for himself.
To control the dimmer one should touch the sensor button to turn on or off the LEDs (easy intensity change instead of momentarily). If the sensor button has been held down the intensity of the LEDs decreases and if one needs higher intensity one should wait for it to decrease itself to zero and increase to desired point after that. Turning the LEDs off always lowers the intensity for a short period of time instead of turning off immediately.
In order to connect large amount of LEDs Q1 must be changed with more powerful one, keeping the base current lower than 20mA to protect the microcontroller from damage. It is possible a MOSFET to be used, but one supporting TTL input levels.
One can use a precompiled HEX file published below. Copy all the code. Open new Notepad window and paste the code in it. Save the file with a name of your choice and HEX extension. Use ANSI encoding. There must be one empty row at the end of the file. Start your preferred software for programming microcontrollers, choose PIC12F629 for programing and load the HEX file into it. Program the chip and it is ready to be used.
Compiled firmware in HEX file:
Copy everything between the rows filled with =
=========================================
:10000000D801D901DA01DB01DC01DD01DE01831653
:10001000FF23900083010C28F1238316051283121D
:1000200005160330A000A00B1328831605168312B3
:10003000051E232A6430DA005B08D800D901D808ED
:1000400003192628FF308500D8032A2885010000DF
:1000500000000000D90A590F1F28DA03031D1C28CD
:1000600083160512831205160330A000A00B362854
:10007000831605168312051E5E29DE01DE03831634
:100080000512831205160330A000A00B4528831625
:1000900005168312051E5C29DF080319D1285B0AA7
:1000A0000319CF28DB0A05305B02031859284030BA
:1000B000DA000A305B020318632805305B02031C78
:1000C00063282A30DA0010305B0203186D280A30EA
:1000D0005B02031C6D281C30DA0017305B0203182A
:1000E000772810305B02031C77281430DA001C30AC
:1000F0005B020318812817305B02031C81280F3034
:10010000DA0028305B0203188B281C305B02031CCA
:100110008B280B30DA0038305B0203189528283022
:100120005B02031C95280930DA005F305B0203187C
:100130009F2838305B02031C9F280730DA007F308D
:100140005B020318A9285F305B02031CA928053055
:10015000DA00C3305B020318B3287F305B02031C54
:10016000B3280430DA00C3305B02031CB928033023
:10017000DA00DA08031953295B08D800D901D80836
:100180000319C628FF308500D803CA28850100005E
:1001900000000000D90A590FBF28DA03B928DF018F
:1001A0005329DB0803195129DB0305305B020318CF
:1001B000DB284030DA000A305B020318E5280530FE
:1001C0005B02031CE5282A30DA0010305B020318BA
:1001D000EF280A305B02031CEF281C30DA001730CE
:1001E0005B020318F92810305B02031CF928143055
:1001F000DA001C305B020318032917305B02031C72
:1002000003290F30DA0028305B0203180D291C3057
:100210005B02031C0D290B30DA0038305B02031837
:10022000172928305B02031C17290930DA005F30D8
:100230005B020318212938305B02031C2129073097
:10024000DA007F305B0203182B295F305B02031C4E
:100250002B290530DA00C3305B02031835297F30C3
:100260005B02031C35290430DA00C3305B02031C37
:100270003B290330DA00DA08031953295B08D80058
:10028000D901D80803194829FF308500D8034C2923
:100290008501000000000000D90A590F4129DA0346
:1002A0003B29DF01DF038316051283120516033095
:1002B000A000A00B59294728DF01232A5E0FC62979
:1002C000DB08031D6629DD01DD0368295B08DD000D
:1002D0005D08DC00DC080319C32902305C02031846
:1002E00073290A30DA0003305C0203187D290230DA
:1002F0005C020730031C7E29DA0007305C02031819
:10030000872903305C02031C87290530DA000F308F
:100310005C020318912907305C02031C9129043008
:10032000DA001F305C0203189B290F305C02031CAB
:100330009B290330DA003F305C020318A5291F30E7
:100340005C02031CA5290230DA003F305C02031C6A
:10035000AB29DA01DA0ADA080319C1295C08D800E6
:10036000D901D8080319B829FF308500D803BC2962
:100370008501000000000000D90A590FB129DA03F5
:10038000AB29DC036A29DB01DE01232AFF30DB0015
:10039000DE00DC0102305C020318D0290A30DA00EA
:1003A00003305C020318DA2902305C020730031CB8
:1003B000DB29DA0007305C020318E42903305C0211
:1003C000031CE4290530DA000F305C020318EE2923
:1003D00007305C02031CEE290430DA001F305C0297
:1003E0000318F8290F305C02031CF8290330DA00E7
:1003F0003F305C020318022A1F305C02031C022AF1
:100400000230DA003F305C02031C082ADA01DA0A03
:10041000DA0803191E2A5C08D800D901D808031984
:10042000152AFF308500D803192A85010000000035
:100430000000D90A590F0E2ADA03082ADC0AFE3016
:100440005C02031CCA295E0F0D285B08D800D90185
:10045000D80803192F2AFF308500D803332A8501D5
:10046000000000000000D90A590A03190D28282AA3
:1007E20038308316850083128501073099008B010A
:0C07F200DB01D801DE01DF01DF0308009D
:02400E00843FED
:00000001FF
=========================================
Hi. can I instead of the sensor have a ldr
Maybe not with that firmware I think. You can try of course. I just haven’t thought of that option and don’t know if it will work.
I have tried but no luck
Why do I use Switching Mode Power Supply, will make the led lamp blinks?
Probably your power supply has some issue. Its output is not very stable and doesn’t react well to pulse loads. Try different supply of better quality.
Hi there
Please do not joke too much in sharing some knowledge on the C language code for the above program.
please share the C code so that we may learn and understand things better. You will not benefit anything by hiding the code !
thanks
sibujacob@gmail.com
Back then when I created this project I learned by trial and error principle. I didn’t use code from Internet. I imagined how it should work in principle and I started experimenting until I managed to develop some well functioning code. Anyway I might send you the C code if I find it. It was like 5-6 years ago when I worked on that project. I changed computers a few time since then and some old stuff is gone.
Hello, have the hex file for the 12f675???…..it’s verryyy similar processor…but not the same!
Or….it’s possible to have the C code …. i compile it for 12f675!
Greeeeatttt project 🙂
Hello! From what I know 629 and 675 have the same hardware but 675 has more memory. Have you tried to program 675 with this HEX file? I am not sure but I think it might work. I didn’t compiled it for this PIC. I also don’t have the compiler available. I used trial version and it expired long ago. If I recompile now it might not fit in the memory or it might perform differently because of lower optimization level. The C code is also specifically designed for this version of the compiler and will not compile correctly with newer version or different compiler like the new Microchip compiler. It can be adapted of course but the idea was to provide ready to use HEX file so everyone can build the circuit and program the PIC with any programmer.
Can you use example PIC 16F84A or smaller tha PIC 12F629.
PIC 16F84A is not compatible at all. It is also more expensive (3.11$ compared to 0.70$ for 12F629 according to microchip.com) and bigger (18 pins compared to 8). You can use smaller PIC like 6 or 5 pin but the code will not be compatible again. It has to be recompiled for the exact chosen device and the configuration needs to be changed for that PIC too.
well, I’m not yet really into C, still in learning phase, the PWM doesn’t really interest me, it’s the sensor bit.
I guess you do that in an interrupt, so the main program can continue doing what it has to, but that’s momentary out of my lead 😦
(would you be so kind to send the code rather than distributing it publicly?: pic@rcpt.at)
To be honest this is one of my first experiments with C for microcontrollers. It is not very good example for learning. You are right that the button reading should be put in interrupt but actually it isn’t. In fact there is no interrupt in the whole code. Everything is running in real time. As it is described in the article I make the pin output and set it to high level for a few clock cycles. Then I make the pin input and immediately read its value. If it is 1 (high level), I wait 100ms and and do the same again to confirm that the button was really pressed. If it was, I change the PWM duty ratio for example. While waiting 100ms I actually run the software PWM so the user doesn’t see glitches in the output. That is why I don’t use interrupt. It is just not possible. Of course there are other ways to build sensor button using low end microcontroller and using interrupt at the same time.
Could you plz post the C code?
thanks 🙂
I don’t post C codes here. They are more or less compiler dependent. I also don’t want to make my codes public.
The article describes how the dimmer is working and how the sensor button is implemented. If you have some specific question about the dimmer, please ask, but I don’t plan to post the C code.
Have a nice day!
Hello,
I don’t know the forum you mentioned. There are many like it I am sure. And a lot of people prefer to make something with their own hands instead of buying it. I do it too. I just don’t have enough time to spend on updating something which I am not going to use anyway. I didn’t add anything to my page for a long time for the same reason. At the moment I am working on another project but it is audio related (loudspeakers) so I will post about it soon I hope.
Have a nice day 🙂
no problem,
i will try to do so, and if i make it i will let you know.
thanks again.
Hello,
very nice project. It is very useful and very practical. I’m wondering if you could add some modes for example to have a button and control in modes the brightness.
0%, then press once the button and make the brightness to 10% then to 50 and then to 100%. Then press for 2 seconds the buttons and enter in emergency strobe mode.
This would be the only one project over the internet for a flashlight.
Best regards,
Hello,
Thank you for your comment. It is probably a good idea to add the brightness functionality as well as the emergency mode. It is possible I think. I actually didn’t think of flashlight when I started the project. It was ment to be a project for decorative lighting in my home. Later I switched to more powerful controller and I ended with better solution for my needs. After that I posted the article you see above for people who like to experiment with some simple schematics. It was used by many and seems that it still attracts new people even having in mind the cheap alternatives in eBay. I don’t have plans to build a flashlight and in fact I don’t have any plans to extend this particular project.
Best regards
Hi again,
there are a lot of people that trying to built a small flashlight with only this pic12f629. I dont know if you have seen the CPF forum about that. People are more crazy to build a simple code for that reason instead of buying from Ebay.
i didnt want to press you or to push you to make it, but would be great for many people.
thank you very much for your reply.
This circuit and software works perfectly!I put it in my LED flashlight,and it’s so great to have such a regulation.It saves energy also.I would like to have .asm if possible?I don’t know who did this project,but I would like to thank him for this.Greetings from Croatia to all of you!
Hello,
I did this project as well as all other projects you can find in my web site. I can not provide *.asm file because this project was built with Hi Tech PICC Compiler. So it is C-code and not assembler. The code is really simple and I don’t think it is worth posting it here. It is just a software PWM (“for” loop), simple button press check routine and duty cycle control (set a number for PWM loop).
Best regards!
Hello,
i build this schematic and try to program it, I dont know how to set the config settings in the programmer software:
BG1=?
BG0=?
CPD=?
CP=?
BODEN=?
MCLRE=?
PWRET=?
WDTE=?
FOSC2=?
FOSC1=?
FOSC0=?
can somebody explain to me how to set these config?
thanks very much!
hello,
im new with program microcontrollers, can you give me advise how to put the config settings?
thanks very much,
Hans
Hello Hans,
If I don’t forget something, all the settings are in the HEX file which you can find in this article. What I used is the following: __CONFIG(UNPROTECT & PWRTEN & BORDIS & MCLRDIS & WDTDIS & INTIO); // Code protection is disabled; BOR, MCLR and WDT are also disabled; Internal oscillator is used without clock output. Do not use this configuration with copy-paste because it is compiler dependent. I used HiTech PICC 9 Pro. If you use different version or entirely different compiler use only my comments to get the correct configuration.
Best regards!
hello, I tried your software and I like it a lot. I wanted to know the EEA can increase the speed of the dimmer. is possible to modify the software?
greetings
Andrea
It is possible I think but this is old project and I don’t keep the source because it was just for trying the controller.