Introduction: ATTiny85 Traffic Light

About: I am a geek, dad, coder, CTO of American Reading Company, electronics enthusiast, and amusement park aficionado.

This instructable will show you how to build a fun traffic light circuit using the cheap and powerful ATTiny85 microcontroller. The ATTiny85 can be programmed using the Arduino Processing language, and can do many of the things an Arduino can do. Learning how to use the ATTiny85 in this way will let you take your awesome Arduino projects and make them permanent. It's much more feasible to leave a $2 ATTiny85 attached to your circuit than it is a $30 Arduino.

The traffic light itself is a very simple gadget. Press the tiny pushbutton, and the light advances from red to green, then from green to yellow, and finally back to red. To make it even more sophisticated, the lights "fade" on and off using pulse width modulation (PWM).

After finishing this quick project, you'll have a foundation upon which you can build many cool circuits using the ATTiny85.

Step 1: Go Shopping!

Here are the parts I used to build the traffic light.

Feel free to make substitutions, and buy parts from any vendor you like.

1x - Red LED
1x - Yellow LED
1x - Green LED
(You can get all three LEDs in this assortment at RadioShack: http://www.radioshack.com/product/index.jsp?productId=2062588)

3x - 100 Ohm Resistors
1x - 10k Ohm Resistor
(You can get both resistor values in this assortment at RadioShack: http://www.radioshack.com/product/index.jsp?productId=2062304)

22AWG Solid Core Hookup Wire
(There is a three pack of hookup wire at RadioShack. Make sure to get solid core! http://www.radioshack.com/product/index.jsp?productId=2049743)

1x - ATTiny85 8-pin PDIP Microcontroller
(You can get these at Jameco: https://www.jameco.com/webapp/wcs/stores/servlet/Product_10001_10001_2151312_-1)

1x - 8 pin socket for the ATTiny
(You can get these at Jameco: https://www.jameco.com/webapp/wcs/stores/servlet/Product_10001_10001_112206_-1)

1x - Small DPDT Slide Switch
(You can get these at Radioshack: http://www.radioshack.com/product/index.jsp?productId=3020764)

1x - Momentary Tactile Button Switch
(You can get a 20 pack of these useful buttons at Adafruit: http://www.adafruit.com/products/367)

1x - 5V Power Regulator
(You can get these at Jameco: https://www.jameco.com/webapp/wcs/stores/servlet/Product_10001_10001_51262_-1)

1x - Printed Circuit Board (Perfboard)
(This one from RadioShack has a really nice layout for the traffic light: http://www.radioshack.com/product/index.jsp?productId=2102845)

1x - 9V Battery Snap Connector
(You can get these at RadioShack: http://www.radioshack.com/product/index.jsp?productId=2062219)

Here are some things we're assuming you already have.

Arduino
(I used an Arduino Uno - R3 https://www.adafruit.com/products/50)

10uF Capacitor
(RadioShack has a nice assortment of capacitors: http://www.radioshack.com/product/index.jsp?productId=2062376)

Arduino IDE
(I used Arduino 1.0.4 http://arduino.cc/en/Main/Software)

Soldering Iron
(I happen to be using this one from RadioShack: http://www.radioshack.com/product/index.jsp?productId=2062750)

Solder
(I'm using 60/40 Rosin Core 0.05" Solder from RadioShack: http://www.radioshack.com/product/index.jsp?productId=2062712)

Step 2: Setup Your Arduino to Program the ATTiny85

The ATTiny85 is a cheap, programmable microcontroller. It can do many of the things an Arduino can do. But, how do you get your Arduino sketches onto an ATTiny85? You can setup your Arduino to program an ATTiny85, that's how!

The smart people at High-Low Tech have written a terrific tutorial that explains exactly how to do this. You can read the tutorial here: http://hlt.media.mit.edu/?p=1695

In a nutshell, here's what you're going to do:

1.  Connect your Arduino and upload the ArduinoISP example sketch to it. This sketch comes pre-installed with the Arduino IDE. It's in the Examples folder.

2.  Setup a breadboard with the ATTiny85 and connect it to the Arduino. You'll find the full details on this setup in the High-Low Tech tutorial.

3.  Select ATTiny85 @ 1MHz (Internal Oscillator) from the Tools > Board menu.

4.  Upload the Traffic Light sketch (seen later in this tutorial) to your ATTiny85.

Here are some additional things you should know:

1.  I have version 1.0.4 of the Arduino IDE, and the instructions in the High-Low Tech tutorial worked perfectly for me.

2.  When programing an ATTiny85, you must select a board definition in the Tools > Board menu of the Arduino IDE. This tells the Arduino IDE which ATTiny "core" to use. The High-Low Tech tutorial tells you to download and use an ATTiny core from GitHub. There's nothing wrong with this core, and many people use it! However, it only lets you use hardware pulse width modulation (PWM) on two pins of the ATTiny. I wanted to use PWM on three pins. Thankfully, you can use any ATTiny core you like. I used the arduino-tiny core from Google Code. This core lets you use PWM on pins 0, 1, and 4.

So, you should download the arduino-tiny core from: https://code.google.com/p/arduino-tiny/

3.  If you bought the exact same ATTiny85 I bought from Jameco, you'll notice that it is a 20MHz microcontroller. So, you'll probably do what I did and look for a 20MHz board definition in the Tools > Board menu. If you're using the arduino-tiny core, you won't find one! But, that's okay! It wouldn't have worked properly anyway! See, these ATTiny85 chips run at 1MHz from the factory unless you upload a specific bootloader and use an external crystal. You don't need to do any of that for this tutorial.

So, you should select ATTiny85 @ 1MHz (Internal Oscillator) from the Tools > Board menu.

Step 3: Upload the Traffic Light Program

You'll find the traffic light sketch in the attached file, ATTiny_Traffic_Light.txt. Please look through the code and read the comments. They explain what each function does.

1.  Create a new sketch in the Arduino IDE called "ATTiny_Traffic_Light" and paste the attached code.

2.  Upload your sketch to the ATTiny85.

3.  Once the sketch is uploaded, you can unplug the Arduino from your computer's USB port.

4. Now you're ready to remove the ATTiny85 from the breadboard (where it was programmed) and embed it in your final project.

Step 4: Review the Circuit Schematic

Review the circuit schematic below.

1.  The 9V battery is fed into a 5V voltage regulator. The ATTiny85 needs exactly 5V to operate.

2.  From the regulator, we go to a slide switch. This allows us to easily turn our traffic light on and off while leaving the battery connected. The other pole of the switch will be used to feed the positive power supply of our circuit.

3.  Next, we provide positive voltage to pin 8 of the ATTiny85.

4.  We connect pin 4 of the ATTiny85 to ground.

5.  Pin 7 of the ATTiny85 is connected to the negative side of a momentary pushbutton. The other negative pin of the pushbutton is connected to a 10K Ohm resistor, which itself is connected to ground. The positive side of the momentary pushbutton is connected to our circuit's positive power supply. When a user presses this button, it will register as a HIGH signal on pin 7. In the Arduino IDE, this is referenced as pin 2.

6.  Pin 5 of the ATTiny85 is referenced in the Arduino IDE as pin 0. This pin is connected to the anode (long leg) of the red LED. The cathode (short leg) of the led is connected to a 100 Ohm resistor. The resistor is connected to ground.

7.  Pin 6 of the ATTiny85 is referenced in the Arduino IDE as pin 1. This pin is connected to the anode (long leg) of the yellow LED. The cathode (short leg) of the led is connected to a 100 Ohm resistor. The resistor is connected to ground.

8.  Pin 3 of the ATTiny85 is referenced in the Arduino IDE as pin 4. This pin is connected to the anode (long leg) of the green LED. The cathode (short leg) of the led is connected to a 100 Ohm resistor. The resistor is connected to ground.

Step 5: Build the Traffic Light Circuit on a Printed Circuit Board (Perfboard)

I found that the Radio Shack General Purpose Printed Circuit Board (pictured) was well-suited for the layout of the traffic light circuit. Please review the photo of the finished circuit to plan your layout. The skills and methods needed to layout and solder a circuit onto a circuit board are outside the scope of this tutorial. Moreover, I am not the best person to teach those things!

For help with soldering, check out Dave Jones' excellent EEVBlog on YouTube. There is a great video on soldering here:
Soldering Part 1: http://www.youtube.com/watch?v=J5Sb21qbpEQ
Soldering Part 2: http://www.youtube.com/watch?v=fYz5nIHH0iY

For help working with circuit boards, and specifically perfboard, watch Collin Cunningham's video here:
http://blog.makezine.com/2010/06/24/circuit-skills-perfboard-prototypin/

Step 6: Connect a Battery and Amaze Your Friends and Family

This traffic light is a fun circuit that you can show off to friends, family, co-workers, and various other captive audiences. To see it in action, check out the video here: