Flameless Candle From an Attiny13

36,838

87

40

Published

Introduction: 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

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

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

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.

Share

    Recommendations

    • Casting Contest

      Casting Contest
    • Clocks Contest

      Clocks Contest
    • Make it Move Contest

      Make it Move Contest
    user

    We have a be nice policy.
    Please be positive and constructive.

    Tips

    Questions

    40 Comments

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

    2 replies

    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.

    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).

    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.

    1 reply

    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.

    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?

    2 replies

    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.

    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.

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

    4 replies

    You will have to learn bitmath for it to make sense.  There is a great tutorial here http://www.arduino.cc/playground/Code/BitMath

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

    PORTB is the variable representing the pins on the attiny.

    PORTB &= ~(1 << thePin);
    and
    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

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

    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.

    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. 

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

    Thanks!

    1 reply

    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

    Hi,
    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... http://www.transpapers.com/pwmcandle.zip 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

    2 replies

    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.

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

    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.