It’s time to get those jack-o-lanterns twinkling for Halloween. If you don’t want to use candles or buy a jack-o-lantern light this Halloween you can do like [Johannes Bauer] and code your own pseudo-random flickering super bright LED. His wife wanted their pumpkin to be illuminated this year and he knew it would be easy to do with an Arduino, but that would be overkill for such a simple project. Plus, he doesn’t have an arduino. [Johannes] used very few components; 4 slightly depleted AA batteries, a super bright LED, 680 ohm resistor and a little custom code on an 8 pin ATtiny13. The circuit does work great for a pumpkin lantern but his video is more of a tutorial on coding linear congruential generator (LCG) for the 8 bit pseudo-random LED flickering.
The code is short and can be gleaned from the YouTube video. [Johannes] used avr-gcc to compile and has packaged his code and build scripts for download. The hex file can be flashed over to the chip using avrdude or AVR Studio. If you have any ATtiny13s lying around you should cobble this hack together just in time to emulate that real look of a pumpkin candle without the hassles and hazards of real flames.
If you want something with a lot more light that still has that candle like flicker then checkout “Flickering Pumpkin Lanterns” that used the signal from LED tea lights to power some 12 V lamps.
Follow along after the break to watch [Johannes Bauer’s] video.
This isn’t a hack. It’s an LED randomly going on and off.
“It’s not a hack. It’s [description of end result that totally ignores tools, method, materials, or anything else that could actually be used to determine if it’s a hack].”
The code uses an 8 bit “random” generator which cycles after 256 calls. The code also requests 20 numbers a second – so the light flickers with a 13 second sequence and then repeats over and over. I think a 16 bit generator would have been better…
Yup, that’s true. Although I don’t think the 13 second repetition is a problem (because I guess few people will stare at a flickering pumpkin for 13 seconds straight), it can easily be fixed by using a uint16_t and a multiplier of 65 (also efficient without multiplication libs) and an increment of 30677.
Joe: Two questions… How do you get good parameters for the LCG (the multiplier and the offset)? Also, have you experimented with xor-shift generators? These have the usual pattern of:
xs16 ^= (xs16 <> b);
xs16 ^= (xs16 << c);
and a good example is a = 13, b = 9, c = 7.
Totally mangled…
xs16 ^= (xs16 << a);
xs16 ^= (xs16 >> b);
xs16 ^= (xs16 <<c);
So why not grab the least significant bit off an analog read?
Yup, that’s a possibility. However with an open pin or pin fixed to a certain voltage it’s hard to ensure that the LSB is really random (and not influenced due to systemic effects such as the 50Hz/60Hz background and such). So I’d rather use the LSB to seed a PRNG, which would ensure entropy pool mixing and yield better results.
Isn’t there a built in random number generator in arduino? Doesn’t arduino support attiny85 (at about $1 a piece)? The rest is some batteries, a resistor and an LED.
First off, thanks Johannes for taking the time to share this. My one suggestion would be to use the PWM to modulate the intensity of the LED instead of making it on/off. A real candle is always lit, so it always produces some light. Vary the PWM duty cycle from 20% on the “off” state and 100% in the “on” state, or ramp it between those duty cycles for bonus points.
Thanks for your kind words.
Initially I planned on using the PWM generator (and I have the LED connected to the OC pin for this exact reason); the result would certainly look better, but I just didn’t bother for this project. Should be very easy to integrate however.
Eggzacktley. I did something similar a couple of years ago, and ended up with 3 LEDs running off an ATTIny (85, IIRC) using separate PWM channels, and not letting the modulation drop below 20% for each of the channels. Used the random() function in the GCC libs, which I found to be good enough.
I like it – its so simple!
thx
for those who also want to know what kind of software melt is:
http://www.mltframework.org/
My kids’ table lamps use pseudo-random generator for color cycling of an RGB LED with software-PWM on ATTiny85, with internal clock. Free tools, avr-gcc, built on Linux, programmed with AVR-Dragon two years ago. I did not find justification to call it a hack worth anybody’s time.
Did this ages ago using a PIC 12C508 but these days you could use a Tiny13.
Someone should totally make one of these using a red phlatlight, these are incredibly efficient and can illuminate the largest pumpkins.
For glare reduction, I’d avoid using a lens at all and mount the LED, heatsink and driver in the top
of the pumpkin projecting downwards.
There are also circuits “out there” for transistor based shift registers that can be modified to flicker an LED, a simple one can be built using a modified multivibrator circuit with each stage coupled to the next and a tap point used to further randomize the output.
I built this back in 1995 :)