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!

Tuesday, December 13, 2011

About this blog

Welcome to my first post. This blog is about my new found love to electronics. I like to build electronics, currentely mostly based on arduino and would like to share them. I'm no engineer, and i havn't studied electronics, so please correct me if something is somehow wrong. So, have a nice day!