Tuesday, November 1, 2011

Larger Loads (CIRC-11)

PURPOSE:

The purpose of this circuit experiment is to control a relay using a transistor, and turn on and off two LEDs.

EQUIPMENT:
·         1 x Arduino Holder
·         1 x Arduino Uno
·         1 x Arduino Breadboard
·         1 x Breadboard Sheet (CIRC-11)
·         1 x Diode (1N4001)
·         1 x Transistor P2N2222AG (TO92)
·         1 x Relay (SPDT)
·         1 x Yellow LED
·         1 x Red LED
·         1 x 10k Ohm Resistor (Brown-Black-Orange)
·         2 x 330 Ohm Resistor (Orange-Orange-Brown)
·         Wires (any colour)
Relay (SPDT)
PROGRAM DETAILS:

This circuit lab is a bit of a test, as we combine our knowledge about using transistors in CIRC03 to control a relay. A relay is an useful device and an electrically controlled mechanical switch. Inside the small, black plastic relay box, there is an electromagnet. When the electromagnet gets energized, it causes a switch to trip and it gives a pleasant clicking sound. In this lab, we also use a part called diode which is the electronic equivalent of a one way valve. It allows current to flow in one direction but not the other. For this lab, the current will flow when end with the line is connected to ground.

In terms of coding, no new method or topic was introduced. In fact, the code for this program is very similar to the one from Getting Started (CIRC-01). The pin the LED is connected to is pin 2. The digital pin is set as output using the pinMode() method and digitalWrite(pin, HIGH/LOW) method is used to set the LED on/off.

At the beginning, the relay, transistor, resistors, and LEDs were attached respectively on to the Arduino breadboard. The wiring was done at the very end. When we were putting everything together, one of our 330 Ohm resistors snipped.  Then we got a new one and this time, we were being extra careful with it since it’s so fragile and easy to break off.

TIME TO COMPLETE:

15 minutes to build, 6 minutes to code.

RESULTS:

At first, we couldn’t hear any clicking sound and we realized this was because the transistor or the coil portion of the circuit isn’t quite working. We checked the transistor to see if it is plugged in the right way. We realized that it wasn’t plugged in the right way so we fixed it, and finally our circuit worked as it was supposed to.

PHOTOS OF PROJECT:


TIPS:

The example code uses pin 13 and this lab has the relay connected to pin 2. If nothing happens after your program is uploaded, then check to see if you changed pin 13 to pin 2 in the code.

FURTHER WORK:

For further work, I would like to control a motor. In CIRC-03, we learned how to control a motor using a transistor. However, if you wish to control a larger motor then using a relay would be the right option. This can be done very easily by removing the red LED, and connecting the motor in its place. (Don’t forget to bypass the 330 Ohm resistor).

PROGRAM MODIFICATIONS:

My program is the same as the program in Sprakfun Inventor’s Guide (Page 28 and 29), and same as the one from http://ardx.org/CIRC11 . However, when I was copying the code from File > Examples > 1.Basic > Blink, I made a small change in the code. The small change is that I changed pin 13 to pin 2 because our relay was connected to pin 2.

PROGRAM (with comments):

/*
  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
}

REFERENCE:



1 comment:

  1. If I change the delay in the code, the relay still switches back and forth every second. The delay doesn't seem to do anything! And I can't get it to work on any other pins. HELP!

    ReplyDelete