Introduction: Flameless Candle From an Attiny13

Picture of Flameless Candle From an Attiny13

I need to light my jack-o-lanterns, but this year I wanted something better then a regular candle. I want to flicker, but I want to get rid of the flame.

Any fire is dangerous, especially around kids, burny melty pumpkins stink, and regular candles need to be replaced quite often. So I searched around for a project to copy, oddly enough I could not find one I liked, so I created one myself. My original proof of concept was on an arduino, but that is a bit pricey for a simple candle. Once I proved it would work, I found a way to do it cheap.

Here is how I did it, out of the stuff I had on hand.

My first circuit and my first instructable.

Step 1: The Parts

Picture of The Parts

I used what I had on hand. This came out to.

1) ATtiny13 x1
2) Red led x1
3) Yellow led x1
4) 100 ohm resistors x2
5) 8pin socket x1
6) thru hole switch x1
7) battery holder for 2AA batteries x1
8) perf board

resistors will vary based on your leds, you can probably find a better switch than I, you can even skip the perf board and wire it up dead bug if you want.

Step 2: The Circuit

Picture of The Circuit

My basic circuit, just wire it up like this.

Step 3: The Code

Here is the code I used. I just flash the leds, and try to add some randomness to it. The code could be better using pwm, and power saving features, but I don't know how to do any of that. My first non-arduino circuit, and my first instructable. The effect from the two leds is satisfactory in spite of how I did it. Varying brightness, color, and flickering.

Compile and upload the code to the tiny13, and you are good to go.

Feel free to post better code...

#include <avr/io.h>

int main(void) {
int thePin = 0x0;
long randVal;
srandom(123); //random seed
DDRB = 0x3; // B0-1 set to output

for(;;) {
randVal = random(); // choose a pin
if((randVal % 2) == 0) {
thePin = 0x0;
else {
thePin = 0x1;

randVal = random(); //high or low
if((randVal % 2) == 0) {
PORTB &= ~(1 << thePin); // x &= ~(1 << n); forces the nth bit of x to be 0. all other bits left alone.
else {
PORTB |= (1 << thePin); // x |= (1 << n); forces the nth bit of x to be 1. all other bits left alone.


Step 4: That Is It

Picture of That Is It

You can sand the leds to diffuse the light, or use frosted leds, you can use a 2 color red/yellow led as well.

Put it in a ziplock bag to keep it from getting gross, and drop it in the pumpkin... instant flameless candle, and it will last for hours, and you don't have to worry about the kids messing with it.


Idrispo (author)2014-04-27

Hey, can you show me your reference on Attiny13 wit Arduino. I'm a newbie here...

aballen (author)Idrispo2014-04-28

I'm not sure you can get ardunio on an attiny13. Also, the goal of this project was to make it small, and to learn a little about AVR GCC.

If you want to do something similar on an arduino, get your led to go on/off... and then make use of the random function. That would be very similar to what I did here. There are lots of improvement you can make from there as well.

Idrispo (author)aballen2014-05-05

so, how to upload the code? I'm willing to use any other method beside arduino to upload the code. Can you give me the link... i've stumbled upon countless web tutorial but i dont know where to begin with (for attiny13).

Ploopy (author)2014-03-10


metalhead8711 (author)2014-02-15

Great project you have here. Currently building it. I do have a question. Suppose that I use a simple push button instead of the switch you used. Is there a line if code or a command that will shut of the power in say 20 seconds? I have an arduino book and I can't seem to find a good reference. Any help will be appreciated. Thank and keep up the good work.

aballen (author)metalhead87112014-02-16

Sorry, my code runs in a continuous loop. The switch powers it on and off. The AVR does have a sleep mode, and you could rig it up with a watchdog to power it back up on the press of a button, but this code does not do that.

scottinnh (author)2011-12-01

Pardon my ignorance.. but would the article's supplied .hex file work (as is) on an ATtiny85?

I'm using avrdude and the Adafruit USBtinyISP, and it seems to flash. No errors on the avrdude's output (everything done, ok, thank you). Command was `sudo avrdude -c usbtiny -p t85 flash:w:AVRCandle.hex`

Yet when I plug in a 3v power source.. nothing, I'm not using a switch, just direct Vcc to pin 8.

And If I pull the ATtiny85 and run VCC directly to the anodes (positive end) of the LEDs, they light up, which confirms the remaining circuit is valid.

I assume whatever has gone wrong for me is to do with my chip or the flash.
This is a nice simple instructable - it's Hello World. It's also like the third ATtiny tutorial I've tried following to no success (the others are more complex, for sure).

Does anything stand out? Is my avrdude command correct? I am also assuming the ATtiny85 isn't radically different from the tiny13. Thoughts?

aballen (author)scottinnh2011-12-01

I have not run avrdude from linux, but if you are seeing.... verifying... verified... done thank you. I believe you programmed the chip properly.

I don't think there are any differences between the 85 and the 13, other than memory... but I could be wrong. You really have to look at my code and compare to the 85 datasheet, my code may be flipping the wrong pins for your 85. One thing to try is to compile the code posted by MixMasterM below... he was running it on an 85.

Other things to consider.

1) you might have a bad chip, can you try on another 85(better yet a 13)? the problem may be the chip.
2) does your chip need 5v? check the exact version... if you have a 5v version, and are only providing 3v, that might explain why none of your attiny projects work.... and if you go up to 5v... increase your resistors.
3) check the led datasheet, maybe it is working and the voltage from the pin is too low... if you tested the led to vcc through the 100 ohm resistors, this should not be a problem though
3) have the fuses been altered? this project needs the internal oscillator set. I did not alter the fuses from factory settings for this project.
4) check the batteries(use a multimeter), they may be low... 3v to an led usually burns it out, so if vcc is not, maybe your batteries are suspect.
6) if you programmed the chip, it is probably oriented correctly in your circuit... but double check. Pin1 has a dot next to it. The datasheet will show you where each pin in my circuit diagram is physically on the chip.

Don't get discouraged. Sometimes you just need to take a break, and come back with a fresh set of eyes. I know the project seems pretty basic, but coding c for the chip was quite challenging for me at the time.

I can say though that this does work, several people have tested it successfully. Please let us know if you have some success.

scottinnh (author)aballen2011-12-01

Thanks for the speedy and specific suggestions. I trust it works, and I am just having bad luck. :-)

Of those suggestions, the two I should look into are the fuses and the pins (datasheet tiny 85 vs tiny13). I'll look into those two, and try the other code. When it is solved I'll post what the problem was (to help future time travelers who have the same problem)

I know the pin1 is correctly located, and my power supply is set and measured to 3v. I have tried 4 different chips.

FYI there are versions of the ATtiny 25/45/85, but they all handle the 2.7V to 5.5v range. The PU model (which I have) is 1.8v-5.5v.

purpulhaze (author)2010-04-15

Can you explain the code? I'm trying to figure out how to add another led if possible.

aballen (author)purpulhaze2010-04-19

You will have to learn bitmath for it to make sense.  There is a great tutorial here

Check out AVRFreaks for other tutorials on avrs and avr programming.

PORTB is the variable representing the pins on the attiny.

PORTB &= ~(1 << thePin);
PORTB |= (1 << thePin);

is me flipping the bit(a single pin) on and off

to add another led you can
1) simply connect the second led to the same pin
2) add a transistor to the pin, and connect both led to the transistor
3) add the new led to a new pin so it will work independently, you will need to update the code too

icebat21 (author)aballen2011-10-07

How would you modify the code to work on the other pins?
i absolutely love this project BTW.

aballen (author)icebat212011-10-08

well I just checked the code. The two pins I'm using are 0x0 and 0x1. You just need to change the values there to address other pins... not sure what the values are off the top of my head... you will have to check the datasheet.

I chose these two pins because my ICSP programmer uses the other 6, though I believe you can use 6 of the pins for IO... two of them will always be needed for VDD and GND

This means you can get 6 leds working here with one pin to a led... you can get even more working with a multiplex, or charlieplex setup.. but again I have not looked at the datasheet in some time.

purpulhaze (author)purpulhaze2010-04-20

from what I can tell seems it would be easier to find another micro controller to add more leds since there are really only 2 general purpose i/o pins and need the other ones ffor programming. 

punkzter (author)2010-09-10

Did you have to burn the fuses on the chip? What settings did you use?


aballen (author)punkzter2010-09-10

I did not touch the fuses, I think by default it is set to use the internal oscillator, and that was good enough for this project

siklosi (author)2009-03-07

I saw this project and decided to make it. Arther playing little in bascom I made pwm version of avr candle. Here is link to hex and bas file... circuit is not modified but during programming lfuse should be changed from default 6A to 7A (Divide clock by 8 internally; CKDIV8=0 disabled) so there is no flicker from pwm... I'm not code guru but I personally like more this pwm version... it's not hard for eyes and looks more like real fire. Basic code is also included in archive

aballen (author)siklosi2009-04-13

just wanted to say thanks. I use a usbtinyisp on the mac so to program by tiny13 I used the following command avrdude -c usbtiny -p t13 flash:w:pwmcandle.hex -U lfuse:w:0x7a:m -U hfuse:w:0x1f:m Looking forward to seeing it this evening.

aballen (author)siklosi2009-03-08

very cool, I can't wait to try it!!! thank you

drgabler (author)2009-02-18

Built this last nigh. In a dark room this gives you a pretty bad headache and drives you batty. We need to make some changes to the code to be nicer.

aballen (author)drgabler2009-02-18

The flickering is being faked with a random on/off for two leds. It could definitely be better code, but if you obscure the device inside a pumpkin like I did, or even just under a napkin, I don't think the effect is that bad. Please put up some better code though. I would love to see some pwm on this bad boy, but I'm a newb, and I have not had much time to learn how to use the timers properly :( my hobby time seems to be less and less these days.

drgabler (author)aballen2009-02-18

Yep that is why I said WE not YOU :). Yeah I had it obscured by frosted glass. I thought I was living the effects of a serious bender the headache I got. I tried adding in a delay, it got better but...... Love the Simple project.

sotsirh194 (author)2008-12-08

what software and programmer did you use?

aballen (author)sotsirh1942008-12-09

I used a USBTinyISP

the software I used to code was eclipse, with the avr-eclipse plugin. Then avrdude to program the chip. acr-eclipse has some nice hooks into avrdude which make it nice to use, just click and flash the chip.

blackrazor (author)2008-11-27

I just built this as my first attiny13 project, and it worked perfectly! Thanks for the Instructable- it is terrific!

MixMasterM (author)2008-10-20

Nice project - I like the effect produced by the dual LEDs. Looking at them directly, it's not that great, but as a test, I placed a paper towel over the LEDs to act as a diffuser, and the effect was pretty cool with the lights off.

I felt that the blinking was a bit fast, so I added a delay based upon code by SolidSilver in his flickering LED candle project (thanks SS!).

BTW, I compiled and ran this on an ATtiny45, so we know it works on the 13 and 45 at least.

// =============================================================================
// My version of the flameless candle code.
// Function delay_ms and the _delay_loop_2 borrowed from an Instructable
// by SolidSilver (who also helped me with his particular project) called
// "Flickering LED candle".
// Change the FLICK_DELAY as needed. I felt that a bit of delay gave the
// resulting flicker a bit more realism.
// - 10/20/2008 - cheeze69 on Instructables
// =============================================================================

#include <avr/io.h>
#include <util/delay.h>
static inline void _delay_loop_2(uint16_t count) attribute((always_inline));
#define delay_us(us) _delay_loop_2((unsigned int)(((float)F_CPU*us)/4000000L))
#define FLICK_DELAY 2000

void delay_ms(uint16_t x)

int main(void) {
int thePin = 0x0;
long randVal;
srandom(123); //random seed
DDRB = 0x3; // B0-1 set to output

for(;;) {
randVal = random(); // choose a pin
if((randVal % 2) == 0) {
thePin = 0x0;
else {
thePin = 0x1;

randVal = random(); //high or low
if((randVal % 2) == 0) {
PORTB &= ~(1 << thePin); // x &= ~(1 << n); forces the nth bit of x to be 0. all other bits left alone.
else {
PORTB |= (1 << thePin); // x |= (1 << n); forces the nth bit of x to be 1. all other bits left alone.


aballen (author)MixMasterM2008-10-20

The delay definitely helps thanks for the suggestion. I was really surprised how good the effect was on mine with such crude code. SolidStates project is really cool as well, the package is way nicer, the code in that project is way nicer too PWM and real randomness!

aballen (author)aballen2008-10-22

tried your code, I'm thinking it might be too big for the 13, that or I'm doing something wrong. I borrowed the delay idea though and got something similar to fit on the 13, check out the zip on the last step and let me know how you think it compares.

MixMasterM (author)aballen2008-10-23

Yeah, it is probably too big. The "rand" function uses a lot of memory and the tiny13 isn't packed with memory (that's why I bought a bunch of tiny45 instead of tiny13).

iBOLIT (author)2008-10-22

post hex file please

aballen (author)iBOLIT2008-10-22

just uploaded new code and the hex in a zip on the last step, enjoy.

idrawupay (author)2008-10-20

can this be accomplished by using a 555 timer?

uterrorista (author)idrawupay2008-10-21

I guess not. You only can set fixed duty cycle. Not random!

aballen (author)idrawupay2008-10-20

I'm not sure I could get the same "randomness" to create a flickering effect from a 555, but then I've never used a 555.

rockyt (author)2008-10-20

what are you using to get the code to the ic?

tlalexander (author)rockyt2008-10-21

There is always the lovely Atmel STK-500, the dev board designed to program these things. They aren't as cheap as other options but they make things so easy it really is nice. Plus, most people don't know this, but students get 50% off on development kits from Atmel. You'd have to call them for details but it could save you $50 or so, i forget how much my STK-500 was. -Taylor

aballen (author)rockyt2008-10-20

I build it out to a hex using eclipse and avr-eclipse, then I flash it with a USBTinyISP and a iscp to chip adapter I made myself.

The programer

The board is something liek this

Kiteman (author)2008-10-20

Could you add a short video of it, to let us see how realistic the effect is?

aballen (author)Kiteman2008-10-20

Yes I'll try to get a short video up of it tonight.

TheWelfareWarrior (author)2008-10-20

I've heard of people using music as the flicker... not only is it semi random (i guess music has a beat...) but you could tune it to get better results...

About This Instructable




More by aballen:minimal larson scanner/cyclonFlameless candle from an attiny13
Add instructable to: