Saturday, December 17, 2011

LED Candle Light Flicker with Arduino

I've build a simple candle light with an LED which is powered by an arduino. I have one of these cheap candle lights, but they aren't very smooth and they don't look like a flame, so build one myself.




What you will need:

Wire the led and the resistor up like in this schematic:
LED Flicker Setup

Now upload the following code to the arduino:
#define FLICKER_LED_PIN 10

// the start of the flicker (low)
static int flicker_low_min = 200;
static int flicker_low_max = 240;

// the end value of the flicker (high)
static int flicker_high_min = 230;
static int flicker_high_max = 256;

// delay between each low-high-low cycle
// low->high |flicker_hold| high->low
static int flicker_hold_min = 40; // milliseconds
static int flicker_hold_max = 80; // milliseconds

// delay after each low-high-low cycle
// low->high->low |flicker_pause| low->high...
static int flicker_pause_min = 100; // milliseconds
static int flicker_pause_max = 200;  // milliseconds

// delay low to high and high to low cycle
static int flicker_speed_min = 900; // microseconds
static int flicker_speed_max = 1000; // microseconds

void setup() {
  pinMode(FLICKER_LED_PIN, OUTPUT);
}

int flicker_random_low_start = 0;
int flicker_random_low_end = 0;
int flicker_random_high = 0;
int flicker_random_speed_start = 0;
int flicker_random_speed_end = 0;

void loop() {
  
  // random time for low
  flicker_random_low_start = random(flicker_low_min, flicker_low_max);
  flicker_random_low_end = random(flicker_low_min, flicker_low_max);
  
  // random time for high
  flicker_random_high = random(flicker_high_min, flicker_high_max);
  
  // random time for speed
  flicker_random_speed_start = random(flicker_speed_min, flicker_speed_max);
  flicker_random_speed_end = random(flicker_speed_min, flicker_speed_max);
  
  // low -> high
  for (int i = flicker_random_low_start; i<flicker_random_high; i++) {
    analogWrite(FLICKER_LED_PIN, i);
    delayMicroseconds(flicker_random_speed_start);
  }
  
  // hold
  delay(random(flicker_hold_min, flicker_hold_max));
  
  // high -> low
  for (int i = flicker_random_high; i>=flicker_random_low_end; i--) {
    analogWrite(FLICKER_LED_PIN, i);
    delayMicroseconds(flicker_random_speed_end);
  }
  
  // pause
  delay(random(flicker_pause_min, flicker_pause_max));
}

Everything in the flicker is randomized. You can change each parameter, the get the flicker faster or much slower, glowing like.



The candle light is mostly done. Now cut a 2-3 cm piece of the hot glue stick and drill a 5mm hole in it (diameter of the led). Stick the led in it, and the light is nicely diffused. The get the best result, place a piece of paper around it like in the photo above.

Hope you like it!

15 comments:

  1. Replies
    1. Just simple Board with USB connector running with ur PC (PC - Privet Computer -> Any Computer -> Windows or Apple. A lot ppl dont understand what PC is. Everyone think it is only Windows. That what Apple use to sell Laptops, 2x more MONEY.) There is all kinds of Arduino Boards. On that Board is Microchip that U program it with code above. The open-source Arduino environment makes it easy to write code and upload it to the i/o board. It runs on Windows, Mac OS X, and Linux. The environment is written in Java and based on Processing, avr-gcc, and other open source software. We all share codes and getting help from ppl around the world if we need help. One think a lot of ppl dont explain is, U pull out programed Microchip and use on the Board for project, saving $45-$25 for new Arduino. Microchip is about $7, so put back on ur Board and program it again. Link to Arduino: http://arduino.cc

      Delete
  2. This looks awesome. Could anyone tell me what it would take to run several of these? Could they be run off a single Arduino (with different PWM ports and separate code for each)? I'm new to this, but this project is just the sort of thing I'm looking for. Any help would be appreciated- thanks!

    ReplyDelete
    Replies
    1. Yes, you would just define another PWM pin...

      #define FLICKER_LED_PIN1 3

      Set it up for OUTPUT in setup and then just add code to the loop in both of the analogWrite sections

      analogWrite(FLICKER_LED_PIN, i);
      analogWrite(FLICKER_LED_PIN1, i);

      Delete
  3. Hi! How many mcd's does the led you used have?

    Nice idea, i'll try to replicate it.

    Regards

    ReplyDelete
  4. Shouldn't one use pink noise rather than white noise to approximate the flicker spectrum of a candle?

    ReplyDelete
  5. I don't know! How to apply pink noise? I can test it, if i know how to generate pink noise. Btw: Brownian noise is pretty relaxing: http://archive.org/details/TenMinutesOfWhiteNoisePinkNoiseAndBrownianNoise

    ReplyDelete
  6. Thank you for the nice laid out example. I used your code and adapted it for the Microchip PIC 12f683 here: https://github.com/silviuvulcan/12f683-led-candle

    Please let me know if that is not ok with you.

    ReplyDelete
  7. Thank U. That is nice one. More simple and so easy, that I have seen before

    ReplyDelete
  8. thank you for the idea, but is there a code for the attiny45 ? the cost are lower

    ReplyDelete
  9. My husband is back!!! I had a problem with my husband 8 months ago,which lead to us apart. When he broke up with me,I was no longer myself,I felt so empty inside .Until a friend of mine told me about one of her spells that helped her in same problem too that she found on a television program. i emailed the spell caster and I told him my problem and I did what he asked me. To cut the story short,Before I knew what was happening,not up to 48 hours,my husband gave me a call and he come back to me and told me he was sorry about what has happened, I'm so grateful to this spell caster and i will not stop publishing his name on the internet just for the good work he has done for me.If you need his help,you can email him at (ezizaspellhome@gmail.com)or tel +2347068534025.....thank you sir great Dr.EZIZA for all that you do and i greatly appreciate that.

    ReplyDelete
  10. I was not happy until i met Dr.Agbazara through these details +2348104102662 OR agbazara@gmail.com because my husband has left me and never had the intention of coming back home. But just within 48 hours that i contacted Dr. Agbazara my marriage changed to the positive side, At first my husband came back home and since then my marriage has been more peaceful and romantic than ever before

    ReplyDelete
  11. Dear Admin,
    Its very spectacular, that's the website you admire I likable it.
    I have in addition this connected web site.
    Candle making as a craft hobby is becoming increasingly popular as more and more people discover this absorbing and rewarding hobby which can be done from the comfort of their own home.
    Original LED Kerze
    Best Regards
    Jennifer Fraser

    ReplyDelete
  12. nice blog
    thanks for sharing information.
    I have also this related site.
    Years ago, candles were a necessity, as they were often the primary source for light. Yet LED candles are a much safer choice. You'll find that LED candles are so versatile.
    LED Kerze
    Best Regards
    Marlene Lorie

    ReplyDelete
  13. Hi my name is Sanaullah alam can I add frequency 25Khz-40Khz in the Arduino sketch for camera lighting application

    ReplyDelete