110V switch.

Hi. I'm trying to build a circuit using my adruino and a switch to control a 110v relay (SRD-05vdc-SL-C )as the relay connected as normally OPEN:
i'm using a push button.

Basically what I'm doing is I'm wiring up my house because it's in the framing stage. The light switches will go into a closet where they will connect to my relay / arduino controller and out to a button pad. I'm hoping to later use a WIFI shield as well to control the lighting.

So what i did was I modified the BUTTON tutorial. however, Something is wrong.
Can someone help me troubleshoot?

i am trying to use this sketch:

/*Button 
 Turns on and off a RELAY SWITCH connected to digital  
 pin 13, when pressing a pushbutton attached to pin 2. 
 
 
 The circuit:
 * RELAY attached from pin 11 to ground 
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 
 
 
 created 2005
 by DojoDave <http://www.0j0.org>
 modified 30 Aug 2011
 by Tom Igoe
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/Button
 */

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int relayPin =  11;      // the number of the relay pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the relay pin as an output:
  pinMode(relayPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn RELAY on:    
    digitalWrite(relayPin, HIGH);  
  } 
  else {
    // turn RELAY off:
    digitalWrite(relayPin, LOW); 
  }
}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

What appears to be wrong is that the relay will only be actuated whilst the button is being pressed. Look up 'state machine' to overcome this problem.

I hope you've got a reverse bias diode across your relay coil, to kill any back EMF when the relay is switched off. Otherwise it could kill your arduino!

The SRD has a 70 ohm coil and should not be driven by the Arduino without a driver of some kind (transistor, MOS fet or driver like ULN2803).
You will have to deal with bounce suppression of the button contacts (using software or hardware).
Do you what a push ON push OFF type of operation?

I plan to use this as the switch.

http://www.leviton.com/OA_HTML/ProductDetail.jsp?partnumber=LVS-8W&section=37956

Something is wrong.

but you have not said what.

What is wrong? It doesn't turn on when I press the button.

If this relay is 70 ohms, it will try to draw about 70mA (5V / 70) when High. The Arduino pins can handle max 40mA. So that will not work. You will need a driver to handle the greater current (i.e. ULN2003 or a transiscor [universal NPN]).
Also I am not sure about your switch, but normally for simple applications one would connect a pullup resistor (10K) to Vcc and have the switch connect to GND). That ensures the state of the Switch. But in either case the relay would have attracted. Thus you are overloading the Arduino pin.

The switch in the link says it runs of 24V. You're going to have to re-think your hardware schematic if you use that.

The code looks good. It should turn on the relay while the button is pressed. When you let go of the button, the relay should turn back off. I'm not sure that is your plan for the final project, but that code should do that.

Which relay are you using?

I am using this

http://www.ebay.com/itm/10pcs-1-Channel-5V-Relay-Module-Shield-Arduino-ARM-PIC-AVR-DSP-SRD-05VDC-SLC-/251195948214

I'm revising the code to this. Any thoughts?
I'm not near my arduino now.

/* sketch 
turn on a fan using a relay and a button
*/
int pinButton = 8;
int Relay = 2;
int stateRelay = LOW;
int stateButton;
int previous = LOW;
long time = 0;
long debounce = 500;

void setup() {
  pinMode(pinButton, INPUT);
  pinMode(Relay, OUTPUT);
}

void loop() {
  stateButton = digitalRead(pinButton);  
  if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
    if(stateRelay == HIGH){
      stateRelay = LOW; 
    } else {
       stateRelay = HIGH; 
    }
    time = millis();
  }
  digitalWrite(Relay, stateRelay);
  previous == stateButton;
}

I'm revising the code to this. Any thoughts?
I'm not near my arduino now.

  1. You should make both debounce and time unsigned long.
  2. I would enable the pullup on pinButton using digitalWrite(pinButton,HIGH);
  3. I would have all I/Ps buffered with an Opto Isolator.
  4. The I/P to pinButton has to be normally low going high on a push.
  5. I think you will toggle the light if you hold pinButton down (HIGH).

I think the code will work. There might be an artifact--if you hold down the button longer than debounce, and then it bounces when you release it (highly probably), you'll toggle the relay when you let off the button.

Using that relay module should eliminate all your problems with current usage from the Arduino pins. However, you're still going to have to power it. How have you decided to supply the power to the Arduino and the relay modules? And the switches?

So. I've got a button working using the following code.

const int buttonPin = 2;    // the number of the pushbutton pin
const int relay1 = 13;      // the number of the LED pin

// Variables will change:
int relay1State = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

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

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(relay1, OUTPUT);

  // set initial LED state
  digitalWrite(relay1, relay1State);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button 
  // (i.e. the input went from LOW to HIGH),  and you've waited 
  // long enough since the last press to ignore any noise:  

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  } 
  
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        relay1State = !relay1State;
      }
    }
  }
  
  // set the LED:
  digitalWrite(relay1, relay1State);

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
}

I used the debounce excercise.

However, I am using a different switch. http://www.leviton.com/OA_HTML/ProductDetail.jsp?partnumber=LVS-5W&section=37956&minisite=10251
/url]

now what this switch does is it sends out a signal when pressed which is half the voltage that goes into the power in the back to light the LEDs on the buttons. So I'm using a 9volt power supply for the arduino board and thus i've used the OUTPUT power of the Arduino to power it and i've connected the switch to PIN2. I am able to turn on off the relay however, i cannot turn it back on. What could i be doing wrong?

Chris