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
Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
any type you want to use
Duplex Ground Fault Receptacle
Proto Shield
optional, or bread board to construct button circuit
LED light
Power Cord grounded
Button Switch
10 ohm resistor
12 inches of 20 gauge YELLOW solid core wire
12 inches of 20 gauge RED solid core wire
12 inches of 20 gauge BLACK solid core wire
12 inches of 18 gauge BLACK 600 volt wire
12 inches of 18 gauge BLACK 600 volt wire
12 position Mini European Style Terminal Block
optional if constructed as shown


Read more



// 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;

 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
  digitalWrite(LED,LOW);            // turn LED OFF
  digitalWrite(RELAY,LOW);       // turn RELAY OFF




Add projectSign up / Login