Friday, November 18, 2011

Sparkfun Inventor's Kit CIRCUIT-11

Ever sat in your car with the blinkers on, listening to that metronome like clicking sound as the lights flash on an off and wondered about the programming and mechanics of that simple behavior? Or watched traffic lights flash from color to color and thought about what goes into that?
This is the world of relays, a land of electrically controlled mechanical switches.  The clicking sound is caused by an electromagnet inside the a little plastic box. When it is energized the coil of wire that makes up the electromagnet becomes magnetic and is attracted to the other side of the switch, pulling it towards the magnet and closing a circuit path, kind of like a train track switching device.  When the current stops the switch goes back to its original position, sending the current down a different track.

When you power this circuit up before loading the code, the yellow LED is lit.  Once you load the circuit it starts switching between the yellow and the red LED with that satisfying blinker click (makes me want to compose a song to its measured tick tock beat...!).

The set up involved 1 10k Ohm Resistor, 1 330 Ohm resistor, 1 P2N2222AG (T092) transistor, one 1N4001 Diode (acting as a "flyback diode -- se http://ardx.org/4001), a yellow LED, a red LED and a Relay (Single Pole, Double Throw or "SPDT").  The wiring looks a bit bird's nest intimidating and the pattern didn't quite line up on the breadboard so I had to be careful to hook up all the wires in the right position, but I got it to work.
This is a case where it helps to look at the schematic at http://ardx.org/CIRC11 , where you will also find the code, to see that the 10K resistor (shown as a 2.2kohm resistor on the website and a 10K in the booklet) connects to Arduino pin 2 and to the base of the transistor, with the emitter going to ground and the collector going to the flyback diode and to the relay pin whose other side is connected to the 5V source which picks up the other side (the negative side) of the diode. The middle pin on the part of the relay facing ground in the schematic, which is actually the pin on the LED side of the physical relay,  is connected to +5 volts while the two pins that are parts of the "different train tracks" (and which the middle 5 volt pin switches between) are each connected to the LEDs through their positive sides, with the negative sides going to ground.








/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
  The circuit:
* LED connected from digital pin 13 to ground.
  * Note: On most Arduino boards, there is already an LED on the board
connected to pin 13, so you don’t need any extra components for this example.
 
Created 1 June 2005
By David Cuartielles

http://arduino.cc/en/Tutorial/Blink

based on an orginal by H. Barragan for the Wiring i/o board
*/

int ledPin =  2;    // Relay connected to digital pin 2 <-----Change this to pin 2

// The setup() method runs once, when the sketch starts

void setup()   {               
  // initialize the digital pin as an output:
  pinMode(ledPin, OUTPUT);   
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()                   
{
  digitalWrite(ledPin, HIGH);   // set the LED on
  delay(1000);                  // wait for a second
  digitalWrite(ledPin, LOW);    // set the LED off
  delay(1000);                  // wait for a second
}



The code here is desperately simple; it is simply the "blink" code used through a relay.  Digital pin 2 is used instead of pin 13 and the rest is exactly what one would use for blinking one LED. The difference is that when pin 2  is powered on (ledPin, HIGH) it makes the relay flip to the "train track" that gives the current to the red LED and when off it reverts to a state where the yellow LED is given current.

The use of a flyback diode is interesting. The wikipedia links tells us, "A flyback diode (sometimes called a snubber diode, freewheeling diode, suppressor diode, or catch diode[1]) is a diode used to eliminate flyback, the sudden voltage spike seen across an inductive load when its supply voltage is suddenly reduced or removed.".

Ever since I started working with Joule Thief circuits I've become fascinated with inductors, wherein a magnetic field is created which, when power is shut off, collapses and creates a voltage.  In the case of a relay this can cause arc-ing so the diode here protects from that.
The book suggests replacing the fly-back diode with a light emitting diode.

No comments:

Post a Comment