the Relay Module

A relay is an electrically operated switch of mains voltage. It means that it can be turned on or off, letting the current go through or not.

Controlling a relay with the Arduino is as simple as controlling an output such as an LED.

The relay module is the one in the figure below.

 

Relay1

 

This module has two channels (those blue cubes). There are other varieties with one, four and eight channels.

 

Mains voltage connections

In relation to mains voltage, relays have 3 possible connections:

 

Relay2

 

  • COM: common pin
  • NO (Normally Open): there is no contact between the common pin and the normally open pin. So, when you trigger the relay, it connects to the COM pin and supply is provided to a load
  • NC (Normally Closed): there is contact between the common pin and the normally closed pin. There is always connection between the COM and NC pins, even when the relay is turned off. When you trigger the relay, the circuit is opened and there is no supply provided to a load.

If you want to control a lamp for example, it is better to use a normally-open circuit, because we just want to light up the lamp occasionally.

 

Pin wiring

The connections between the relay module and the Arduino are really simple:

 

Relay3

  • GND: goes to ground
  • IN1: controls the first relay (it will be connected to an Arduino digital pin)
  • IN2: controls the second relay (it should be connected to an Arduino digital pin if you are using this second relay. Otherwise, you don’t need to connect it)
  • VCC: goes to 5V

 

Example: Controlling a Lamp with a Relay Module and PIR Motion Sensor

 

Relay4

 

In this example you will create a motion sensitive lamp. A lamp will light up for 10 seconds every time motion is detected.

Motion will be detected using a PIR motion sensor.

 

Safety warning

Before proceeding with this project, I want to let you know that you’re dealing with mains voltage. Please read the safety warning below carefully.

 

Relay5

 

 

Parts required

Here’s the needed parts for this example:

  • Relay Module
  • Arduino UNO 
  • PIR Motion Sensor 
  • Lamp Cord Set 

 

Code

Copy the following code to your Arduino IDE and upload it to your Arduino board.

Warning: you shouldn’t upload new code while your Arduino is connected to the relay. 

 

/*********

  Rui Santos

  Complete project details at http://randomnerdtutorials.com  

*********/



// Relay pin is controlled with D8. The active wire is connected to Normally Closed and common

int relay = 8;

volatile byte relayState = LOW;



// PIR Motion Sensor is connected to D2.

int PIRInterrupt = 2;



// Timer Variables

long lastDebounceTime = 0;  

long debounceDelay = 10000;



void setup() {

  // Pin for relay module set as output

  pinMode(relay, OUTPUT);

  digitalWrite(relay, HIGH);

  // PIR motion sensor set as an input

  pinMode(PIRInterrupt, INPUT);

  // Triggers detectMotion function on rising mode to turn the relay on, if the condition is met

  attachInterrupt(digitalPinToInterrupt(PIRInterrupt), detectMotion, RISING);

  // Serial communication for debugging purposes

  Serial.begin(9600);

}



void loop() {

  // If 10 seconds have passed, the relay is turned off

  if((millis() - lastDebounceTime) > debounceDelay && relayState == HIGH){

    digitalWrite(relay, HIGH);

    relayState = LOW;

    Serial.println("OFF");

  }

  delay(50);

}



void detectMotion() {

  Serial.println("Motion");

  if(relayState == LOW){

    digitalWrite(relay, LOW);

  }

  relayState = HIGH;  

  Serial.println("ON");

  lastDebounceTime = millis();

}

view raw   Projects/Arduino_Relay_PIR_Guide.ino

 

 

Schematics

Assemble all the parts as in the schematics below.

Warning: do not touch any wires that are connected to mains voltage. Make sure you have tighten all the screws.

 

Relay6

 

Demonstration

Now, when motion is detected, your lamp lights up.

Relay7

 

Wrapping up

Controlling a relay module with the Arduino is as simple as controlling an output.

With the relay module you can control almost any AC electronics appliance (not just lamps).

I hope you’ve found this guide useful.

 

Les commentaires sont clôturés