Simulated Flame Confusions...

johnmobley

New Member
For some time now I have had several projects that I would like to attempt, all of which could greatly benefit from some sort of simulated flame. As time goes on, I keep coming up with more ideas with no idea of how to do it.

I have been looking online for any kind of information on this topic but it is apparently hard to come by. If I understand correctly, I believe that I need to use a PWM command in some way, with random duty cycles to create a simulated flame. However I am not sure how to create these random numbers for the PWM command.

I would appreciate any help that I can recieve in this matter. And if I am way off base on my understanding, I am always open to learning...the key to progress...
 

manuka

Senior Member
Dirt cheap ($US 50c) "Electronic Tea Candle" amber LED approaches exist of course. Before considering code, just how large,bright (& convincing !) and what colour do you want?
 

premelec

Senior Member
There was a complicated article in Circuit Cellar a few years ago - and I have some commercial 'candle' units that use two yellow LEDs and a CMOS 4060: they produce pretty good flicker effect with very simple circuit.... how big a flame do you need? :)
 

johnmobley

New Member
The projects very in size, but as of yet, there is nothing that needs to be large in scale. I was thinking about using a red and yellow LED, each driven by it's own PWM command so that the resulting colors of "orange", red and yellow are constantly changing. I have seen the Electronic Tea Candles but have never really been impressed with them.
 

goom

Senior Member
Here is some code that I picked up somewhere. I modified them to run on PICAXES rather that the Basic Stamp for which they were originally written. All credit goes to the original authors shown in the listings.
For a -18 or -14:
Code:
' © Parallax, Inc. • Application Note AN0001 Faux Candles • 5/2005
' =========================================================================
'
' File....... Faux_Candles.BS1
' Purpose.... Simulate six candles with LEDs; with wind/trigger input
' Author..... Team EFX
' E-mail..... [EMAIL="teamefx@parallax.com"]teamefx@parallax.com[/EMAIL]
' Started.... 19 MAR 2005
' Updated.... 01 JUN 2005
'
'PICAXE-18, -14
'
' =========================================================================
' -----[ Program Description ]---------------------------------------------
'
' Simulates candle flicker by passing random values to the "candle" output
' pins (P0 - P5). When TriggerWind is high, the candle appears to burn
' slowly and steadily. When TriggerWind is low, the candle flickers as
' though it was subjected to a stiff breeze.
' -----[ I/O Definitions ]-------------------------------------------------
SYMBOL TriggerWind = PIN1 ' active-low (1 -> 0)
' -----[ Constants ]-------------------------------------------------------
SYMBOL Yes = 0 ' for active low input
SYMBOL No = 1
' -----[ Variables ]-------------------------------------------------------
SYMBOL flicker = W0 ' random flicker
SYMBOL blankTest = W1 ' test for extended blank
SYMBOL candles = B4 ' new candle outputs
SYMBOL flickVal = B5 ' width of darkness
SYMBOL flickDly = B6 ' delay between updates
' -----[ Initialization ]--------------------------------------------------
Reset:
PINS = %00000000 ' all candles off
'DIRS = %00111111 ' make LED pins outputs
' -----[ Program Code ]----------------------------------------------------
Main:
RANDOM flicker ' tumble random generator
IF TriggerWind = 1 THEN Has_Wind
No_Wind:
   flickVal = %0011 ' load calm values
   flickDly = 12
   GOTO Chk_Blackout
Has_Wind:
   flickVal = %1111 ' load windy values
   flickDly = 45
Chk_Blackout: ' test bits for all zeros
   blankTest = flicker & flickVal ' isolate test bits
   IF blankTest > 0 THEN Update_Candles ' if not blank, do update
   flicker = flicker + 1 ' else insert a 1
Update_Candles:
   candles = flicker & %00111111 ' use active candle pins
   PINS = candles ' update LEDs
   PAUSE flickDly ' delay between updates
   GOTO Main
END
And for the -08M:
Code:
' =========================================================================
' File....... Faux_CandlesD4.BS1
' Purpose.... Simulate eight candles with LEDs
' Author..... Dennis Griesser
' E-mail..... [EMAIL="wolfstone@pobox.com"]wolfstone@pobox.com[/EMAIL]
' Started.... 5 July 2005
' Updated.... 9 July 2005
' {$STAMP BS1}
' {$PBASIC 1.0}
' =========================================================================
' -----[ Program Description ]---------------------------------------------
' Simulates candle flicker by passing random values to the "candle" output
' pins (P0 - P7).
'
' Features:
' o Each LED has 75% chance of being lit.
' o A single call to RANDOM drives all LEDs, using 2 bits of entropy each.
' o Each set of 2 bits has a different distance between them to reduce visible
'   "ripple".
' o The loop is unrolled for greater speed.
'
' We try to call RANDOM as infrequently as possible to keep things fast.
' We consume eight probabilities per iteration, but since we want the candles
' "mostly on", we can pick a percentage, like 75% and hard code that as using
' 2 bits of entropy. Thus a single call to RANDOM returns enough entropy to
' light all 8 candles, each one 75% of the time.
'
' RANDOM is documented as generating a sequence of 65535 pseudo-random numbers.
'
' Parallax does _not_ document the following:
' o RANDOM uses a linear feedback feedback shift register that is advanced
'   once per call.
' o So we're not _really_ getting 16 bits of entropy. We get one fresh bit,
'   and the other 15 bits are old, but shifted over one position to the left.
' o This may result in "ripple" of the lights.
'
' Note the way that the two bits are harvested per probability. Each candle
' uses two bits spread a different distance apart. This helps reduce ripple
' that would otherwise appear due to the use of a feedback shift register
' as the PRN generator.
' -----[ I/O Definitions ]-------------------------------------------------
' -----[ Constants ]-------------------------------------------------------
' -----[ Variables ]-------------------------------------------------------
  SYMBOL RndNum      = W0 ' 16-bit random number (and seed for next one)
  SYMBOL RndProb     = W1 ' 2/16-bit value of probability for this candle
  SYMBOL LitBits     = B4 ' composite bitmask of lit bits
' -----[ Initialization ]--------------------------------------------------
Reset:
  PINS = %00000000        ' all candles off
  DIRS = %11111111        ' make LED pins outputs
' -----[ Program Code ]----------------------------------------------------
Main:
  ' Top of the main loop.
  RANDOM RndNum                            ' tumble random generator - 1 bit of entropy, 15 bits shifted
  LitBits = %00000000                      ' no lit bits so far
  RndProb = RndNum & %1000000000000001     ' pick 2 bits that contain probability for this candle
  IF RndProb = 0 THEN AfterBit7            ' 2/2 bits off = 25% of time -> leave candle off
  LitBits = LitBits | %10000000            ' turn on this candle
AfterBit7:
  RndProb = RndNum & %0100000000000010     ' pick 2 bits that contain probability for this candle
  IF RndProb = 0 THEN AfterBit6            ' 2/2 bits off = 25% of time -> leave candle off
  LitBits = LitBits | %01000000            ' turn on this candle
AfterBit6:
  RndProb = RndNum & %0010000000000100     ' pick 2 bits that contain probability for this candle
  IF RndProb = 0 THEN AfterBit5            ' 2/2 bits off = 25% of time -> leave candle off
  LitBits = LitBits | %00100000            ' turn on this candle
AfterBit5:
  RndProb = RndNum & %0001000000001000     ' pick 2 bits that contain probability for this candle
  IF RndProb = 0 THEN AfterBit4            ' 2/2 bits off = 25% of time -> leave candle off
  LitBits = LitBits | %00010000            ' turn on this candle
AfterBit4:
  RndProb = RndNum & %0000100000010000     ' pick 2 bits that contain probability for this candle
  IF RndProb = 0 THEN AfterBit3            ' 2/2 bits off = 25% of time -> leave candle off
  LitBits = LitBits | %00001000            ' turn on this candle
AfterBit3:
  RndProb = RndNum & %0000010000100000     ' pick 2 bits that contain probability for this candle
  IF RndProb = 0 THEN AfterBit2            ' 2/2 bits off = 25% of time -> leave candle off
  LitBits = LitBits | %00000100            ' turn on this candle
AfterBit2:
  RndProb = RndNum & %0000001001000000     ' pick 2 bits that contain probability for this candle
  IF RndProb = 0 THEN AfterBit1            ' 2/2 bits off = 25% of time -> leave candle off
  LitBits = LitBits | %00000010            ' turn on this candle
AfterBit1:
  RndProb = RndNum & %0000000110000000     ' pick 2 bits that contain probability for this candle
  IF RndProb = 0 THEN AfterBit0            ' 2/2 bits off = 25% of time -> leave candle off
  LitBits = LitBits | %00000001            ' turn on this candle
AfterBit0:
  ' We are done assembling the bitmask of bits to light up.
  PINS = LitBits                           ' update LEDs
  ' If the flicker is too fast for you, you can put some delay here by un-commenting the PAUSE.
  ' Fiddle with different values for that "45" until you get something that you like.
  ' Larger values make it run slower. Smaller values make it go faster.
' PAUSE 45                                 ' delay between updates
  ' Update the candles again.
  GOTO Main
END
Both codes run in the simulator. I did have at least one running on a real PICAXE in the past. Hope you can use or adapt for your particular purpose.
 

Rickharris

Senior Member
for something visual that appears random, why not use the 'tune' command to drive the LEDs...
Just to expand on this - rather than put a sounder on the output put an LED there and play some sounds through it with the tune command.

Happy Birthday works well.

This produces a flickering LED - It is the way the cheap tea light approach does it.
 

3buns

New Member
Hi all, I know it's a very old thread, but my son and I came up with this code for a candle simulator for my wife's Moroccan lantern. I wanted a nice smooth flicker as would be in a lantern. We basically created a sawtooth PWM between 70% and 100% and varied both up and down slopes using two random number generators. The effect is a gentle flicker with the odd fast flick here and there.
Cheers, Kurt.

Code:
'Lantern.bas
'Kurt & Adam Nunn - 01/08/2012
'Revision 1.0
'Device PICAXE-14M
'Pinout
'        +ve  +-u-+  gnd
'  Serial In  |   |  Serial Out
'             |   |  
'             |   |  
'       Bulb  |   |  
'             |   |  
'             +---+  
symbol pwmcount = w3

soft_start:
	for pwmcount = 0 to 350 step 5
	pwmout pwmdiv16,2,124,pwmcount
	pause 20
	next pwmcount
up_rand:
	random w0
	w1 = w0//30
up_ramp:
	for pwmcount = 350 to 500 step 5
	pwmout pwmdiv16,2,124,pwmcount
	pause w1
	next pwmcount
down_rand:
	random w0
	w1 = w0//30
down_ramp:
	for pwmcount = 500 to 350 step -5
	pwmout pwmdiv16,2,124,pwmcount
	pause w1
	next pwmcount
	goto up_rand
end
 

Jeff Haas

Senior Member
Kurt,

I just gave your code a try on an 08M. Very nice - a slow, smooth change. It's different than what you see with the flickering LEDs in the tea candles.

The other way to give this kind of effect is to put an electrolytic capacitor across the lines going to the LED. Then you randomly flicker the LEDs and let the capacitor deal with the ramp up/down.

Jeff
 

3buns

New Member
Jeff - thanks. Our circuit drives 2 x 6v 300mA bulbs via a driver transistor, initially we did try the tune command and a fat capacitor across the base, but it didn't turn out that well. My wife was pleased with it, and looking at it in the dark this evening - it's kinda mellow.

Kurt
 

Jeff Haas

Senior Member
Ah, that makes a difference. I was using it to drive an LED. What kind of bulbs are you using? Got a link? You should post a schematic of the rest of the circuit too.

FYI, if you're using this directly to LEDs, I like it a bit better if you bump the frequency up (SetFreq M8).

Jeff
 

3buns

New Member
Hi, I had some 6v MBC bulbs in the box, the closest I could find as an example is the MES here at Maplin, http://www.maplin.co.uk/round-mes-type-e10-1977
Order code WL79L is the closest to what I had. The transistor is also one I had spare and was perfect rated at 1A, driven into saturation by the 330R resistor. Please find drawing attached.
The power supply was an old 5v 1A Iomega Zip Drive supply (remember those? :) ).
Cheers, Kurt.
CandleFlicker.PNG
 
Top