Interface Two Channel Relay Module with Arduino

Содержание

tutorial for controlling ac devices with relay module and arduino

Sometimes you want your Arduino to control AC powered devices like lamps, fans or other household devices. But because the Arduino operates at 5 volts, it cannot directly control these higher voltage devices.

That’s where the relay module comes in. You can use a relay module to control the AC mains and Arduino to control the relay.

This tutorial walks you through how to setup the relay to switch on a lamp or other device, but let’s begin with a short introduction into relays.

How Do Relays Work?

A relay is an electromagnetic switch operated by a relatively small current that can control much larger current.

Here’s a simple animation illustrating how the relay uses one circuit to switch on another circuit.

Initially the first circuit is switched off and no current flows through it until something (either a sensor or switch closing) turns it on. The second circuit is also switched off.

When a small current flows through the first circuit, it activates the electromagnet, which generates a magnetic field all around it.

The energized electromagnet attracts a contact in the second circuit toward it, closing the switch and allowing a much bigger current to flow through the second circuit.

When the current stops flowing, the contact goes back up to its original position, switching the second circuit off again.

Relay Basics

Typically the relay has 5 pins, three of them are high voltage terminals (NC, COM, and NO) that connect to the device you want to control.

relay pinout

The mains electricity enters the relay at the common (COM) terminal. While use of NC NO terminals depends upon whether you want to turn the device ON or OFF.

Between the remaining two pins (coil1 and coil2), there is a coil that acts like an electromagnet.

When current flows through the coil, the electromagnet becomes charged and moves the internal contacts of the switch. At that time the normally open (NO) terminal connects to the common (COM), and the normally closed (NC) terminal becomes disconnected.

When current stops flowing through the coil, the internal contact returns to its initial state i.e. the normally closed (NC) terminal connects to the common (COM), and the normally open (NO) terminal reopens.

This is known as a single pole, double throw switch (SPDT).

Two Channel Relay Module

For this tutorial, we are going to use two channel relay module. However there are other modules with one, four and eight channels. You can choose the one that best suits your needs.

two channel relay module hardware overview

This module is designed for switching two high powered devices from your Arduino. It has two relays rated up to 10A per channel at 250VAC or 30VDC.

There are two LEDs on the relay module indicating the position of the relay. Whenever a relay is activated, the respective LED will light up.

One of the best things about these modules is that they come with two Optocoupler ICs which provide good isolation between relay and Arduino.

Output Terminal Blocks

We have three channels per relay broken out to blue screw pin terminals. The channels are labeled for their function: common (COM), normally closed (NC), and normally open (NO)

The names explain the state of the channel with relation to the switch at rest.

two channel relay module output terminal blocks

COM (Common): This is the pin you should connect to the signal (mains electricity in our case) you are planning to switch.

NC (Normally Closed): A normally closed configuration is used when you want to turn off the relay by default. In this configuration the relay is always closed and remains closed until you send a signal from the Arduino to the relay module to open the circuit.

NO (Normally Open): A normally open configuration works the other way in which the relay is always open until you send a signal from the Arduino to the relay module to close the circuit.

Control Pins

On the other side of the module, there are four pins a Ground pin and a VCC pin to power the module and two input pins IN1 and IN2 to control the relay.

two channel relay module control pins

The input pins are active low, meaning the relay will be activated when you pull the pin LOW and it will become inactive when you pull the pin HIGH.

Power Supply Selection Jumper

Notice that the module has a jumper cap connecting the VCC and JD-VCC pins.

two channel relay module power supply selection jumper

The jumper cap allows you to choose whether the circuit is physically connected to the Arduino.

When the jumper is in place, the electromagnet of the relay is directly powered from the Arduino. In this case the relay module and the Arduino are not physically isolated from each other.

relay module power selection jumper setting

Without the jumper cap, you have to provide an independent power source to power the electromagnet of the relay through the JD-VCC pin. This configuration physically isolates the relay module from the Arduino with the help of the built-in optocoupler.

Four Channel Relay Module

The four channel relay module and the two channel relay module work the same way. There is not much difference between the two modules, only the four channel relay module comes with two extra relays and two extra control pins.

Two Channel Relay Module Pinout

Let’s have a look at the pinout of two channel relay module.

relay module pinout

Control Pins:

VCC pin supplies power to the built-in optocoupler and optionally to the electromagnet of the relay (if you keep the jumper in place)

GND is the common Ground connection.

IN1 IN2 pins are used to control the relay. These are active low pins, meaning the relay will be activated when you pull the pin LOW and it will become inactive when you pull the pin HIGH.

Power Supply Selection Pins:

JD-VCC supplies power to the electromagnet of the relay. When the jumper is in place, it takes power from the Arduino’s 5V line. Without the jumper cap, you have to connect it to an independent power source.

VCC With the jumper cap on, this pin is shorted to the JD-VCC pin. If you remove the jumper, keep this pin unconnected.

GND is the common Ground connection.

Output Terminals:

COM pin is connected to the signal you are planning to switch.

NC pin is connected to the COM pin by default, unless you send a signal from the Arduino to the relay module to break the connection.

NO pin is open by default, unless you send a signal from the Arduino to the relay module to make the connection.

Wiring Two Channel Relay Module with Arduino UNO

Now that we know everything about the relay module, it is time to put it to use!

Warning:
This board interacts with HIGH AC voltage. Incorrect or improper use could result in serious injury or death. So, it is intended for people experienced around, and knowledgeable about HIGH AC voltage.

Let’s set up our relay module to switch on a lamp.

Start by connecting VCC pin on the module to 5V on the Arduino and GND pin to ground. Connect the digital pin #6 to the IN1 input pin for controlling the first relay.

You’ll also need to place the relay module in line with the AC powered device (lamp in our case) you’re attempting to control. You’ll have to cut your live AC line and connect one end of the cut wire (coming from the wall) to COM and the other to NC or NO depending on what you want your device’s resting state to be.

If your AC device is going to be off for most of the time, and you occasionally want to turn it on, you should connect the other to NO. Connect to NC if the device will be on for most of the time.

For this project we want our lamp to be off in resting state and illuminated when we activate the relay, so we will put one end in COM and the other in NO.

The following illustration shows the wiring.

wiring relay module with arduino

In the above wiring diagram we have kept the jumper in place, due to which the electromagnet of the relay will be driven directly from the Arduino. In this case the relay module and the Arduino will not be physically isolated from each other.

If you want to keep them isolated then you have to provide an independent power supply through the JD-VCC pin. The following diagram shows how to do this.

wiring relay module with arduino and external supply

Arduino Code

Now that our hardware is all set up, let’s take a look at the code that turns the lamp on.

Here’s the simple code that will turn on the lamp for 3 seconds and keep it off for the next 3 seconds.

Code Explanation:

Controlling a relay module with the Arduino is as simple as controlling an LED. The sketch starts by declaring the pin to which input pin of relay module is connected.

In the setup function, we define that pin as being output.

Now in the loop function, we turn the device ON/OFF by pulling relay pin LOW/HIGH respectively.

digitalWrite(RelayPin, LOW) pulls the pin LOW whereas digitalWrite(RelayPin, HIGH) pulls the pin HIGH.


Источник: lastminuteengineers.com