Candle Simulator



candle photo The aim of this project is to create a credible simulation of the light of a candle. Candle light is usually warm and waves slowly with random oscillations caused by the air flowing in the surrounding environment. Using a random number generator to modulate a light emiter like a LED or a light bulb it is possible to create a very credible effect.



I tested both LEDs and small incandescent light bulbs and although LEDs require less current to produce the same amount of light, light bulbs tend to create a softer effect. In this circuit I'll use LEDs but nothing stops you from using light bulbs.

The brightness of the LED is digitally controlled using Pulse Width Modulation [1], generated by the micro-controller. The amount of brightness and its oscillations are governed by a random number generator based on a Linear Feedback Shift Register [2].

PWM or Pulse-width modulation of a signal or power source involves the modulation of its duty cycle, to control the amount of power sent to a load. This is because the average power delivered is proportional to the modulation duty cycle. With a sufficiently high modulation rate, passive electronic filters can be used to smooth the pulse train and recover an average analog waveform [1].

A linear feedback shift register (LFSR) is a shift register whose input bit is a linear function of its previous state. Applications of LFSRs include generating pseudo-random numbers, pseudo-noise sequences, fast digital counters, and whitening sequences. Both hardware and software implementations of LFSRs are common [2].

Circuit Implementation

I'll use both Microchip PICs 12F629 and 12F675 micro-controllers to develop the candle simulator. Each output pin of the micro-controler is limited to source or sink 25mA. This is enough to power a small 5mm LED but not a light bulb or even a more powerful 1W LED.

To power small light bulbs or more LEDs it is required to use a transistor like a bs170 n-channel mosfet, connected to the micro-controller. It will then provide the remaining power to the light bulbs or LEDs.


Simple circuit with one LED

The LED has a 68Ω resistor to limit its current to 25mA.

The math is simple:
Vcc = 5V, Vled = 3.3V, Idesired = 0.025A
I=(Vcc-Vled)/0.025 = 68Ω

More powerful circuit with a mosfet

It supports LEDs connected on connector J2 using an appropriated resistor or light bulbs directly.

To use with a 1W white LED and assuming the input voltage at J1 is 12V you can calculate the current limiting resistor the same way as above:

Vcc = 12V, Vled = 3.3V, Idesired = 0.3A
R = (12-3.3)/0.3 = 29Ω

There are no 29Ω resistors but you may use a 33Ω for safety.


Both circuits use a 78L05 power regulator to stabilize 5V to the micro-controller. This small regulator can handle the simple circuit with one led without overload. It can handle currents up to 100mA, so it's safe!

On the more powerful circuit, the transistor BS170 Enhancement-FET can drive a maximum current of 500mA, which should be more than enough for a 1W LED or several light bulbs in parallel.

The PIC micro-controller itself consumes about 1mA which is almost nothing.



Simple circuit with one LED

candle simple

More powerful circuit with a mosfet

candle simple


There are common components in both schematics:

  1. the 78L05 voltage regulator;
  2. the 100nF capacitor to decouple the micro-controller voltage;
  3. the PIC to generate the effect;
  4. the 4k7 MCLR pull-up resistor to keep the micro-controller out of reset.


The output stage depends on whether just a simple LED is enough or several small light bulbs are required.
There's no external crystal or oscillator because we are using the micro-controller's internal 4MHz oscillator instead.
The power supply should be able to provide 7Vdc/60mA or more in order to make the simple circuit work properly or for example 12V if you want the powerful one with 12V light bulbs.



The prototype was assembled in a pre-drilled, easy to cut, prototyping board for testing but there are two example PCBs for anyone wishing to build the project. The eagle files are available below at the download section.

PCB for a single 5mm LED

candle simple

PCB for lamps or more powerful LEDs

candle simple



The software implements a PWM generator and a random bit generator to drive the PWM!

The PWM is generated like this: timer 0 turns the output ON and starts timer 1 periodically; timer 1, when running, will count the amount of time after which the output should be turned OFF. When the time if over it turns the output and itself off.


Main loop.

The main function runs a loop, reading a random bit, updating the current brightness level based on the bit received and delaying a few milliseconds before repeating itself again.

Hex files, required to program the PIC micro-controller, are available below for download.

As an alternative, and if you like to see how it is done, the source code written in Cis available for download. It may be compiled with the demo version of the PICC compiler from HI-TECH.


Modifying the effect in the software:

At the bottom of the source file you have the main loop. It has 3 parameters that change the behaviour of the effect:

            i += 3; // if bit is 1 increment 10
            i -= 3; // if bit is 0 decrement 10;

        /* protect i within 0...100 limit */
        if( i<50 ) i=50;    // not too low so the LED doesn't go off completely
        if( i>80 ) i=80;    // not too high to obfuscate the surroundings

        /* configure t1value for pwm generation and pause */
        t1value = 65535-(100+99*i)+1; 
        for(pause=0; pause<6000; pause++);

If you modify the increment value (green) the changes between subsequent intensities of the LED will become more noticeable. Good values are between 3 and 5. Above these it will become very irregular.

If you modify the clipping IF values, you change the lowest and highest brightness the LED will achieve. For a good candle effect, similar to some wind blowing it, try to set lowest value to 30 or even down to 20 if the LED still remains too bright. The highest clipping value is usually fine at 80 but you may change it to 100 to allow the loop to light the LED up to its maximum, when the random bit generator produces a long sequence of ones!

The final and probably most important parameter is the pause variable of the loop. It controls how quick the circuit changes from one brightness value to the next one. You may try values from 2000 up to 6000, the higher the value, the softer it will become.


These are 2 videos that show the software in action:

Since the software implements a 16 bit shift-register to generate bits these are pseudo-random. Pseudo-random sequences repeat themselves after a while. So far I was unable to see any similarities between the random oscilations of the light during the days that it has been working! This means the sequence is long enough to seem random to people.


Here are all the files from this project available for download. I made two versions, one with the regular candle effect and another with a smoother/slower effect more apropriated for simulating fire with red/orange LEDs.


Eagle schematics and PCBs:


  1. Wikipedia definition of Pulse-width modulation
  2. Wikipedia definition of Linear feedback shift register


Published on Saturday 2007/06/09, last modified on Wednesday 2014/06/04