Friday, September 5, 2014

Control Stop Light with an ATtiny85 or Arduino

One of my first projects with Arduino was to provide a little automation to an old stop light (and crosswalk signal) I had out in the garage. I'd already wired them to individual switches (good for games of "red light, green light"), but the allure of having it operate light a real light (green, then yellow briefly, then red, repeat) was too great. Arduino seemed like the perfect fit.

Of course, the Arduino can't switch anything high voltage nor high amperage. So you'll need to use a relay. I am a big fan of the cheap relay boards like these. For a regular stop light, you'll need three individuals or a four in one. I used a four in one relay board and a two in one separate board for the crosswalk lights. I stuffed it all in a plastic enclosure box. Don't undertake the high voltage part of this project without a firm understanding of the risks involved. This post isn't about high voltage relays or how to integrate them with the Arduino, so I'll assume you know how to do that. Follow along if you want to build a stop light simulator with 5mm LEDs, the same principles apply.

ATtiny 85
I recently wanted to find a way to experiment with using an ATtiny (baby brother of the ATmega microcontroller in the Arduino). I picked up some ATtiny85 and ATtiny84 chips off an eBay vendor. The ATiny can be programmed, with a new library, in the Arduino IDE, are much cheaper than an ATmega ($1.3/ea for the ATtiny85 vs $2.30 for the ATmega328p, and $5+ if you don't want it shipped from China), but are limited in the number of input/output options. The ATtiny runs slower (internal clock, the ATmega uses an external), uses less energy, and comes in a bare chip form factor (need some soldering skills to get beyond the breadboard phase). Here's a good comparison of the two chips.

Since the ATtiny chips run off 5v I planned on using a commodity USB power supply. Since the ATtiny85 has an internal clock chip, it's easier to build a circuit (no external crystal).  My original stop light controller was powered by an Arduino Micro (about $20). So I wanted this new controller to be smaller and cheaper. My total cost to replace the Micro was about $3.

Since I used only 5 outputs and no inputs for my light controller, the ATtiny85 is about the smallest and cheapest of the Atmel chips that will do the job.

First, to program the chip. I used a programmer shield, but you can use some hookup wire and do it yourself or build your own shield. Follow these directions if you want to take that route.

Program it!
Here's the code:

"


int Walk = 3;
int DontWalk = 4;
int Green = 0;
int Yellow = 1;
int Red = 2;

void setup()
{
 

  pinMode(Walk, OUTPUT);
  pinMode(DontWalk, OUTPUT);
  pinMode(Green, OUTPUT);
  pinMode(Yellow, OUTPUT);
  pinMode(Red, OUTPUT);
}


void loop()
{

  //HIGH = ON, LOW = OFF
 
  digitalWrite(Walk, LOW);
  digitalWrite(DontWalk, LOW);
  digitalWrite(Green, LOW);
  digitalWrite(Yellow, LOW);
  digitalWrite(Red, LOW);
 
  digitalWrite(Green, HIGH);
  digitalWrite(Walk, HIGH);
  delay(800);
  digitalWrite(Walk, LOW);
  digitalWrite(DontWalk, HIGH);
  delay(50);
  digitalWrite(DontWalk, LOW);
  delay(50);
  digitalWrite(DontWalk, HIGH);
  delay(50);
  digitalWrite(DontWalk, LOW);
  delay(50);
  digitalWrite(DontWalk, HIGH);
  delay(50);
  digitalWrite(DontWalk, LOW);
  delay(50);
  digitalWrite(DontWalk, HIGH);
  delay(50);
  digitalWrite(DontWalk, LOW);
  delay(50);
  digitalWrite(DontWalk, HIGH);
  delay(200);            
 
  digitalWrite(Green, LOW); 
  digitalWrite(Yellow, HIGH);
 
  delay(350);
 
  digitalWrite(Yellow, LOW);   
  digitalWrite(Red, HIGH);  
 
  delay(1200);             

}
"


Now to breadboard it. Follow this pinout guide to wire it up. Here I used LEDs to take the place of the relay outputs to see if it looked like a stop light and to test my timings. Since my relays are triggered by a "high" signal, when the LED is on, the relay would be triggered to send the high voltage to the light.


It works! Now to solder.

Top of the board
Bottom, my lackluster soldering skills...
Now back out to the garage to plug it in to the relays and the lights.


Here is the final, ready to be buttoned up project.

This photo shows the dramatic size reduction you can realize switching from the already small Arduino Micro to a basic ATtiny85 on some perfboard.

Provided you don't need a bunch of inputs/outputs the ATtiny might be a cost, and perhaps more importantly size, effective solution.

Here's the Fritzing diagram:


No comments:

Post a Comment

Search This Blog