In this project a LED is used to mimic a flickering candle as closely as possible. When using a LED to behave like a candle, it may not be possible to sway the flame, but what can be achieved is the random variation of intensity.

The intensity of the LED can be varied using a random number generator and PORTB of the microcontroller implement the intensity control. To simulate the color of light of a candle, a warm white or yellow LED can be used.

The picture below shows the diagram of the flickering candle. 

Schematic and Build

led candle sch

We have used the ATMega328 board. The LED is arranged in the current sink mode and four I/O pins of PORTB are used to connect to the LED with a series resistor of 100Ohm. The voltage on the LED is 3.5V so each pin will sink around 15mA. The maximum current through the LED is the 60mA.

We have made this project on a breadboard connected to the ATMega328 board. The board can be purched at a piece of paper of about A6 size (10cm by 15cm), roll it into a cylinder, then glue the edges together so it remains that way. Then place the cylinder over the top of the LED. This will diffuse the light from the LED.

Five leads are connecting the pins from PORTB to the breadboard that contains also the four resistors and the LED.

Software Code

The compiled code, along with the MAKE file can be downloaded below. The code runs at a frequency of 16 Mhz. The output of the random generator is put on PORTB so random numbers of LED channels are switched on. between two port updates, a random amount of delay is executed.

program name: flickering candle
date: 26-oct-2014
target device : ATMEGA328

#define F_CPU 1600000UL

int main(void)

unsigned long lfsr = 1;
unsigned char temp;
DDRB= 0xFF; // set PORTB as output

lfsr = (lfsr >> 1) ^ (-(lfsr & 1u) & 0xd0000001u); // taps 32 31 29 1
temp = (unsigned char) lfsr; //take lowermost eight bits

PORTB = temp;//Give the value of 0 to the pins declared output

temp = (unsigned char) (lfsr >> 24);