Making an ATtiny13 Powered Pumpkin!

Okay, this project is a bit silly. I was playing around with my Arduino/Gameduino Satellite Tracker, and Halloween was fast approaching. I had some ATtiny13 processors lying around, and some rather bright 1w Cree LEDs.. I decided I’d try to hack together a simple project: a pumpkin lit by the LED, and which could be toggled into two different modes. One mode would flicker the LED to simulate a guttering candle. The other would just brighten and dim.

So, I made one. Here’s a video of it in operation:

Why would you want do do something like this? Well, a couple of reasons.

As projects go, it’s pretty cheap. The ATtiny13 costs less than two dollars in unit quanities from digikey, and something like $1.20 if you order them in quanitity twenty five. I paid about $3 for this super bright LED, and I scavenged an IRF510 power transistor (figure less than two dollars, even at Radio Shack). If you didn’t want to use such a powerful LED, you could use a much cheaper transistor or even skip it entirely. It’s a project that makes pretty good use of whatever junk you have lying around.

It’s also pretty easy. I wired mine all together on a tiny breadboard. You could solder one together. You could make a PCB board if you like. Nothing is very critical.

And yet, it’s doing something kind of fun.

Okay, let’s pretend that you’ve got some ATtiny13’s lying around (more expensive/larger AVR chips will work, but are overkill for something this small). You’ll need an Atmel programmer, or you can use an Arduino, following the instructions that I linked to earlier. And of course, you’ll need the code! Here’s the program that I wrote. Note: this isn’t an Arduino program, it’s meant to be compiled using avr-gcc separately.

[sourcecode lang=”cpp”]
#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>

/*
* candle.c
*
* A simple program written for the ATtiny13 processor to
* control an LED light to be used inside a pumpkin. It has
* a switch (connected between pin 3 and the ground of the ATtiny13)
* which toggles it between two modes. In one mode, it rises smoothly
* in brightness and then dims, in the other, it randomly ‘flickers’
* to help simulate the flickering of a candle.
*
* Written by Mark VandeWettering for Halloween, 2011
*
* Compile using avr-gcc and the following commands:
* avr-gcc -g -Os -c -mmcu=attiny13 candle.c
* avr-gcc -mmcu=attiny13 candle.o -o candle.elf
* avr-objcopy -O ihex -R .eeprom candle.elf candle.hex
*/

#define LED PB0 /* pin 5 on the ATtiny13 */
#define SWITCH PB4 /* pin 3 on the ATtiny13 */

int i;

unsigned int lfsr = 1 ;

unsigned char cnt = 0 ;
signed char dir = 1 ;

int
main(void)
{
/* LED is an output… */
DDRB |= (1 << LED) ;
/* SWITCH is an input, activate the pull up resistor */
PORTB |= (1 << SWITCH) ;

TCCR0A |= ((1 << COM0A1) | (1 << COM0A0) | (1 << WGM01) | (1 << WGM00)) ;
TCCR0B |= (1 << CS01) ;
OCR0A = 0;

for (;;) {
if (!(PINB & (1 << SWITCH))) {
lfsr = (lfsr >> 1) ^ (-(lfsr&1u) & 0xB400u) ;
OCR0A = (lfsr >> 8) ;
_delay_ms(12);
} else {
cnt += dir ;
OCR0A = cnt ;
if (dir > 0 && cnt == 255)
dir = -1 ;
if (dir < 0 && cnt == 0)
dir = 1 ;
_delay_ms(5) ;
}
}
}
[/sourcecode]

The LED is connected to pin 5, which is a PWM output on the ATtiny13. You also need to attach a switch to pin3, which will toggle the program between two modes. In one mode, it’s just ramping up and ramping down the intensity of the LED. In the other, it uses a linear feedback shift register to generate a random number which will serve as the intensity of the LED. Note: it generates a sixteen bit value, so I mask off the top 8 bits to send to the PWM register. You can then use the compilation commands in the comments to create an Intel Hex file, which you can program to the ATtiny13 using avrdude.

Once you’ve got the program burned, you just need to power the circuit. I used just two AA batteries, providing 3v. In theory, the standard ATtiny13 can be powered down to 2.8 volts, so that works okay, at least as long as the batteries are fresh. If you wanted to use standard LEDs, you could just attach one to pin5 with a current limiting resistor to ground, and you’d see the LED flicker. I added a 1K resistor feeding the base of a 2N2222 transistor, with a 1W Cree LED and a 47 ohm 1 watt resistor on the collector side. In theory, I could reduce the resistor down to 15 ohms, but when I did that, the 2N2222 got pretty hot, so i decided to ease off on the current. It reduces the light a bit, but there is still plenty.

And that’s all there really is too it! If anything is unclear, feel free to mail me with questions, and I’ll try to improve the page. Happy Halloween!

2 thoughts on “Making an ATtiny13 Powered Pumpkin!

  1. paolo

    hi, how much current to power the 1W LED? How can I calculate it with for a 2n2222 led driver with a attiny85?

Leave a Reply

Your email address will not be published. Required fields are marked *