Created December 22, 2014

Arduino 5V Relay module (Digital) with Mains Power

Learn how to use relay with a standard 110V power outlet

Full instructions provided5,258
Arduino 5V Relay module (Digital) with Mains Power

Things used in this project

Hardware components

Seeed Studio 5V Relay module (digital) Electronic Brick
×1
Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
any type you want to use
×1
Duplex Ground Fault Receptacle
×1
Proto Shield
optional, or bread board to construct button circuit
×1
LED light
×1
Power Cord grounded
×1
Button Switch
×1
10 ohm resistor
×1
12 inches of 20 gauge YELLOW solid core wire
×1
12 inches of 20 gauge RED solid core wire
×1
12 inches of 20 gauge BLACK solid core wire
×1
12 inches of 18 gauge BLACK 600 volt wire
×1
12 inches of 18 gauge BLACK 600 volt wire
×1
12 position Mini European Style Terminal Block
optional if constructed as shown
×1

Story

Read more

Code

file_11009.txt

C/C++
// Test Relay signal when button is pressed
// Use PIN 7 to send signal to relay
// Turn on LED when the button is pressed to test Sketch code
// and keep it on after it is released
// including simple de-bouncing
//

 #define LED 13                    // the pin for the LED
 #define BUTTON 2              // the input pin where the pushbutton is connected
 #define RELAY 7                 // PIN for relay signal
 
 int val=0;                               // val will be used to store the state
                                                // of the input pin
 int old_val=0;                      // this variable stores the previous
                                               // value of "val"
 int state=0;                          //0=LED off and 1 = LED on

void setup(){
 pinMode(LED, OUTPUT);               // tell arduino LED is an output
 pinMode(BUTTON, INPUT);           // and BUTTON is an input
 pinMode(RELAY, OUTPUT);          // tells arduino RELAY is an output
}

void loop(){
 val=digitalRead(BUTTON);         // read input value and store it -- new input
                                  
 // check if there was a transition
 if ((val==HIGH) && (old_val==LOW)){
   state = 1 - state;
   delay(10);
 }

 old_val=val;                    // val is now old, let's store it

 if (state == 1){
  digitalWrite(LED,HIGH);           // turn LED ON
  digitalWrite(RELAY,HIGH);      // turn RELAY ON
 }else{
  digitalWrite(LED,LOW);            // turn LED OFF
  digitalWrite(RELAY,LOW);       // turn RELAY OFF
 }
} 

Credits

rik

Comments

Add projectSign up / Login