Candle LED with Simulated Flicker

Hello everyone! After finding out about the Arduino I ordered one. And I don't think I've unplugged it from my computer since!
Well I have done many small project and such so far, I'm finding a wealth of information all over the net. But by far this Forum as been one of my favorite resources. Any-who, I have finshed my first little project, it's not much, but as a first project I'm very proud of it! After reading Todbot's Spooky Projects Spooky Projects ‚Äď Introduction to Microcontrollers with Arduino ‚Äď todbot blog I converted a pumpkin into a LED lighted pumpkin, complete with glowing eyes! Well that wasn't enough, and in the spirit of Arduino I enhanced my LED pumpkin, and here's where my project comes in! The LED glows, but when I blow across a simple little switch I tossed together, the LEDs flicker like a candle does when you blow on it! Soon after the LEDs return to normal operation. I know this isn't much, but then again its my first Arduino project.

Here's the code: ( Excuse any mess. I need to clean it up a bit )

 * Wind Switch with Flickering LED
 * by Electrick_RWM (
 * Date Oct 18 2007 
 * Makes the LED on Pin 9 glow sloftly and when Switch or "Wind" Switch is toggled it flickers!  
 * After the LED flickers it returns to its normal glowing loop until blown on again 
 * Thanks to TodBot for the Spooky Arduino Projects! 
 * Without his tutorials I would have never been able to create this project
int ledPin = 13;             // choose the pin for the LED
int ledCandle = 9;           // choose the pin for the LED
int inputPin = 7;            // choose the input pin (For Wind Switch)
int val = 0;                 // variable for reading the pin status
int max_count = 60;          // counter for the flicker table
int max_count_Normal = 64;   // counter for normal glowing table
int count = 0;
int countTwo = 0;
byte Norm_Glow_table[] = {   10, 10, 10, 10, 20, 30, 30, 30, 40, 50, 60, 70, 80, 90,100, // The table for our normal glowing
                            140,130,120,110,100, 90, 80, 70, 60, 50,
                             40, 30, 30, 30, 20, 10, 10, 10, 10 };
byte Flicker_table[] = {      10, 10, 20, 30, 30, 30, 40, 50, 60, 70, 80, 70, 70,     // the table that tells us how to flicker
                            60, 60, 50, 50, 50, 60, 70, 80, 90, 100,
                            240,230,220,100, 80,70,70, 70, 80, 80,
                            140,130,120,110,200,210,220,220,100, 90,
                             40, 30, 30, 30, 20, 10, 10 };

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(ledCandle, OUTPUT);   // declare Wind led as output
  pinMode(inputPin, INPUT);     // declare Wind Switch as input

void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    analogWrite(ledCandle, Norm_Glow_table[count]);  // Runs through the Norm_Glow_table 
    count++;                                         // We stay in this loop till the switch is activaled
    if(count > max_count_Normal )
      count = 0;    // Helps makes sure our next normal glow doesnt start in an arbitrary place on the table
  } else {
    digitalWrite(ledPin, HIGH); // turn LED ON ~ Means our wind Switch worked
    digitalWrite(ledPin, LOW);
    for ( int i=0; i <= 60; i++) {  // This for loop runs untill the flicker table finishes
    analogWrite(ledCandle, Flicker_table[countTwo]);
    if(countTwo > max_count ){
      countTwo = 0;  // Helps makes sure our next flicker doesnt start in an arbitrary place on the table
    delay(40);  // the delay for our flicker, make it faster to to make it flicker a little more violently
  delay(40);    // the delay for our normal glow 

Here's a Picture of the board itself, as you can see, pretty simple::

Well I hope someone out there can put the code and/or info to some use! If you have any questions just email me, or send me a message on here. I'll upload a picture and maybe a video to youtube of the pumpkin in use in the next couple of days if people ask for it.


What wind-switch do you use?

Can you share a video?

Here is a close-up of the little Wind Switch. Really all it is, is Aluminum Foil hanging from a piece of wire, its light enough where if you were to try and blow it out like a candle it swings and makes contact, and completes the circuit. Its nothing special, but just a nifty idea that dawned on me when I almost used a Mic to receive the wind input. Because... Candles don't flicker when you talk loudly around them, so why use a Mic?

Here's a little video if it in action, Its not as vibrant in the daytime, but you can still see it function.

For now I'm off to go insert all these guts into that pumpkin in the background. Happy Halloween :stuck_out_tongue:


Super simple, super efficient.

simple but wonderful solution!

can you put up a more detailed picture or a schematic maybe? i am building this as part of a motion activated pumpkin project, and the flickering led would make it look really cool.

This flickering led is something like what I needed for my first project, but I wanted it to be random instead of a repeating pattern, plus more than one LED. So, I came up with this sketch. Basically it generates a random value between -40 and 40, and adds that to the previous PWM value, while keeping it within the valid PWM range. This makes the value go up and down, but kind of stick around the boundary values. For example, once it hits the brightest value, it's even probability that it will stay that bright.

After writing this sketch, I have changed the random function to pick from -35 to 40, which keeps the LEDs more on the bright end, but they still venture down to darkness.

Here is a video of this sketch in action: arduino five randomly flickering leds - YouTube

The circuit is simply five LEDs, one each on pins 5, 6, 9, 10, and 11, and each with a 1k resistor.

I'm working on a bigger project with this, so I'll probably start a new thread when I'm finished.

 * randomly flickering LEDs

int ledPin[] = {
  5, 6, 9, 10, 11};              // pwm pins only
int ledState[5];                 // last state of each led
long randNumber;

void setup() {
  for (int i=0; i<=4; i++){      // set each led pin as an output
    pinMode(ledPin[i], OUTPUT);       
  randomSeed(analogRead(0));     // seed the rnd generator with noise from unused pin

  for (int i=0; i<=4; i++){      // init each led with a random value
    ledState[i] = random(20, 201);

void loop(){ 
  for (int i=0; i<=4; i++){                  // for each led:
    analogWrite(ledPin[i], ledState[i]);     // set the pwm value of that pin determined previously
    randNumber = random(-40, 41);            // generate new random number and add that to the current value
    ledState[i] += randNumber;               // that range can be tweaked to change the intensity of the flickering
    if (ledState[i] > 200) {                 // clamp the limits of the pwm values so it remains within
      ledState[i] = 200;                     // a pleasing range as well as the pwm range
    if (ledState[i] < 10) {
      ledState[i] = 10;
  delay(100);    // the delay between changes