Introduction: 3 Easy ATTiny Holiday Gifts

Picture of 3 Easy ATTiny Holiday Gifts

 Every year the holiday season rolls around and I get stuck on what to give for my friends and family.  People always say that it's better to make the gift yourself than buy it at a store so this year I did just that.  The first displays a name or basic image when waved through the air, the second fades smoothly between two white led's (It also has a safety pin to attach it to clothes or a purse), and the third is a mood light of sorts, scrolling seamlessly between colors. All are under $15 and are easy to assemble in less than an hour.  You could also give these instructions as well as parts as a gift to someone interested in learning electronics.

Note:  A basic level of soldering is assumed.  However, unlike my previous instructable I will gloss over how to program an AVR.  I really hope that this helps people who are trying to get started in electronics.  I remember when I was there and will be happy to take any questions you may have.  Remember there are no stupid questions!  I have left it open to you to determine how you will lay out your circuit board in hopes that you will come up with a new way of displaying the simple circuits I have drawn out.  Please post pictures once you are done, I can't wait to see what you come up with!

Parts for All of the Projects
N = number of projects

xN ATTiny45 (
xN 8-pin DIP socket (RadioShack)
x 1  Large Perf Board (I got mine at RadioShack)
xN 3v Coin Cell Battery and holder (RadioShack)
x1 Programmer (I use this and the supplied makefile will be configured to use this one)
x2N Resistors, one 10 ohm and one 10k ohm (RadioShack)

Here's a picture of the three completed projects:

Step 1: The Name Flasher

Picture of The Name Flasher
Other Parts

x5 DIFFUSED 3mm LED's (RadioShack)

This project uses persistence of vision to display an image or text when moved rapidly.  It works better when it's dark.  With the AVR we are switching between LED's faster then the eye can see we can create a picture in the air with very few components.

The Code:  (Download the .zip file at the bottom of the page for the code, makefile, etc. (That code does not have the extensive comments that this code does but if this code doesn't work, try the one in the zip file))

#define F_CPU 1000000
void dispClear()
  PORTB = ~0b00000000;
int main()
  DDRB = 0xFF; //For those of you who have never read C before the double slash indicates a comment.  This sets the pins of the avr as an output
  char x = 10;  //x is used to set the delay legnth.  Increasing the value give a slower switch between pixels, decreasing, a lower
  while(1)  //While(condition is true); {Do This}  (True in C is 1)
      PORTB = ~0b00010001;  //My friend's name is Zoe so I drew out the characters on graph paper and then imputed them into PORTB.  The first three zeros don't matter because there are only five led's connected.  A one in one of the remaining five spaces indicates that the LED is on, a zero, off.
      _delay_ms(x);  //delay in miliseconds
      PORTB = ~0b00010011;
      PORTB = ~0b00010101;
      PORTB = ~0b00011001;
      PORTB = ~0b00010001;
      dispClear();  //Open space between characters
      PORTB = ~0b00001110;
      PORTB = ~0b00010001;
      PORTB = ~0b00001110;
      PORTB = ~0b00011111;
      PORTB = ~0b00010101;
      PORTB = ~0b00010001;
      _delay_ms(x*5);  //The word is done so for clairity I have a bigger space between words

Now follow the instructions on page 4 to compile this code and send it to your AVR.

Step 2: The Purse Light

Picture of The Purse Light

Other Parts

x2 LED's NON DIFFUSED (RadioShack)

This project fades smoothly between two LED's.  My mom wanted something to go on her purse to help her bee seen at night.  I felt like a flashing bike light would draw too much attention so I made this.

It uses PWM (Pulse Width Modulation) to give the effect of dimming and brightening.  What is actually happening is the led is flashing faster than the eye can see at varying intervals to simulate a change.  You can see this more clearly when you wave it in of your face as you would with the Name Flasher.  The code can be downloaded at the end of this page.  See page 4 on how to compile to your AVR.

Instead of commenting the code here I'll briefly explain the concept.  PWM is generated by a timer.  Each clock cycle the timer counts up one.  When it reaches a certain value (In this case OCR1B) it changes the state of a pin (In this case OC1B).  To get the LED's to "switch off" I wired the other one to [OPPOSITE]OC1B (That's what the bar across the top means).  Then we use x to increment and decrement the amount of time for the LED's to be on.

Step 3: The Color Scroller

Picture of The Color Scroller

Other Parts

x1 RGB LED (diffused is better) (RadioShack)

This to me is a classic.  I have always been fasinated by lights like this and find them very calming so naturally I wanted to make one.  Also their visibility in society gives them a real WOW factor.  "You MADE that?!" 

The code is nearly the same and, in my opinion, easier to read.  You can download the at the bottom.  This time we provide a PWM'd pin for each grounding pin on the RGB LED.  In this way we ground through the chip.  Volts can be measured as the difference between two points.  
This last instance is what we are doing when we ground through the chip.  When there are three volts on both sides, per say, there are not enough volts to drive a LED.  So to fade between colors we chose a color and another color.  Set one equal to x and the other equal to 255-x or the INVERSE of x.  Like we were doing in the previous project.  Now go to step 4 to program.

Step 4: Programing

Picture of Programing

 First download and install Emacs, avr-gcc, and Avrdude and any dependancies they might have.  On Arch linux I did this with: sudo pacman -Sy emacs avrdude

Now it's time to hook up your programmer to your avr.  Wire the pins on the 6-pin ISP programmer (picture below) to the corosponding pins on your AVR (Datasheet, page 2)(I did this on a breadboard).  Now plug in the programmer to your computer and VERY QUICKLY touch the top of the AVR.  If it is hot, REMOVE THE PROGRAMMER FROM YOUR COMPUTER AT ONCE check your connections and try again (Power and Gnd backwards?).  If it is not hot then open emacs and press Ctl-x Ctl-f to find the .c file.  Open it and then press Meta(Usualy  Alt)-x and type compile.  Press enter twice and if you are lucky your AVR should now be running your code!

Thanks for reading!  I hope you enjoyed it and learned something.  Please, don't hesitate to ask questions and don't forget to post pics of what you come up with.  Happy holidays and don't forget to vote!


yachtsman (author)2013-12-19

Nice DIY project. :) Can be ATTINY45 programmed via the Arduino?

JamesZ16 (author)yachtsman2016-04-18


(realize post is 2 years old, posting for others who scroll by... like you)

rpetruț (author)2015-11-29

Hy , if you help me , can you give the code to write STF with PORTB , I would be grateful ! Thanks!

Build_it_Bob (author)2014-04-21

Very nice projects ...thanks for sharing .


Ploopy (author)2013-09-10


harrypotterrocks (author)2013-09-03


harrypotterrocks (author)2013-09-02

where do you write the code sorry im new to AVRs

ogama8 (author)harrypotterrocks2013-09-03

I generally write in good old emacs when I'm on a *nix system, though I am partial to Sublime Text 2. Neither are IDEs.

enikka (author)2013-05-27

First of all nice tutorial!
But I have one question, it is written in the Attiny45 datasheet that it has only 2 PWM pins, am I misunderstanding something?
Have a nice day!

ogama8 (author)enikka2013-05-27

*Note: OC stands for output compare. In the datasheet it says that the 2nd T/C has separate Output Compare Registers. This is what's really important to us. One output compare reg. gets us one easy channel of PWM.

ogama8 (author)enikka2013-05-27

Yep, that'a a little tricky. In the data sheet it says there's a Timer/Counter that has 2 PWM outputs. But, if you look right underneath that, there's the High-Speed T/C. This T/C can also do PWM on some other pins.

In general, I look at the pinout and see how many pins have a separate OC** designator, as those are the pins that can be used for PWM.

karonth (author)2010-12-16

Great instructable!
This evening i got the rgb scroller working with the abby version, and i got an idea for a mod.
My idea is to read a 10 k potentiometer and to select a color based on the value.
So if the potentiometer is a 10 bit value i could break the value in blocks of three that'll be the codes for the color components, and if the last bit is set it scrolls through colors.
I'm loosing some resolution there, but it's ok.
I'm just starting to work with microcontrollers and i have had experience only with arduino so far. What do you think, could this work?
Can you give me some advice on how to proceed?

ogama8 (author)karonth2010-12-22

I think this could work. If you didn't mind (relatively) low resolution you could use the number from the adc to specify where in the code you are and have the code be near the same as I have already written. Since you're good with arduino I'd try out your ideas there and then translate them into code for the attiny45.

robot1398 (author)ogama82011-06-17

can you give the code for writting robot

ogama8 (author)robot13982011-06-19

I'm not sure what you mean by that statement. Could you explain?

robot1398 (author)ogama82011-06-19

as you rote zoe with pov can you give the code to write robot with pov

ogama8 (author)robot13982011-06-23

It would look something like this:




"o" again


robot797 (author)ogama82012-01-12

lol thank you

karonth (author)ogama82011-01-13

I'm having some trouble modifying the code. If i try something like y=bright(); the hex file gets almost 10 times bigger and i get the following

>>avrdude: ERROR: address 0x1010 out of range at line 257 of new.hex

Can I use the adc value directly or am I missing something? I'd like to change the wait time with a pot so i could "freeze" a color or fast forward if i don't like it

Sorry if this is a noob question, but i haven't got much experience with microcontrollers.

Thank you!

ogama8 (author)karonth2011-01-13

I'm not sure why "y = bright();" would kill the hex file. You will need to change the bright() function so that if the pot at a certain point stops the cycle. Something like this:

char bright()
(just before the return statement)
while(adc <= 1); //This'll do nothing while the adc is less than 1

You'll need to change the "_delay_ms(y);" to "_delay_ms(bright());"

theAVRdude (author)2010-12-30

Hi ogama8, I like your intruc :) but could you please specify if in the _delay_ms(x)?
Should I put a value for the x in there? How is this sentence written. I tried both with a value of 100 and x alone.

Maybe could it be the char x = 10; that should be change?

I did recompile this twice but could not get anything except Kligon stuff!!!

Thanks a lot for your time


ogama8 (author)theAVRdude2010-12-30

Yes, char x = 10; should change. If you want the lights to go faster you'd lower the value of x, like so:

char x = 2;

char specifies the type of variable x is, and = 2 sets the value of x when it's called elsewhere. For example:

char x = 10;

because x == 10 that means that _delay_ms(x) is the same as _delay_ms(10).

Since I have multiple instances where I'm using a _delay_ms() it makes the value easier to change.

theAVRdude (author)ogama82010-12-31

Yes, such a great answer!! That make so much sence. Thanks a lot...

punkzter (author)2010-12-25

Is there a way to speed up the flashing on this project? So that it is more like a bike light?

ogama8 (author)punkzter2010-12-28

Yes. Lower the value of x.

Absolute_Zero (author)2010-10-29

Please update this post with the applicable code. The zip files which were previously hosted on a free hosting site have been deleted due to inactivity.

ogama8 (author)Absolute_Zero2010-10-29

All done.

Absolute_Zero (author)ogama82010-10-29

Thanks :)

punkzter (author)2010-09-12

I built the purse light, but it does not work, I am hoping that someone can help me out. I used avrdude to flash the chip with the make.hex file. I built everything, but it doesn't turn on. I am new to these projects, so I do not know appropriate troubleshooting steps.

I did run a wire directly from the battery (+) to the 2nd pin of the chip, and one of the leds turned on. Running it to the 3rd pin turns the other on. Does this mean that the chip wasn't programmed correctly?


ogama8 (author)punkzter2010-09-12

Are you sure that you have the resistor from pin 1 to 8? What about the ground on the chip (pin 4)? Did you solder directly to the chip or use a socket? Could you upload a picture of both sides of the board? That'd really help.

punkzter (author)ogama82010-09-12

Yes. I rechecked those connections. Interestingly, I rebuilt the project on a breadboard with the original LEDs. It didn't work, even though I knew that the LEDs worked. Then, I tried some different LEDs and they did work. I put those back into the original circuit board (what I took a picture of) and it worked.

Do you think that you could add some information on how to compile a *.c file?

ogama8 (author)punkzter2010-09-12

I'm glad it's working now.

I have no idea on how to compile a .c file on AVRDude. If you do decide to use emacs you simply press Alt-x, type compile, press enter twice and you will (hopefully (if your code is correct)) end up with a hex.

punkzter (author)2010-09-10

Also, could someone help me with the programming part of this project? I have avrdude and avrstudio installed. I do not understand how I can take the modified *.c file and convert it back into a .hex file.

punkzter (author)2010-09-10

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


klarkg (author)2010-09-09

Thanks man , these are really cool , Ive got these all going now im lovin the AVRs. Your instructables are really good. The only other question I had was how to beableto read the name flasherI got it going fine dude but Icounlnt figure out how to read it . did you put it on a wheelor somthing?

ogama8 (author)klarkg2010-09-09

I took a long exposure pic in the dark. A wheel will also work. You have to move your hand from left to right. Try starting out slow and then speed up until you see it. You can't focus on it too much.

klarkg (author)2010-09-09

Wondering about the Abby varrient file you have there schematic?

ogama8 (author)klarkg2010-09-09

Ah. I forgot about that one. It's new since this was written. The Abby Variant adds a photo-resistor on pin 2. You hook one end to Vcc and the other to the second pin. You also have to hook up a 10k resistor between that pin and Gnd, too. This gets read by the AVR and turns the light off if there is enough light. I'm working on another version that automatically turns off after a pseudo-random time so it wont go all night.

klarkg (author)2010-08-17

Sorry I meant attiny45 or attiny12 ..

ogama8 (author)klarkg2010-08-18

This instructable was written for the 45. The 12 will not work any PWM application but will work for the Name Flasher. I'd change the value for "MEGA" to 12. Let me know if that doesn't work.

klarkg (author)2010-08-17

Anyone know how to change the code to use a aattiny13 instead? that would be radd!

ogama8 (author)klarkg2010-08-17

You will have to go into the make file with your text editor and change the entry for "MEGA" to 13 (I think). Sadly the attiny13 only has 2 PWM pins so you won't be able to do the color scroller *easily.* You could hook it up to some transistors etc. but then it's a real pain and it gets bigger. The purse light also won't work because it is using different pins for the PWM. I can work around these problems for you if you like but I'm on vacation and don't have access to AVR's at this time. I hope this helps!

nm17 (author)2010-08-03

Thanks for the instructable! I was looking for code similar to this.

mertaxoy (author)2010-07-19

your zip file is not available !

ogama8 (author)mertaxoy2010-07-19

Fixed! Thanks for catching that!

About This Instructable




More by ogama8:3 Easy ATTiny Holiday GiftsBike Hand Signal Light
Add instructable to: