Chuyển tới nội dung
Home » Arduino Uno Relay Module | Challenge Yourself

Arduino Uno Relay Module | Challenge Yourself

Using 5V 1 channel relay module for Arduino

Conclusion

In this tutorial, we have gone through some basics of how a relay works, including how the internal mechanism works, but also how to create a circuit with high power components, and how to create a program that activates or de-activates them.

Relays are incredible popular electronic components that are practically used everywhere: cars, planes, heating systems, industrial machines and many many more.

Suggested changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.

Arduino – Relay

In a previous tutorial, we have learned how to turn on/off an LED. In this tutorial, we are going to learn how to turn on/off some kind of devices that use the high voltage power supply(such as a light bulb, fan, electromagnetic lock, linear actuator…).

The common: Just like controlling LED, we use the Arduino’s output pin to turn on/off them.

The difference:

  • For LED, we can use power from the Arduino board (≤ 5v). Therefore, we can connect LED directly to Arduino’s pin.
  • For the light bulb, we MUST use another power source (high voltage and/or high current), which can burn Arduino. Therefore, we CANNOT connect the light bulb directly to Arduino’s pin. We need to use a relay between Arduino’s pin and light bulb to protect Arduino from high voltage/current.

Summary

That’s all on Arduino Relay Tutorial: Control High Voltage Devices with Relay Modules! Now that you know what is the relay and how it works, possibilities are endless as you can control many high voltage devices using various devices like the TV remote, Bluetooth, internet, etc with the Arduino.

We hope you’ve found this guide useful. If you have any questions, feel free to leave them down in the comments below.

The labels of the input pins are hidden in the images they supply. The input pins are ‘DC-‘, ‘DC+’, and ‘IN’.

The only hookup that makes sense is to provide 12V to the DC+ and DC-. The module and Arduino must share a common ground. A pin from Arduino will drive the ‘IN’ input. But will it work? Or, do you suggest I set it aside and order a 5V relay module?

Post a cell phone photo of the side that has the jumper so we can see what it says. Typically there is a jumper that allows you to isolate the module with an opto coupler. Either way, we need to see the jumper
label.

Nevermind. I found it on Aliexpress. Apparently the jumper is the H/L level trigger select. I could be wrong
but I guessing that’s the vendor’s way of saying ACTIVE HIGH or ACTIVE LOW. (meaning you turn the relay on with HIGH (5V) or with a low (0V) from the arduino.

Yes, I’m quite sure you can use this with an Arduino.

The module has a relay with 12V coil, so a separate 12V power supply is needed. It’s a good thing to have a separate supply anyways, because if you’re controlling a higher voltage AC load, the added opto isolation gives a much needed benefit.

I believe the circuit would be the same as below, but you would connect the 12V supply to DC+ and DC-.

The arduino connections would be the middle pin (jumper removed) for trigger. This would be connected to Arduino +5V (low level trigger) or GND (high level trigger). The IN1 or CH1 terminal connects to the Arduino output pin.

Here are the notes I’ve made for future reference:

Two Relay Module designs exist.

Relay Module Design 1
Design 1 is distinguished by a jumper used on header-1 with three pins labeled “JD-Vcc”, “Vcc”, and “GND”. A second header, header-2 has pins labeled “GND”, “IN1”, and “Vcc”. An additional “INx” pin is provided for each relay on the module.
To hook up, first remove the jumper.
On header-1, attach a seperate voltage supply to “JD-Vcc” and “GND”. For example, if using a 5V relay module, connect 5V to “JD-Vcc”, and ground to “GND”. (A 12V relay would use a 12V supply.)
On header-2, attach ‘Arduino 5V’ to “Vcc”. Attach each GPIO pin from Arduino to each “INx” pin on the Relay Module. The relays will be activated by a “LOW” signal.
Each relay has outputs labeled “NO”, “COM”, and “NC”. Attach these to the load as necessary. (For example, for a 12V Water Valve, use a 12V Supply with 12V power to “COM”. “NO” attaches to the load’s 12V input”. The ground of the load is attached to the ground of the 12V supply.)
WARNING: Be sure to use a flyback diode across any loads with a coil, such as a water valve. Attach the cathode to the 12V input and the anode to the ground of the load device.

Relay Module Design 2
Design 2 is distinguished by a jumper used on header-1 with one pin labeled “H”, the middle pin is unlabeled, while the third pin is labeled “L”. A second header, header-2 has pins labeled “DC+”, “DC-“, and “CH1”. An additional “CHx” pin is provided for each relay on the module.
To hook up, first remove the jumper.
On header-1, attach ‘Arduino 5V’ to the center, unlabeled pin.
On header-2, attach a seperate 5V supply with 5V to “DC+”, and ground to “DC-“. Attach Arduino GPIO pins to each “CHx” pin. The relays will be activated by a “LOW” signal.
Attach relay outputs as above.

There’s at least one other, which is like your #2 (that is, no JD-Vcc) but with no jumper to select if it’s active high or low. My one is active low, and I guess it’s possible there’s an active high one; so that’s 3 or possibly 4 designs.

How to Use Relays to Control High-Voltage Circuits with an Arduino

To control high-voltage or high-power circuits with an Arduino, you have to isolate them from the Arduino with a relay. Here’s how!

Circuits that operate at high voltages or at high currents cannot be controlled directly by an Arduino. Instead, you use a low-voltage control signal from the Arduino to control a relay, which is capable of handling and switching high-voltage or high-power circuits. A relay consists of an electromagnet that, when energized, causes a switch to close or open. Relays provide complete electrical isolation between the control circuit and the circuit being controlled.

Pins in a Typical Relay

A relay typically has five pins:

  1. Coil input pin1. This is generally connected to the positive terminal of your signal source.
  2. Coil input pin2. This is generally connected to the negative terminal of your signal source.
  3. Normally Open pin(NO). This pin is normally not connected to the common pin, it is connected when the relay is activated.
  4. Normally closed pin(NC). This pin is normally connected to common pin and is disconnected when relay is activated.
  5. Common. In most of the cases, this pin is connected to the ground of the source we use to drive the appliance.

Contactor

Project: Controlling High-Voltage Circuits with a Relay and an Arduino

In this tutorial, we will turn on a 12V motor using a relay. An optocoupler is added to provide even more isolation between the Arduino and the high-power load.

Hardware Required
  • 1 x 12V power supply
  • 1 x DC motor
  • 1 x 5V relay
  • 1 x Arduino Mega 2560
  • 1 x 2N2222A NPN transistor
  • 1 x 4N25 Optocoupler
  • 1 x 1N4148 Diode
  • 1 x 1k resistor
  • 1 x jumper wires

Click here for complete BOM.

Wiring Diagram

The circuit shown in the diagram below uses a relay to switch 12V across a DC motor. To turn on the motor, the program writes a HIGH value to pin 3, which activates the optocoupler which in turn switches on the transistor. When the transistor turns on, current flows through the relay coil causing the relay to close, which connects 12V across the motor, making it spin.

Arduino Code


void setup() { pinMode(3,OUTPUT); } void loop() { digitalWrite(3,HIGH); // motor runs for one sec delay(1000); digitalWrite(3,LOW); // motor stops for one sec delay(1000); }

Watch the Project Video

Give this project a try for yourself! Get the BOM.

Số lượng mua (Cái)

Đơn giá (VND)

1+ 59.000
10+ 58.000
20+ 57.000

Giao hàng toàn quốc

Thanh toán khi nhận hàng

Cam kết đổi/trả hàng

Thuộc tính Giá trị Tìm kiếm

Điện áp kích

5 VDC

Mức kích

Kích mức cao

Loại relay

Thường

Số kênh

124 Sản phẩm tương tự
Relay 4-Kênh Relay
Giao tiếp Chuẩn Arduino Shield
Mức kích Kích mức cao
Led báo Led báo màu đỏ
Điện áp hoạt động 5V
Dòng điện 130mA
Điện áp tải Max 250VAC/30VDC
Dòng Max 3A

Lưu ý: Sản phẩm không được bảo hành. Quý khách vui lòng tham khảo Quy định bảo hành và Quy định đổi trả hàng

int RL1 = 4; int RL2 = 5; int RL3 = 6; int RL4 = 7; void setup() { pinMode(RL1, OUTPUT); pinMode(RL2, OUTPUT); pinMode(RL3, OUTPUT); pinMode(RL4, OUTPUT); for (int i = 4; i < 8; i++) { digitalWrite(i, LOW); } } void loop() { for (int i = 4; i < 8; i++) { digitalWrite(i, !digitalRead(i)); delay(500); } }

Chat

Hướng dẫn sử dụng Module Relay 2 kênh với Arduino

Relay là một linh kiện phổ biến được nhiều học sinh, sinh viên sử dụng trong các dự án nghiên cứu và học tập. Vậy bạn đã biết gì về module Relay 2 kênh? Nếu bạn đang thắc mắc về câu hỏi này thì bài viết này dành cho bạn. Thông qua bài viết các bạn sẽ nắm tất tần tật về cấu tạo, cũng như cách thức hoạt động của Relay.

Để tạo động lực cho Team Arduino KIT ra nhiều bài viết chất lượng hơn, các bạn có thể ủng hộ mình bằng cách Donate qua MoMo, Ngân hàng, Paypal…Nhấn vào link bên dưới nhé.

Xem ngay: Điều khiển Đèn 220V bằng Rơ le (Relay) sử dụng Arduino

Using 5V 1 channel relay module for Arduino
Using 5V 1 channel relay module for Arduino

How does the Relay works?

As mentioned above, the relay is an electrically operated switch where the relay opens when the two contacts are disconnected, while the relay is closed when the two contacts touch. When set to high, the relay will close allowing current to flow.

Even though there are many types of relays, electromechanical relays are the most commonly used which we are going to talk about them and how they work. They consist of coils, armatures, and contacts:

When the coil is energized, the induced magnetic field moves the armature, which opens or closes the contact.

Each contact connects to an input or output terminal. The input terminal is called Pole, and the output terminal is called Throw. According to the number of terminals, the relay is divided into several types. where the commonly used are the SPST and SPDT. Let’s look at how the SPST and the SPDT works:

SPST (Single Pole Single Throw)

The SPST is the simplest relay, you can consider it as a button. They have 2 terminals that can be connected or disconnected. Including 2 for the coil, an SPST relay has 4 terminals in total

This switch is normally open and when the trigger signal comes, the pole contact will connect to the throw contact which causes the switch to be closed. It is great for applications that need only an on or off state. If this the relay you are looking for, the Grove-Relay is perfect for you.

SPDT(Single Pole Double Throw):

The SPDT relay is also known as the A/B switch, as you can see below, there are two throws, this kind of relay is great for selecting between two options.

As you can see, they have three high voltage terminals that connect to the device you want to control which are the:

  • COM – Common terminal
  • NC – Normally closed 120 – 240V terminal
  • NO – Normally open 120 – 240V terminal

And also normally additional 3 or 4 voltage pins which connect to the Arduino which are:

  • 1 – Ground: Connects to the Ground on the Arduino
  • 2 – 5V VCC: Connects to the 5V on the Arduino
  • 3 & 4 – Signal Pins: Carries the trigger signal from the Arduino that activates the relay

Here is an example of the pinout of an SPDT relay which is our Grove – 2-Channel SPDT Relay:

The NC terminal is normally used if you want the relay to be closed by default which = current is flowing unless a signal is sent from the Arduino to the relay module to open the circuit and stop the current.

The NO terminal works the other way where the relay is always is open and is broken unless a signal is sent from the Arduino to close the circuit.

If you wish to light up a lamp occasionally, using a normally open circuit configuration will be better.

One-Channel Relay Module Hardware Overview

The one-channel relay module is designed to allow your Arduino to control a single high-powered device. It has a relay with a maximum current rating of 10A at 250VAC or 30VDC.

Modules with two, four, and eight channels are also available. You can choose the one that best meets your needs.

LEDs

The relay module contains two LEDs.

The power LED will light up when the module is powered on. The status LED will light up when the relay is activated.

Output Terminal Block

The high voltage terminals (NC, COM, and NO) of the relay are broken out to a screw terminal. The device you want to control can be connected across them.

Control Pin

On the other side of the module, there is an input pin IN for controlling the relay. This pin is 5V logic compatible, so if you have a microcontroller like an Arduino, you can drive a relay with any digital output pin.

The input pin is active low, which means that a logic LOW activates the relay and a logic HIGH deactivates it.

Module Power

The module operates on 5 volts and draws approximately 70 mA when the relay is activated.

The module also includes a flyback diode that is connected in parallel with the relay coil to safely shunt current when the relay coil is de-energized.

How to use 5v relay module | Arduino UNO with 5v relay module [Code and circuit diagram]
How to use 5v relay module | Arduino UNO with 5v relay module [Code and circuit diagram]

Pin wiring

The low-voltage side has a set of four pins and a set of three pins.

The set at the right consists of VCC and GND to power up the module, and input 1 (IN1) and input 2 (IN2) to control the bottom and top relays, respectively.

The second set of pins consists of GND, VCC, and JD-VCC pins. The JD-VCC pin powers the electromagnet of the relay.

Note: notice that the module has a jumper cap connecting the VCC and JD-VCC pins; the one shown here is blue, but yours may be a different color. The jumper cap allows you to choose whether the circuit is physically connected to the Arduino circuit or not, and you can choose to have it on or not. With the jumper cap on, the VCC and JD-VCC pins are connected. That means the relay electromagnet is directly powered from the Arduino’s power pin, so the relay module and the Arduino circuits are not physically isolated from each other (this is the configuration we’ll use). Without the jumper cap, you need to provide an independent power source to power up the relay’s electromagnet through the JD-VCC pin. That configuration physically isolates the relays from the Arduino with the module’s built-in optocoupler.

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

  • 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

Challenge Yourself

  • Automatically turn on the light when you enter into your room and turn off the light after you leave 30 seconds. Hint: Refer to Arduino – Motion Sensor.

A single-channel relay is an electronic switch that can be controlled by a low-power electrical signal, such as the output from an Arduino microcontroller. By using an Arduino Uno and a single-channel relay module, you can control high-voltage or high-power devices, such as lights, motors, and appliances, from your computer or mobile device. In this blog, we will explore how a relay works, how to interface a single-channel relay with an Arduino Uno, and demonstrate a simple example of how to use the 5v relay module to control a lamp.

Single Channel Relay Module Pinout

VCC – this pin provides power to the module

GND – this is the common ground

IN – This pin is also called the control pin because it is used to control the output of the relay.

COM – is connected to the device you intend to connect.

NC – terminal is connected to the COM terminal by default unless you activate the relay which breaks the connection

NO is normally open unless you activate the relay which then connects it to the COM terminal

NOTE: – In a few generic versions of the relays, the pin sequence could be different. Remember to check the annotations on the PCB or the datasheet before making the connections

Single Channel Relay Module Parts

The construction of a single-channel relay typically consists of the following components:

1. Coil: The coil generates a magnetic field when an electrical current is passed through it, which is used to open or close the switch contacts.

2. Contacts: The contacts are the switching elements of the relay and can be normally open (NO) or normally closed (NC). When the coil is energized, the magnetic field attracts a movable armature, which opens or closes the contacts.

3. Armature: The armature is a movable component that is attracted by the magnetic field generated by the coil. It opens or closes the contacts, depending on the state of the coil.

4. Frame: The frame provides mechanical support for the relay components and protects the relay from external damage.

5. Terminals: The terminals provide a means of connecting the relay to external circuits. The coil is connected to a control circuit, while the contacts are connected to the load.

In addition to these basic components, some relays may also include additional features, such as LED indicators, protection diodes, snubber circuits, or other components to enhance their performance and reliability.

Common questions about Single Channel Relay Module

What is the difference between 1 channel and a 2-channel relay?

A 1-channel relay has a single switch or channel, which means it can only control one load or circuit at a time. This type of relay is typically used in simple applications where only one load needs to be switched, such as turning a single light on or off.

A 2-channel relay, on the other hand, has two switches or channels, which means it can control two separate loads or circuits independently. This type of relay is often used in more complex applications where multiple loads need to be switched, such as controlling two separate lights or motors.

Why are relays used?

Relays are used to switch high power loads using a low power control signal, providing electrical isolation between the control circuit and the load. They are often used to control lights, motors, and other high-power devices, and can be controlled by a variety of signals, such as switches, sensors, or microcontrollers. Relays are also used to switch different loads independently, and to protect sensitive electronic components from high voltages and currents.

What is the difference between SSR and relay?

The main difference between SSRs and relays is that SSRs use solid-state electronics to switch loads, while traditional relays use electromechanical contacts. SSRs provide faster switching speeds, and longer lifespan, and do not produce electrical arcing, but are typically more expensive than traditional relays.

How does a Single Channel Relay work?

“A relay is a simple electrical component that allows you to control a high-power electrical device with a low-power electrical switch.”

Now, what does this really means?

It’s like a middleman that acts as an on/off switch for another device.

Think of a relay like a switch that is controlled by electricity. Instead of you physically flipping the switch, an electrical signal does it for you. Here’s how it works:

An electrical current flow through the coil of wire around the relay’s electromagnet, creating a magnetic field.

This magnetic field attracts the metal armature inside the relay and causes it to move, making contact with a switch.

This switch can then control the flow of electricity to a high-power device, like a light, motor, or heating element.

When the electrical current to the coil is turned off, the magnetic field disappears, and the armature returns to its original position, breaking the connection to the switch.

In this way, a relay acts like a switch that can be controlled remotely. It allows you to control a high-power device with a low-power electrical signal, making it very useful in many applications, such as automotive electronics, home automation, and industrial control systems.

Circuit Diagram of Interfacing Single Channel Relay with Arduino UNO

Below is the circuit diagram to control AC appliances using Arduino and relay:

A relay usually has 3 or 4 pins. This relay has 3 pins- VCC, GND and signal

Connect the VCC pin to the 5V pin on the Arduino, the GND pin to a GND pin on the Arduino, and the IN pin to a digital pin on the Arduino.

Arduino Code

Example code of Arduino interfacing with single channel relay module

int relay_pin = 7; void setup() { pinMode(relay_pin,OUTPUT); } void loop() { digitalWrite(relay_pin,HIGH); delay(2000); digitalWrite(relay_pin,LOW); delay(2000); }

Explanation of the Code

int relay_pin = 7;

This code sets the variable “relay_pin” to 7, which means that the relay is connected to digital pin 7 on the Arduino Uno.

void setup() { pinMode(relay_pin,OUTPUT); }

In the setup() function, the pinMode() function is used to configure the relay pin as an output, meaning that the pin will be used to send signals to the relay.

void loop() { digitalWrite(relay_pin,HIGH); delay(2000); digitalWrite(relay_pin,LOW); delay(2000); }

In the loop() function, the digitalWrite() function is used to send a digital signal to the relay pin. The first time digitalWrite() is called, it sets the relay pin to HIGH, which turns on the relay. The delay() function is then used to wait for 2000 milliseconds (2 seconds) before turning the relay off by setting the relay pin to LOW. This sequence is repeated continuously in the loop.

In summary, this code alternates the state of the relay every 2 seconds, turning it on and off continuously.

Projects Using Relay module

Never forget to water your plants again with this intelligent irrigation system. Using an Arduino microcontroller, moisture sensors, and a relay which controls the water pump, it automatically waters your plants and sends you a text message alert when the soil is dry. Keep your plants healthy and thriving with this easy-to-build DIY project.

Want to control high-power devices with ease? Add a relay to your circuit! With its ability to switch high voltage and current loads using a low-power signal, the relay is the perfect electronic switch for your project. From controlling lights to motors and more, a relay can handle it all. Learn how to use a relay in your circuit and take your projects to the next level.

Say goodbye to fumbling for light switches in the dark with this innovative project. Using a PIR sensor and relay, the Automatic Staircase Lights system detects when you enter the staircase and automatically turns on the lights. No more wasted energy and no more tripping in the dark. Bring safety and convenience to your home with this easy-to-build DIY project.

Keep your water tank levels under control with this smart and simple project. By using an Arduino microcontroller, water level sensors, and a water pump, this system automatically monitors and controls the water level in your tank. With real-time level indicators and automated water filling, you’ll never have to worry about running out of water again. Build your own Automatic Water Level Indicator and Controller and experience water management made easy.

int relay_pin = 7;

void setup() {

pinMode(relay_pin,OUTPUT);

void loop() {

digitalWrite(relay_pin,HIGH);

delay(2000);

digitalWrite(relay_pin,LOW);

delay(2000);

In your upcoming project, you might want to use your Arduino to control a high-voltage device, like a lamp, fan, or other household appliance. However, because the Arduino runs on 5 volts, it cannot directly control these high-voltage appliances.

This is where relay modules come into play. These well-contained modules are inexpensive, simple to connect, and ideal for home-brew projects that require switching modest amounts of AC or DC power. The only downside is that, because these are electro-mechanical devices, they are more prone to wear and tear over time.

This tutorial will walk you through setting up the relay module to turn on a lamp or other device, but first, a quick primer on relays.

What is Relay? | How to control Relay using Arduino
What is Relay? | How to control Relay using Arduino

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

In this example, we create a motion sensitive lamp. A lamp lights up for 10 seconds every time motion is detected.

Motion will be detected using a PIR motion sensor. If you are not familiar with the PIR motion sensor, you can read the following post:

To control the lamp with mains voltage we’ll use a relay module in normally-open configuration.

How to use the Relay with Arduino?

Now that we know what is the relay, how it works and which type of relay will suit your project the most, let us get started by using them with the Arduino.

For today’s example, we will be showing you how to control a relay with a button where when 1 button is pressed, the relay will close and when another button is pressed, the relay will open.

What do you need?

  • Seeeduino V4.2
  • Base Shield V2 – Optional, to make connections easier
  • 2 x Grove – Button
  • Grove – Relay

Step by step instructions

Step 1: Connect Hardware

  • Firstly connect Grove-Relay to port D4 of Grove-Base Shield.
  • Connect Grove-Button#1 to port D2 of Grove-Base Shield and connect Grove-Button#2 to port D3 of Grove-Base Shield.
  • Plug Grove – Base Shield into Seeeduino.
  • Connect Seeeduino to PC via a Micro-USB cable.
  • Your connection should look something like this:
  • If you do not have the base shield, you can directly connect the relay and button to the Arduino board by following the below connection:
Grove – Relay Arduino Grove Cable
GND GND Black
VCC 5V Red
SIG D4 Yellow
Grove – Button #1 Arduino Grove Cable
GND GND Black
VCC 5V Red
SIG D2 Yellow
Grove – Button #2 Arduino Grove Cable
GND GND Black
VCC 5V Red
SIG D3 Yellow

Step 2: Code

  • Next, open up Arduino IDE and copy the following code into a new sketch.


// Relay Control void setup() { pinMode(2, INPUT); pinMode(3, INPUT); pinMode(4, OUTPUT); } void loop() { if (digitalRead(2)==HIGH) { digitalWrite(4, HIGH); delay(100); } if (digitalRead(3)==HIGH) { digitalWrite(4, LOW); } }

Step 3: Uploading Code

  • After copying the code onto a new sketch, you will need to upload the demo.
  • If you are unsure of how to upload the code, you can check out our guide on How to Upload Code.

And you are done! If you press the button#1 the relay should be on and if you press the button#2 the relay should be off.

As you can see, controlling the relay with the Arduino is as simple as controlling an output where you just need to send HIGH or LOW signals using an Arduino digital pin.

Want to put everything you have learned to the test? You can try to control almost any AC electronics appliances!

Warning

However, do note that when making projects that are connected to mains voltage, MAKE SURE YOU KNOW WHAT YOU ARE DOING as they involve high voltages that can cause SERIOUS INJURY OR DEATH.

Please take all necessary precautions and turn off all power to a circuit before working on it. If in doubt contact a professional such as a licensed electrician for help.

How to use an Arduino Relay Module
How to use an Arduino Relay Module

Wiring a One-Channel Relay Module to an Arduino

Now that we know everything about the relay module, it’s time to put it to use! Let’s wire up our relay module to operate a lamp.

Warning:

Begin by connecting the module’s VCC pin to the Arduino’s 5V pin and the GND pin to ground. Connect digital pin #6 to the IN input pin.

You’ll also need to connect the relay module to the AC-powered device you want to control, in this case, a lamp. You’ll need 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 initial state to be.

If you want to keep your device off most of the time and turn it on occasionally, connect the other end of the wire to NO. Otherwise, connect it to NC.

For this project, we want our lamp to be off at first and then turn on when we activate the relay, so we will connect one end of the wire to COM and the other to NO.

The following illustration shows the wiring.

Mains voltage connections

The high-voltage side has two connectors, each with three sockets: common (COM), normally closed (NC), and normally open (NO).

  • COM: common pin
  • NC (Normally Closed): the normally closed configuration is used when you want the relay to be closed by default, meaning the current is flowing unless you send a signal from the Arduino to the relay module to open the circuit and stop the current.
  • NO (Normally Open): the normally open configuration works the other way around: the relay is always open, so the circuit is broken unless you send a signal from the Arduino to close the circuit.

If you just want to light up a lamp occasionally, it is better to use a normally-open circuit configuration.

Control High Voltage Devices – Arduino Relay Tutorial
Control High Voltage Devices – Arduino Relay Tutorial

The Arduino 4 Relay Shield

Inside the shield, the low power circuit is already made. The only thing we need to connect is a power supply (max 48V), and a high power component (max 48V). These are connected to the high power pins. In the image below, you can get a better understanding on the layout of the shield:

Circuit

Let’s begin by mounting our Arduino 4 Relay Shield on top of an Arduino UNO.

Programming the Board

We will now get to the programming part of this tutorial.

First, let’s take a look at how we will activate our relays. We are actually not using a library, as the operation is very basic.

  • – assigns


    int relay_1 = 4;

    to pin 4. It is important that we assign it to pin 1, as the relay is internally wired to this pin.


    relay_1

  • – assigns


    int relay_2 = 7;

    to pin 7. Same here, the relay is wired to pin 2, so we can’t use a pin of our choosing.


    relay_2

  • – assigns


    int relay_3 = 8;

    to pin 8. Same here, the relay is wired to pin 2, so we can’t use a pin of our choosing.


    relay_3

  • – assigns


    int relay_4 = 12;

    to pin 12. Same here, the relay is wired to pin 2, so we can’t use a pin of our choosing.


    relay_4

  • – configures relay 1 to be an


    pinMode(relay_X, OUTPUT)


    OUTPUT

  • – write either a high or low state to relay 1.


    digitalWrite(relay_X, state)

The sketch can be found in the snippet below. Upload the sketch to the board.

1int relay_1 = 4;2int relay_2 = 7;3int relay_3 = 8;4int relay_4 = 12;56void setup() {7 // put your setup code here, to run once:8 Serial.begin(9600);910 pinMode(relay_1, OUTPUT);11 pinMode(relay_2, OUTPUT);12 pinMode(relay_3, OUTPUT);13 pinMode(relay_4, OUTPUT);1415}1617void loop() {1819 digitalWrite(relay_1, HIGH);20 digitalWrite(relay_2, HIGH);21 digitalWrite(relay_3, HIGH);22 digitalWrite(relay_4, HIGH);2324 Serial.println(“All relays ON”);2526 delay(1000);2728 digitalWrite(relay_1, LOW);29 digitalWrite(relay_2, LOW);30 digitalWrite(relay_3, LOW);31 digitalWrite(relay_4, LOW);3233 Serial.println(“All relays OFF”);3435 delay(1000);36}

Testing It Out

After we have uploaded the code, the program will start running immediately. If everything is working correctly, we will hear a “tick-tack” noise every second. This is the sound of the relays that are mechanically switching on and off. If we take a look at the shield, we will see four LEDs blinking every second. These signify the state of the relays. We can also view the states in the Serial Monitor.

Now in this example, we have simply activated the relays, but we still haven’t connected anything to them. While we are not going to go in-depth on how to connect high power components, we can take a look at how a circuit looks like for turning ON or OFF a 24V lamp.

Let’s begin with the high power pins on the Arduino 4 Relays Shield. There are twelve in total for all four relays, where there are three different type of connections: COM, NC and NO. Below is how the high power pins for Relay 4 looks like.

In this scenario, we are going to use the NC configuration, which means that writing a LOW signal to the relay will connect the NC pin to COM, which provides power to the component connected. The circuit for this could look like this:

In this circuit, we are using a 24V power supply and a 24V light bulb. Now, if we were to write a program for this, we would activate through using:

1digitalWrite(relay_4, LOW)

and to de-activate it:

1digitalWrite(relay_4, HIGH)

Note: Use extreme caution when creating higher power circuits. Make sure that both the power supply and the component does not exceed 48V. For example, connecting it straight to a wall socket without a power converter would supply 220-240V, which is 5 times as high.

Troubleshoot

If the code is not working, there are some common issues we can troubleshoot:

  • We have not connect the LEDs properly (this is an optional requirement).
  • If the code fails to compile, make sure there’s no missing curly brackets {} or semicolons ; anywhere in the code.
  • We have connected the shield correctly on top of the board (the pins should match each other).
Connecting a Relay Module to a Microcontroller
Connecting a Relay Module to a Microcontroller

Relays

Relays allow low-power microcontrollers to handle circuits that uses much higher power than what the board can handle directly. They are typically used in industrial applications to control high power circuits, but it is also used in cars, homes and other electric applications.

Relays are composed by an electromagnet that moves a tiny metallic plank, which is called COM terminal, between two different positions NC terminal and NO terminal. We can decide in which position the COM terminal is connected to through activating/deactivating the electromagnet, by connecting a low power signal in the electromagnet control terminals.

Writing a program to control the relays is very easy: it works very similar to turning ON or OFF an LED. Take a look at the snippet below to understand how it is used:

1digitalWrite(relay, HIGH);23digitalWrite(relay, LOW);

And that’s basically how we control the relays. Depending on the configuration, the logic will be inverted. For example, if we are using an NC (normally closed) configuration, we need to write a LOW signal to activate the relay. If we are using an NO (normally open) configuration, we need to write a HIGH signal to activate the relay.

About Relay

A relay is a programmable electrical switch, which can be controlled by Arduino or any micro-controller. It is used to programmatically control on/off the devices, which use the high voltage and/or high current.

It is a bridge between Arduino and high voltage devices.

WARNING

When you are making projects that are connected to mains voltage, you need to know what you are doing, otherwise, you may shock yourself. This is a serious topic, and we want you to be safe. If you’re NOT 100% sure what you are doing, do yourself a favor and don’t touch anything. Ask someone who knows!

Although some kinds of relays support both DC and AC devices, We highly recommend you to use a DC device (≤24V) for testing.

Relay Pinout

Relay has two groups of pins: input (low voltage) group and output (high voltage) group.

  • Pins in the input group are connected to Arduino, including three pins:
  • DC- pin: needs to be connected to GND (0V)
  • DC+ pin: needs to be connected to VCC (5V)
  • IN pin: receives the control signal from Arduino
  • Pins in the output group are connected to the high voltage device, including three pins (usually in screw terminal):
  • COM pin: is the common pin. It is used in both normally open mode and normally closed mode
  • NO pin: is normally open pin. It is used in the normally open mode
  • NC pin: is normally closed pin. It is used in the normally closed mode
  • We use only COM pin and NO pin if we use normally open mode.
  • We use only COM pin and NC pin if we use normally closed mode.
  • LOW level trigger mode
  • HIGH level trigger mode
  • normally open mode
  • normally closed mode. These modes are the opposite.
  • The normally open and normally closed mode work oppositely
  • The most of relay modules supports both normally open and normally closed mode
  • The LOW level trigger and HIGH level trigger mode work oppositely
  • NOT all of relay modules supports both LOW level trigger and HIGH level trigger mode
  • At a time, The relay module can work at only one of two LOW level trigger and HIGH level trigger mode
  • If the IN pin is connected to LOW (0V), the switch is open. The device is OFF (or inactive).
  • If the IN pin is connected to HIGH (5V), the switch is closed. The device is ON (or active).
  • If the IN pin is connected to LOW (0V), the switch is closed. The device is ON (or active).
  • If the IN pin is connected to HIGH (5V), the switch is open. The device is OFF (or inactive).
  • Connect an Arduino’s pin to the IN pin of the relay
  • Control the relay by programming the pin to LOW or HIGH

In practice, we usually do NOT use all of the pins in the high voltage group. We use only two of them:

Additionally, if the relay supports both LOW and HIGH level triggers, there is usually a jumper to select one of two: LOW level trigger or HIGH level trigger.

※ NOTE THAT:

The order of the relay module’s pins can vary between manufacturers. ALWAYS use the labels printed on the relay. Look closely!

How to Connect the High Voltage Device to Relay

How It Works

Depending on manufacturers and user’s installation, a relay can work differently.

The input mode mode (for IN pin): There are two input modes that make relay works oppositely:

The output mode mode (for output pins): There are two output modes that make relay works oppositely:

The “normally” means “if IN pin is connected to LOW (0V)”.

Before going into detail, let’s see some quick information:

The combination of the input modes mode and output modes modes creates many use cases. If you are a beginner, we recommend using HIGH level trigger mode and normally open mode

Because the LOW level trigger and HIGH level trigger mode work oppositely, The next will explain the HIGH level trigger mode in detail. The LOW level trigger works oppositely.

HIGH Level Trigger – Normally Open Mode

To use this mode, we need to connect the high voltage device to the COM pin and NO pin.

HIGH Level Trigger – Normally Closed Mode

To use this mode, we need to connect the high voltage device to the COM pin and NC pin.

Summary
Input modes Output Modes IN pin (programmable) Output pins Relay state Device state
HIGH Trigger Normally Open LOW COM and NO pin ⇒ open ⇒ OFF
HIGH Trigger Normally Open HIGH COM and NO pin ⇒ closed ⇒ ON
HIGH Trigger Normally Closed LOW COM and NC pin ⇒ closed ⇒ ON
HIGH Trigger Normally Closed HIGH COM and NC pin ⇒ open ⇒ OFF
LOW Trigger Normally Open LOW COM and NO pin ⇒ closed ⇒ ON
LOW Trigger Normally Open HIGH COM and NO pin ⇒ open ⇒ OFF
LOW Trigger Normally Closed LOW COM and NC pin ⇒ open ⇒ OFF
LOW Trigger Normally Closed HIGH COM and NC pin ⇒ closed ⇒ ON

There are up to 8 use cases. It may overload you. However, If you are a newbie, you just need to care about the two first cases, where HIGH level trigger and normally open are used. The rest of this tutorial will use those two use cases

Arduino – Relay

Arduino controls a high voltage device by controlling a relay.

Controlling a relay is simple. We just need:

#18 Add a Relay Module to your Arduino project - Hints, Tips & Traps
#18 Add a Relay Module to your Arduino project – Hints, Tips & Traps

Introduction

In this tutorial, we will test out the four relays on board the Arduino 4 Relays Shield. This shield is a great addon for the Arduino UNO board, as it has four relays capable of handling loads up to 48V. To control the relays, we need to use the following pins:

  • Relay 1 – pin 4.
  • Relay 2 – pin 7.
  • Relay 3 – pin 8.
  • Relay 4 – pin 12.

The sketch we will use is going to be very simple. It will activate all four relays for one second, then de-activating them for one second. The status of the relays can be seen onboard the shield, as it has a status LED for each relay.

Note: Please use extreme caution when using relays and higher power loads. Powering the relays directly from a wall socket can be extremely dangerous, and exceeds the maximum voltage the relays can handle by far.

What is a Relay Module?

Before we begin, we must know what is the relay how it works before we use it to power high voltage devices.

The relay module is an electrically operated switch that can be turned on or off deciding to let current flow through or not. They are designed to be controlled with low voltages like 3.3V like the ESP32, ESP8266, etc, or 5V like your Arduino.

On top of relay modules, you will see channels which are the black cube as seen above. You may see relay modules with more channels from two channels, four channels to also eight channels.

How to use 5V Relay to Turn ON and OFF AC Light bulb - Arduino Tutorial
How to use 5V Relay to Turn ON and OFF AC Light bulb – Arduino Tutorial

Types of Relay modules here at Seeed

Before we begin, we need to have a relay module! Here at Seeed, we offer various types of relays! From electromechanical relays, solid-state relays to reed relays.

For this project, we will be using electromechanical relays that are cheap, cost-effective, able to withstand large inrush currents and can carry high voltage and high current load.

Not to mention, all the electromechanical relays are Grove compatible. Grove is our modular, standardized connector prototyping system that makes connecting, experimenting and building projects easier. To find out more above Grove, you can check out our wiki page!

Here are the relay options you can consider:

Grove – Relay

  • This is a simple electromechanical relay that can work low voltages like 3.3V like the ESP32, ESP8266 etc or the 5V like your Arduino with a max switching voltage 250VAC / 30VDC and current of 5A.
  • There is an indicator LED on the board, which will light up when the controlled terminals get closed.
  • This is an SPST relay which is great for applications that need only an on or off state.

Grove – 2-Channel SPDT Relay

  • The Grove – 2-Channel SPDT Relay has two single pole – double throw (SPDT) switches. It only requires low-voltage and low current signals to control those switches. Specifically, you can use 5V DC to control max.250V AC or 110V DC.
  • The best thing is that you can control the two channels separately. For instance, controlled by SIG1, you can connect the COM1 to NC1 or NO1 as you wish.
  • It is so convenient and reliable that it can be applied to massive products or projects which need to switch high voltage/high current devices.

Grove – 4-Channel SPDT Relay

  • The 4-channel SPDT relay is similar to the 2 channel SPDT relay but with four single pole – double throw (SPDT) switches which only require low-voltage and low current signals to control them.
  • We use an onboard STM32F030F4P6 to control the channels separately. The control command is transmitted via the I2C interface, the on-board STM32F030F4P6 will parse the command so that you can control the switch you want.

Do not know which relay to pick, no worries! We compiled them into a table and compared their differences so you know which relay will suit your project needs the most!

Relay Name Operate voltage Input current Rated load Contact resistance Insulation resistance Operate time Release time Input interface
Grove – Relay 3.3V-5V 100mA 5A@250VAC

5A@30VDC

50mΩ

@6VDC 1A

100MΩ 10ms Max 5ms Max Digital
Grove – 2-Channel SPDT Relay 5V 90mA 10A@250VAC

10A@30VDC

100mΩMax. 100MΩ Min.@500VDC 10ms Max 5ms Max Digital
Grove – 4-Channel SPDT Relay 5V 90mA 10A@250VAC

10A@30VDC

100mΩMax. 100MΩ Min.@500VDC 10ms Max 5ms Max I2C

Not what you are looking for? Here at Seeed, we offer over a dozen relays from electromechanical relay, solid-state relay to also reed relay. In addition, we also offer several special functions relays like a relay shield and a Heelight relay which you can control using sound commands!

Interested? Check out our Seeed Relay Selection Guide to find the relay that suits your project the most!

Wrapping Up

Controlling a relay module with the Arduino is as simple as controlling an output – you just need to send HIGH or LOW signals using an Arduino digital pin. With the relay module you can control almost any AC electronics appliances (not just lamps).

We hope you’ve found this guide useful. If you like this project, you may also like our premium Arduino course:

We have more than 60 free tutorials and projects with the Arduino. If you’re looking for a guide for a specific module, we probably have what you’re looking for.

Finally, you can also get access to our FREE resources here.

Thanks for reading.

January 15, 2019

Arduino 4 Relays Shield Basics

Learn the basics of how relays work, and how to control the four relays onboard the Arduino 4 Relays Shield

Arduino Turn on / off anything at a specific time (Trigger a Relay with DS3231 RTC)
Arduino Turn on / off anything at a specific time (Trigger a Relay with DS3231 RTC)

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.

Warning: when you are making projects that are connected to mains voltage, you really need to know what you are doing, otherwise you may shock yourself. This is a serious topic, and we want you to be safe. If you’re not 100% sure what you are doing, do yourself a favor and don’t touch anything. Ask someone who knows!

Parts required

Here’s the needed parts for this example:

  • Relay Module
  • Arduino UNO – read Best Arduino Starter Kits
  • PIR Motion Sensor
  • Lamp Cord Set (view on eBay)

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

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 https://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(); }

How the code works

First, we create variables to hold the pin the relay IN1 pin is connected to and to save the relay state:


int relay = 8; volatile byte relayState = LOW;

The PIR motion sensor is connected to pin 2:


int PIRInterrupt = 2;

We need to create some auxiliary variables to handle timers with the PIR motion sensor. The lastDebounceTime variable saves the last time motion was detected. The debounceDelay saves how much time the lamp should remain on after motion is detected (here we’re setting 10 seconds = 10000 milliseconds)


long lastDebounceTime = 0; long debounceDelay = 10000;

In the setup(), we set the relay as an OUTPUT and turn it off by default:


pinMode(relay, OUTPUT); digitalWrite(relay, HIGH);

Because we’re using a normally open configuration, there is no contact between the COM and NO sockets unless you trigger the relay. The relay is triggered when the input goes below about 2 V. That means if you send a LOW signal from the Arduino, the relay turns on, and if you send a HIGH signal, the relay turns off; it works with inverted logic.

Set the PIR motion sensor as an interrupt:


pinMode(PIRInterrupt, INPUT); // Triggers detectMotion function on rising mode to turn the relay on, if the condition is met attachInterrupt(digitalPinToInterrupt(PIRInterrupt), detectMotion, RISING);

Whenever the PIR motion sensor is triggered, it calls the detectMotion() function declared at the end of the code to turn the relay on:


void detectMotion() { Serial.println("Motion"); if(relayState == LOW){ digitalWrite(relay, LOW); } relayState = HIGH; Serial.println("ON"); lastDebounceTime = millis(); }

In the loop(), we check whether 10 seconds have passed since the relay is on. If that condition is true, we can turn the relay off.


if((millis() - lastDebounceTime) > debounceDelay && relayState == HIGH){ digitalWrite(relay, HIGH); relayState = LOW; Serial.println("OFF"); }

Schematic

Assemble all the parts as shown in the schematic diagram.

Warning: do not touch any wires that are connected to mains voltage. Also make sure you have tighten all screws of the relay module.

The lamp is connected to the relay using a normally open configuration. The Arduino controls the relay through pin 8 (pin 8 is connected to the relay IN1 pin). Finally, the PIR motion sensor is connected to pin 2.

Sơ đồ chân Module Relay 2 kênh

Chân điều khiển (Control Pins)

Chân điều khiển của module Relay 2 kênh là các chân đầu vào được sử dụng để điều khiển hoạt động BẬT/TẮT của relay.

  • IN1: Chân điều khiển kênh 1 của relay.
  • IN2: Chân điều khiển kênh 2 của relay.

Chân nguồn (Power Supply Selection Pins)

  • VCC: Chân nguồn của module Relay.
  • GND: Chân nối đất (GND) của module Relay.
  • JD-VCC: Chân nguồn (VCC) cho các relay.

Đầu ra (Output Terminals)

  • COM (Chân chung): Chân nối chung của các relay.
  • NO (Thường mở): Chân thường mở của relay.
  • NC (Thường đóng): Chân thường đóng của relay.
[DIY] IR Sensor 5v Relay Module And Submersible Water Pump Wire connect || Indian Jogaru Expriment
[DIY] IR Sensor 5v Relay Module And Submersible Water Pump Wire connect || Indian Jogaru Expriment

This article shows how to control mains voltage with the Arduino using a relay module. We make a brief introduction to the relay module and build a simple project example with the Arduino. The example we’ll build shows how to control a relay module with an Arduino and a PIR motion sensor.

By the end of this tutorial, you should be able to control any electronics appliances with your Arduino using a relay module.

Sơ đồ đấu nối Module Relay 2 kênh với Arduino

Như vậy là bạn đã tìm hiểu qua về Relay Arduino, bây giờ mình sẽ hướng dẫn các bạn đấu nối mạch để điều khiển một bóng đèn. Lưu ý, trong dự án này sử dụng điện áp cao 220VAC, nên các bạn cẩn thận, tránh trường hợp gây cháy nổ không đáng có.

Trong trường hợp đầu tiên, mình sẽ dùng trực tiếp nguồn 5V từ Arduino Uno nên các bạn cần cắm Jumper vào vị trí JD-VCC và VCC

Ở phần đấu thiết bị, mình sử dụng hai tiếp điểm COM và NO, có nghĩa là khi nguồn khởi động trạng thái của bóng đèn sẽ luôn TẮT, nếu các bạn muốn sáng liên tục thì sử dụng tiếp điểm thường đóng (NC) nhé.

Trong trường hợp thứ hai, mình sẽ sử dụng nguồn riêng để cấp cho Relay, nên sẽ tháo Jumper ra và cấp nguồn 5V vào hai chân JD-VCC và GND.

Cách đấu bóng đèn thì tương tự như ở trên nhé.

Linh kiện cần thiết cho dự án

TÊN LINH KIỆN SỐ LƯỢNG NƠI BÁN
Arduino Uno R3 Shopee | Cytron
Module Relay 2 kênh Shopee | Cytron
Dây cắm 10-20 Shopee | Cytron
DIY Smart Switch - Part 1 how to use a relay
DIY Smart Switch – Part 1 how to use a relay

One-Channel Relay Module Pinout

Let’s take a look at the pinout.

Power Pins:

GND is the common ground pin.

VCC pin provides power to the module.

Control Pin:

IN pin is used to control the relay. This is an active low pin, which means that pulling it LOW activates the relay and pulling it HIGH deactivates it.

Output Terminals:

COM terminal connects to the device you intend to control.

NC terminal is normally connected to the COM terminal, unless you activate the relay, which breaks the connection.

NO terminal is normally open, unless you activate the relay that connects it to the COM terminal.

Arduino Example Code

Controlling a relay module with the Arduino is as easy as controlling an LED. Here’s a simple code that will activate the relay for 3 seconds and then deactivate it for 3 seconds.


int RelayPin = 6; void setup() { // Set RelayPin as an output pin pinMode(RelayPin, OUTPUT); } void loop() { // Let's turn on the relay... digitalWrite(RelayPin, LOW); delay(3000); // Let's turn off the relay... digitalWrite(RelayPin, HIGH); delay(3000); }

Code Explanation:

The sketch begins by declaring the pin to which the relay module’s input pin is connected.


int RelayPin = 6;

In the setup function, we configure the input pin to behave as an output.


pinMode(RelayPin, OUTPUT);

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


digitalWrite(RelayPin, LOW)

pulls the pin LOW, whereas

digitalWrite(RelayPin, HIGH)

pulls the pin HIGH.


digitalWrite(RelayPin, LOW); delay(3000); digitalWrite(RelayPin, HIGH); delay(3000);

Arduino Relay Tutorial: Control High Voltage Devices with Relay Modules

Want to control high voltage devices like your fans, lights, heaters, or other household appliances with the Arduino?

Unfortunately, you can’t as the Arduino operates at 5V which means it can’t control these higher voltage devices directly. However, with the help of a relay module to switch the high voltage, you can now use the Arduino to control those devices!

Interested? Learn how to do it through this tutorial! This Arduino relay tutorial will cover:

  • What is a Relay module?
  • How does the Relay works?
  • Types of Relay modules here at Seeed
  • Step by step instructions on how to use the relay with Arduino

Without further ado, let us jump right into the first point of this guide:

How to make a Clap Switch using Arduino UNO!
How to make a Clap Switch using Arduino UNO!

Relay hoạt động động như thế nào?

Thông tin cơ bản về Relay

Relay là một thiết bị điện tử dùng để điều khiển mạch điện bằng cách sử dụng một tín hiệu điều khiển nhỏ. Nó hoạt động dựa trên nguyên lý của cuộn dây điện dẫn điện và cơ cấu chuyển mạch.

Relay có hai trạng thái chính: trạng thái thường đóng (NC) và trạng thái thường mở (NO). Khi một tín hiệu điều khiển được đưa vào cuộn dây điện, relay sẽ chuyển từ trạng thái thường mở (NO) sang trạng thái thường đóng (NC) giúp cho thiết bị được hoạt động.

Nguyên lý hoạt động của Relay Arduino

Khi một tín hiệu điều khiển được cấp vào cuộn dây điện, dòng điện chạy qua cuộn dây sẽ tạo ra một trường từ xung quanh cuộn dây. Trường từ này sẽ tác động lên cơ cấu chuyển mạch trong relay, làm cho nó chuyển từ trạng thái thường mở (NO) sang trạng thái thường đóng (NC).

Khi rơ le ở trạng thái thường mở (NO) thì các tiếp điểm trong mạch relay không được kết nối với nhau. Khi tín hiệu điều khiển được cấp, relay chuyển sang trạng thái thường đóng (NC).

Tổng quan về Module Relay 2 Kênh

Module Relay 2 kênh là một module điện tử được sử dụng để điều khiển các thiết bị hoặc mạch điện thông qua tín hiệu điều khiển từ một nguồn bên ngoài như Arduino, Raspberry Pi… Nó cung cấp hai kênh relay độc lập, cho phép người dùng điều khiển hai thiết bị riêng biệt.

Nó bao gồm 2 Relay, mỗi Relay có dòng điện là 10A và hoạt động ở điện áp 250VAC hoặc 30VDC.

Chân kết nối thiết bị đầu ra (Output Terminal Blocks)

Module Relay 2 kênh sẽ có hai cặp Terminal Blocks, mỗi kênh bao gồm 3 chân: COM (Chân chung), NO (Thường mở) và chân NC (Thường đóng).

Module điều khiển (Module Control)

Module điều khiển của Relay 2 kênh bao gồm hai chân IN1, IN2 dùng để điều khiển Relay.

Để kích hoạt relay, các bạn cần đưa vào một tín hiệu điều khiển tương ứng cho chân điều khiển. Nó có thể là một tín hiệu logic từ Arduino.

Các chân điều khiển hoạt động ở mức logic THẤP sẽ kích hoạt Relay và ở mức logic CAO sẽ tắt relay.

Module relay 2 kênh có hai đèn LED dùng để báo trạng thái. Khi relay được kích hoạt, đèn LED sẽ sáng lên.

Opto cách ly quang (Built-in Optocouplers)

Opto cách ly quang là một linh kiện được sử dụng để cách ly điện giữa các mạch điều khiển và mạch công suất của relay.

Trên module Relay, mỗi kênh đều được tích hợp một Opto quang, giúp cách ly tín hiệu điều khiển từ mạch điều khiển Arduino Uno với mạch công suất. Điều này cho phép điều khiển relay một cách an toàn và cách ly tuyệt đối, tránh sự tác động của nhiễu điện từ và bảo vệ mạch điều khiển khỏi nguy cơ hư hỏng.

Chân nối nguồn điện (Power Supply Selection Jumper)

Chân nối nguồn điện là một tính năng có sẵn trên một số module Relay 2 kênh. Điều này cho phép người dùng lựa chọn nguồn cấp phù hợp cho module relay.

Khi Jumper được cắm giữa 2 tiếp điểm JD-VCC và VCC, thì relay arduino sẽ sử dụng nguồn chung với Arduino. Rủi ro trong trường hợp này là nguồn điện không được cách ly, nhưng bù lại nó dễ sử dụng hơn

Trong trường hợp Jumper bị tháo ra, bạn cần phải cấp nguồn riêng biệt vào hai chân JD-VCC và GND để cho Relay hoạt động.

Module nguồn (Module Power)

Module relay 2 kênh hoạt động ở điện áp 5V và khoảng 140mA, khi hai rơ le cùng được kích hoạt thì dòng điện trên mỗi cái là 70mA.

Module bao gồm các Diode flyback được kết nối song song với các cuộn dây để ngắt dòng điện một cách an toàn khi cuộn dây relay mất điện.

Arduino Multiplexor - MUX INPUTS & OUTPUTS - ANALOG and PWM
Arduino Multiplexor – MUX INPUTS & OUTPUTS – ANALOG and PWM

Code ví dụ

int RelayPin = 6; void setup() { // Set RelayPin as an output pin pinMode(RelayPin, OUTPUT); } void loop() { // Let’s turn on the relay… digitalWrite(RelayPin, LOW); delay(3000); // Let’s turn off the relay… digitalWrite(RelayPin, HIGH); delay(3000); }

Giải thích Code

int RelayPin = 6;

Khai báo biến RelayPin và gán giá trị là 6, là chân kết nối của relay với Arduino.

void setup() { // Set RelayPin as an output pin pinMode(RelayPin, OUTPUT); }

Trong hàm setup(), khai báo chân RelayPin là một chân đầu ra (OUTPUT). Điều này cho phép Arduino gửi tín hiệu điều khiển đến relay thông qua chân này.

void loop() { // Let’s turn on the relay… digitalWrite(RelayPin, LOW); delay(3000); // Let’s turn off the relay… digitalWrite(RelayPin, HIGH); delay(3000); }


digitalWrite(RelayPin, LOW);

được sử dụng để kích hoạt relay. Bằng cách đặt chân RelayPin xuống mức thấp (LOW), tín hiệu điều khiển được gửi đến relay để đóng mạch điện. Làm cho relay hoạt động.


delay(3000);

giữ relay ở trạng thái đó trong 3 giây trước khi thực hiện thao tác tiếp theo.


digitalWrite(RelayPin, HIGH);

được sử dụng để tắt relay. Bằng cách đặt chân RelayPin lên mức cao (HIGH), tín hiệu điều khiển được gửi đến relay để ngắt mạch điện. Làm cho relay dừng hoạt động.


delay(3000);

giữ relay ở trạng thái tắt trong 3 giây trước khi lặp lại quá trình từ đầu.

Introducing the Relay Module

A relay is an electrically operated switch that can be turned on or off, letting the current go through or not, and can be controlled with low voltages, like the 5V provided by the Arduino pins.

Controlling a relay module with the Arduino is as simple as controlling any other output as we’ll see later on.

This relay module has two channels (those blue cubes). There are other models with one, four and eight channels. This module should be powered with 5V, which is appropriate to use with an Arduino. There are other relay modules that are powered using 3.3V, which is ideal for ESP32, ESP8266, and other microcontrollers.

Get a relay module:

  • 5V 2-channel relay module
  • 5V 1-channel relay module
  • 5V 8-channel relay module
  • 3.3V 1-channel relay module
Arduino Basics - Relays
Arduino Basics – Relays

How Do Relays Work?

At the core of a relay is an electromagnet (a wire coil that becomes a temporary magnet when electricity is passed through it). A relay can be thought of as an electric lever; you turn it on with a relatively small current, and it turns on another device with a much larger current.

Relay Basics

Here’s a small animation showing how a relay links two circuits together.

To illustrate, think about two simple circuits: one with an electromagnet and a switch or sensor, and the other with a magnetic switch and a light bulb.

Initially, both circuits are open, with no current flowing through them.

When a small current flows through the first circuit, the electromagnet is energized, creating a magnetic field around it. The energized electromagnet attracts the second circuit’s contact, closing the switch and allowing a large current to flow.

When the current in the first circuit stops flowing, the contact returns to its original position, reopening the second circuit.

Relay Operation

A relay typically has five pins, three of which are high voltage terminals (NC, COM, and NO) that connect to the device being controlled.

The device is connected between the COM (common) terminal and either the NC (normally closed) or NO (normally open) terminal, depending on whether the device should remain normally on or off.

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

Normally (initial position), the COM terminal is connected to the NC terminal and the NO terminal is open.

When current flows through the coil, the electromagnet becomes energized, causing the switch’s internal contact to move. The COM then connects to the NO terminal, disconnecting from the NC terminal.

When the current stops flowing through the coil, the internal contact is returned to its initial position, re-connecting the NC terminal to the COM and re-opening the NO terminal.

To put it another way, the relay functions as a single-pole-double-throw switch (SPDT).

Keywords searched by users: arduino uno relay module

In-Depth: Interface One Channel Relay Module With Arduino
In-Depth: Interface One Channel Relay Module With Arduino
In-Depth: Interface One Channel Relay Module With Arduino
In-Depth: Interface One Channel Relay Module With Arduino
Guide For Relay Module With Arduino | Random Nerd Tutorials
Guide For Relay Module With Arduino | Random Nerd Tutorials
How To Use A Relay Module With Arduino – Surtr Technology
How To Use A Relay Module With Arduino – Surtr Technology
Arduino Relay Module Interface | Circuits4You.Com
Arduino Relay Module Interface | Circuits4You.Com
Basic Setup For Arduino With Relay - Hackster.Io
Basic Setup For Arduino With Relay – Hackster.Io
How To Use 5V Relay Module | Arduino Uno With 5V Relay Module [Code And  Circuit Diagram] - Youtube
How To Use 5V Relay Module | Arduino Uno With 5V Relay Module [Code And Circuit Diagram] – Youtube
Arduino 4 Relays Shield — Arduino Online Shop
Arduino 4 Relays Shield — Arduino Online Shop
Relay Module - General Electronics - Arduino Forum
Relay Module – General Electronics – Arduino Forum
1 Channel Realy Module With Arduino - Youtube
1 Channel Realy Module With Arduino – Youtube
Difference Between Using Relay Alone Or Relay Module - General Electronics  - Arduino Forum
Difference Between Using Relay Alone Or Relay Module – General Electronics – Arduino Forum
2 Channel Relay Not Working No Matter What I Do! - General Electronics -  Arduino Forum
2 Channel Relay Not Working No Matter What I Do! – General Electronics – Arduino Forum
In-Depth: Interface Two Channel Relay Module With Arduino
In-Depth: Interface Two Channel Relay Module With Arduino
B4R - Testing An 8 Channel Relay Module On Arduino Uno. - Youtube
B4R – Testing An 8 Channel Relay Module On Arduino Uno. – Youtube

See more here: kientrucannam.vn

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *