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 =
27 thoughts on “LED PWM dimmer with PIC12F629”
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.
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 !
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?: firstname.lastname@example.org)
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?
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!
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 🙂
i will try to do so, and if i make it i will let you know.
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.
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.
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!
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).
i build this schematic and try to program it, I dont know how to set the config settings in the programmer software:
can somebody explain to me how to set these config?
thanks very much!
im new with program microcontrollers, can you give me advise how to put the config settings?
thanks very much,
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.
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?
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.