How to Build a Traffic Light Circuit with an ATtiny85 Microcontroller


ATtiny85



In this project, we will build a traffic light circuit with an ATtiny85 microcontroller.

A traffic light circuit is a circuit which simulates a traffic light.

3 LEDs are used for this circuit: a green LED, a yellow LED, and a red LED.

As you would think, the green LED is the equivalent of the green light. The yellow LED is the equivalent of the yellow light. And the red LED is the equivalent of the red light.

In this circuit, we will program the ATtiny85 chip so that the green LED is on for 15 seconds, the yellow LED on for 2 seconds, and the red LED on for 15 seconds.

Of course, you can adjust these times to suit any preference that you may have, but these are the times we'll adopt for this circuit.

By doing this circuit, you will have a great understanding of how to manipulate individual digital outputs on an ATtiny85 and be able to keep an output on or off for however long desired.

We show how to build this circuit below.

Components Needed

  • ATtiny85 microcontroller
  • Green LED
  • Yellow LED
  • Red LED
  • 3 470Ω Resistors


The ATtiny85 microcontroller can be obtained through many electronic online retailers. One easy place to purchase it is ebay. Depending on where you get and the quantity you get, it can range anywhere from a little over $1-$4.

The datasheet for the ATtiny85 microcontroller is shown at the following link: ATtiny85 Datasheet.

The ATtiny85 chip is an 8-pin microcontroller.

The pinout of the ATtiny85 is shown below.


ATtiny85 pinout


The power pins are VCC and GND, pins 8 and 4. Anywhere from 1.8V to 5.5V can power the ATtiny85 chip. To our circuit, we will simply apply 5V to VCC and connect GND to power ground.

Besides the 2 power pins discussed right above, the other 6 pins of the ATtiny8 are I/O pins labeled as PORTB pins. These are labeled on the datasheet and in the pinout above as PB0 to PB5.

These pins can be used as inputs or outputs, and they have various uses depending on what you need the pin for.

In our circuit, we are simply using the I/O pins or PORTB pins as outputs.

The PORTB pins of the ATtiny85 source power, so when connecting a digital output, the port provides positive power to the output device, so that we just need to connect the other end of the device to ground.

ATtiny85 Traffic Light Circuit

The circuit diagram of the blinking LED circuit we will building with an ATtiny85 microcontroller is shown below.


ATtiny85 traffic light circuit


The breadboard schematic of the circuit above is shown below.

ATtiny85 traffic light breadboard circuit


First, we give power to the ATtiny85 chip. This is done by connecting 5V to VCC, pin 8, and by connecting GND, pin 4, to ground.

Next, we attach to the outputs of the ATtiny85 microcontroller LEDs. The green LED will be attached to pin PB0, which is pin 5. The yellow LED will be connected to pin PB1, which is pin 6. The red LED will be connected to pin PB2, which is pin 7. All of these LEDs are connected in series with a 470Ω resistor to function as a current-limiting resistor so that too much current doesn't flow through the LED and burn it out.

Being that the outputs source current, we connect the anode terminal of the LED directly to the output and the cathode end gets connected to ground. This supplies sufficient power to the LEDs. Being that LEDs only require about 10mA-20mA of current, they don't use that much power. However, for devices that need significantly more current such as a motor, for instance, we would have to connect a transistor to the output and then attach the output device to the collector of the transistor. This gives current amplification so that it can drive high-current devices. But, again, for LEDs, this is not necessary.

This establishes all the connections that are needed for this circuit.

Now all we need is the code.



Code

The code needed in order to simulate a traffic light with green, yellow, and red LEDs is shown below.

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
while(1)
{
DDRB= 0xff; //sets all the pins as outputs
PORTB= 0b00000001; //sets the LED at PB0, the green LED, HIGH or ON
_delay_ms(15000);//keeps the green LED on for 15 seconds
PORTB=0b00000000;//turns the green LED off
PORTB= 0b00000010; //sets the LED at PB1, the yellow LED, HIGH or ON
_delay_ms(2000);//keeps the yellow LED on for 2 seconds
PORTB=0b00000000;//turns the yellow LED off
PORTB= 0b00000011; //sets the LED at PB2, the red LED, HIGH or ON
_delay_ms(15000);//keeps the red LED on for 15 seconds
PORTB=0b00000000;//turns off the red LED
}
}



First, we must important the <avr/io.h> library.

Then we create the main loop() which will house the while(1) loop. This loop is an infinite loop. So the program will keep executing the code contained inside of it infinitely, over and over again. In this loop, we declare the DDRB pin as 0xff, which makes all of the I/O pins of the ATtiny85 outputs.

In the next line, PORTB= 0b00000001;, we set the PORTB0 pin HIGH. Since the green LED is attached to this pin, the green LED turns on. By writing a delay with a parameter of 15000, we keep the green LED on for 15 seconds.

The _delay_ms() function creates a delay. The number that you place inside the parameter creates that numerical value in delay in milliseconds. A parameter of 500 creates a delay of 500 milliseconds, which is a half a second. You can modify this value to any number. If you want the LED to be on for a full second, the number in the parameter would be 1000. If you want the LED to be on for 3 seconds, the number is 3000. Since there are 1000 milliseconds in one second, if you know the amount of seconds you want the delay in, you simply multiply this number by 1000 to get the numerical value you want place in the _delay_ms() function. Since we are creating a delay of a half a second, or 0.5 seconds, 0.5 * 1000= 500, which is what we use in this program.

In the next line, PORTB= 0b00000000;, turning off the green LED.

We then turn on PORTB 1, which is the yellow LED. We call a delay that keeps it on for 2 seconds. We then turn it off.

We then turn on PORTB 2, which is the red LED. We call a delay that keeps it on for 15 seconds. We then turn it off.

And this is a program which replicates a traffic light.



Related Resources

How to Build an LED Circuit wtih an ATtiny85 Microcontroller

How to Build an LED Blinker Circuit with an ATtiny85 Microcontroller

HTML Comment Box is loading comments...