relay project

Hi!

So I'm trying to do this lab with a 5v dip relay and i noticed that it uses a 2n2222 transistor but i got a 2n3904.

Should it work all the same?

My guess from reading the specs sheets is yes but i wouldn't mind you guys point of view on this.

Thank you

I've tagged my documents to this e-mail so you guys can see what I'm referring to.

CIRC11-sheet-OOML.pdf (106 KB)

datasheet.pdf (99.9 KB)

2n2222.pdf (52.9 KB)

Hi! They are the exact same, except the maximum current the 3904 is 200mAh, as the 2n2222 is 1 amp

If you are running below 2oomAh into the collector, you will be fine, but nothing above or it will burn out.

okay thanks for the heads up

yea, so i'm trying to add a button to my project to switch the relay on and off when i press it.

But it only works when i hold the button down...

Anyone can help me?

int inputPin1 = 5;
boolean inputPin = true; 

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(2, OUTPUT);
  pinMode(inputPin1, INPUT_PULLUP);
}

// the loop function runs over and over again forever
void loop() {
  inputPin=inputPin;
  if (digitalRead(inputPin1) == false) {
  digitalWrite(2, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(2000);              // wait for a second
  digitalWrite(2, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);   
  // wait for a second
}
  inputPin=!inputPin;
  if (digitalRead(inputPin1) == true) {
  digitalWrite(2, LOW);
  delay(1000);
  digitalWrite(2, LOW);
  delay(1000);
  }
}

What is this doing?
inputPin=inputPin;

Why two?
digitalWrite(2, LOW);
delay(1000);
digitalWrite(2, LOW);
delay(1000);

What is this for?
inputPin=!inputPin;

FSstudios1:
Hi! They are the exact same, except the maximum current the 3904 is 200mAh, as the 2n2222 is 1 amp

kinda like they are identical twins in every way, except they do not look alike ?

3904 - 2222
c/e Volt 40 - 50
c/b volt 60 - 75

they are completely different in ratings.

but, for the application, the coil current is the main selection criteria. if your coil current is not over the rating for your chips, you should be good to go.

remember to drive it hard into saturation.

LarryD:
What is this doing?
inputPin=inputPin;

Why two?
digitalWrite(2, LOW);
delay(1000);
digitalWrite(2, LOW);
delay(1000);

What is this for?
inputPin=!inputPin;

I used two, because i have two states with the relay( NC - NO). I want to make the relay stop clicking (in the mean time my leds will stop flashing) and when i press again it runs back on, and my leds start back blinking

inputPin =! inputPin;

usually goes with the boolean variable to be able to differentiate 1 from 0 but i'm not sure if its working right in this project

You aren't using inputPin for anything.

So i think you want to push a button attached to pin 5.
One push the led on pin 2 turns on and stays on.
Another push the led goes off and stays off.
Then this repeats.

Is this correct?

yes that's it

Google "Arduino push on push off"

Have you looked at this :wink:
http://www.arduino.cc/en/tutorial/switch

you will have to change the input from pin 2 to pin 5 and the output from pin 13 to pin 2

/* switch
 * 
 * Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
 * press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
 * a minimum delay between toggles to debounce the circuit (i.e. to ignore
 * noise).  
 *
 * David A. Mellis
 * 21 November 2006
 */

int inPin = 2;         // the number of the input pin
int outPin = 13;       // the number of the output pin

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(inPin, INPUT_PULLUP);
  pinMode(outPin, OUTPUT);
}

void loop()
{
  reading = digitalRead(inPin);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }

  digitalWrite(outPin, state);

  previous = reading;
}

It worked thank you!

I made it work earlier with only the digitalWrite blink with an IF function but forgot to save OMG!

thanks for the help!

but forgot to save

:o

I know, exactly what my face looked like btw

How is this version different?

int inPin = 2;      // switch is connected to this pin
int outPin = 13;    // led is connected to this pin

byte reading;       // the current reading from the input pin
byte flag = LOW;    // if LOW the switch was pushed, if HIGH the switch was released
unsigned long time = 0;         // 
unsigned long debounce = 200UL; // the debounce time

void setup()
{
  pinMode(inPin, INPUT_PULLUP); //the N.O. switch is connected to GND
  pinMode(outPin, OUTPUT);
  digitalWrite(outPin, LOW);
  time = millis();
  flag = LOW; 

}  // END of setup()


void loop()
{
  reading = digitalRead(inPin);

  //have we pushed the switch
  if (reading == LOW && flag == HIGH && (millis() - time > debounce)) 
  {
    time = millis();  //get ready for the next iteration
    //toggle the LED
    digitalWrite(outPin, !digitalRead(outPin));
    //show that the switch was pushed
    flag = LOW;
  }

  //have we let go of the switch
  if (reading == HIGH && flag == LOW && millis() - time > debounce)
  {
    time = millis();  //get ready for the next iteration
    //show that the switch was let go
    flag = HIGH;
  }

}  // END of loop()