Chuyển tới nội dung
Home » Arduino On Off Relay | Mains Voltage Connections

Arduino On Off Relay | Mains Voltage Connections

Using 5V 1 channel relay module for Arduino

A Temperature Controlled Relay Circuit

To show you how to wire the relay, let’s build a temperature controlled relay circuit that will turn off a light bulb when the temperature of a thermistor reaches 150°F. Thermistors are really useful with 5V relays. You can use them to turn off a large motor if gets too hot or turn on a heater if the temperature gets too cold.

WARNING – THIS PROJECT INVOLVES 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.

The setup is fairly simple, just make sure that the high voltage connections to the relay are secure:

Identify the hot power wire (red wire in the diagram above) in the cord leading to the light bulb and make a cut. Connect the side leading to the light bulb to the NO terminal of the relay, and the side leading to the plug to the C terminal. This way the relay is on the hot side, and current is switched before it reaches the light bulb. It’s dangerous to put the relay on the neutral wire, since if the device fails current can still fault to ground when the relay is off.

The thermistor part of the circuit is set up as a voltage divider. The value of the resistor should be the same order of magnitude as the thermistor. For example, I’m using a 10K Ω thermistor, so the resistor should be 10K Ω as well. If you use a 100K Ω thermistor, use a 100K Ω resistor.

If you do use a 100K Ω thermistor, you’ll need to change line 7 in the code below to

Temp = log(100000.0*((1024.0/RawADC-1)));

. See our article on Making an Arduino Temperature Sensor for more information.

The Code

After everything is connected, upload this code to the Arduino:


#include

int pinOut = 10; double Thermistor(int RawADC) { double Temp; Temp = log(10000.0*((1024.0/RawADC-1))); Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp ); Temp = Temp - 273.15; Temp = (Temp * 9.0)/ 5.0 + 32.0; return Temp; } void setup() { Serial.begin(9600); pinMode(10, OUTPUT); } void loop() { int val; double temp; val=analogRead(0); temp=Thermistor(val); Serial.print("Temperature = "); Serial.print(temp); Serial.println(" F"); if (temp >= 150){ digitalWrite(pinOut, LOW); } else { digitalWrite(pinOut, HIGH); } delay(500); }

In this example, the relay will stay activated and let current flow through the light bulb until the temperature of the thermistor reaches 150°F. At 150°F the relay shuts off and the current stops. You can change the temperature in line 27 where it says

if (temp >= 150){

.

Here’s a video you can watch of me wiring up the relay to see it in action:

Another really useful project is a relay controlled power outlet box. This will let you plug any appliance into the outlet and control it with your Arduino without cutting into any power cords. You can also control several devices at the same time. See our tutorial “Turn Any Appliance into a Smart Device with an Arduino Controlled Power Outlet” to see how we built it.

Hope this is useful for you! Leave a comment if you have any questions. If you want us to let you know when we publish more tutorials, be sure to subscribe and share it if you know someone that would find it helpful. Thanks!

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.

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

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.

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.

Arduino Push Button Relay Control
Arduino Push Button Relay Control

Comments

how to code?

Hi,in aurduino can we used python language to code?if possible can u send me the tutorial, to code and how to mention the pin numbers??please do reply..thankyou

I want to use a uln

hi i want to light up a 12 volt LED strip and need to use a uln 2003 a and a 6volt relay and a capacitive sensor to control the light. i.e. when i once touch it it switches on, and when i touch it again it switches off. along with arduino uno. Now i am getting confused as in how shall i used= it. could you be able to help me with this. as i need to add several more componenets in future in order to learn more.

hi all of electrical engineer

how to get ir sensor on protues software?please give your hand!

relay module cod

dear, friend i need a relay module cod please help me!!!

What do you mean by relay

What do you mean by relay module code? IF you wan to know how to control a relay with Arduino, follow the below tutorial

https://circuitdigest.com/microcontroller-projects/arduino-relay-control

Use an Arduino to Control a Relay

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!

Arduino | Hướng dẫn chi tiết cách sử dụng Relay + Arduino
Arduino | Hướng dẫn chi tiết cách sử dụng Relay + Arduino

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.

Bài viết liên quan

  • Mới học Arduino nên chọn board nào?
  • GÓC DIY | Chế tạo Robot tránh vật cản – Hướng dẫn chi tiết
  • Điều khiển Đèn 220V bằng Realy sử dụng Arduino
  • Đo nhiệt độ bằng Cảm biến LM35 sử dụng Arduino
  • Báo động chống trộm bằng cảm biến PIR (HC-SR501)

Chúc các bạn thành công!

Trân trọng.

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

What is Relay? | How to control Relay using Arduino
What is Relay? | How to control Relay using 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.

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
How to use 5V Relay with Arduino to turn ON and OFF AC  bulb or DC load
How to use 5V Relay with Arduino to turn ON and OFF AC bulb or DC load

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}

Code

Đoạn code khá đơn giản ở đây mình sử dụng chân D8 để làm chân điều khiển. Khi bắt đầu chương trình đèn sẽ sáng trong 3 giây, sau 3 giây bóng đèn sẽ tắt .

int Relay = 8; void setup() { pinMode(Relay, OUTPUT); digitalWrite(Relay, HIGH); } void loop() { digitalWrite(Relay, LOW); delay(3000); digitalWrite(Relay, HIGH); delay(3000); }

Giải thích Code

Đoạn code trên dùng để điều khiển một module relay (rơle) kết nối với chân số 8 của board Arduino. Khi chương trình được chạy, ở hàm

setup()

, chân số 8 được cấu hình là đầu ra với lệnh

pinMode(Relay, OUTPUT);

, và sau đó được kích hoạt bằng lệnh

digitalWrite(Relay, HIGH);

, giúp đảm bảo rơle khởi động ở trạng thái ban đầu.

Ở hàm

loop()

, rơle được điều khiển để chuyển đổi trạng thái bằng cách đặt đầu ra của chân số 8 ở mức thấp với lệnh

digitalWrite(Relay, LOW);

, sau đó đợi trong 3 giây bằng lệnh

delay(3000);

. Sau khi chờ 3 giây, chân số 8 được đặt ở mức cao bằng lệnh

digitalWrite(Relay, HIGH);

, rơle sẽ được kích hoạt và giữ ở trạng thái này trong 3 giây nữa. Điều này tạo ra một chu trình chuyển đổi trạng thái của rơle sau mỗi 6 giây.

DC 5V 12V 24V Digital LED Display Infinite Cycle Delay Timer Switch ON/OFF Relay Module (DC 5V)
DC 5V 12V 24V Digital LED Display Infinite Cycle Delay Timer Switch ON/OFF Relay Module (DC 5V)

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.

Use an Arduino to Control a Relay

Kết luận

Tóm lại, việc sử dụng Arduino để điều khiển đèn 220V bằng rơ le là một ứng dụng thực tế và hữu ích trong các hệ thống điện tử. Điều này giúp giảm thiểu việc tiếp xúc trực tiếp với điện áp cao và tăng cường an toàn cho người sử dụng. Bên cạnh đó, việc sử dụng rơ le cũng giúp đảm bảo việc điều khiển đèn 220V được ổn định và chính xác hơn. Tuy nhiên, để thực hiện được ứng dụng này, cần phải có kiến thức và kỹ năng về lập trình và điện tử, cũng như đảm bảo an toàn và tuân thủ các quy định về điện áp.

LoRa Relay Control With Arduino Upto 15KM
LoRa Relay Control With Arduino Upto 15KM

Learn to turn high power devices on and off

Written By: Marcus Schappi

Difficulty

Easy

Steps

10

A relay is a device that is activated by a current in one circuit to open or close another circuit.

In this guide, we will learn to use a relay to turn an LED on and off. In this guide, a Little Bird Uno R3 board, the 5V 1-Channel relay board module is used. Please see parts list for a complete list of components required.

After learning to use the 5V 1-Channel relay component, you can use it to turn off lamps, fans, and other appliances up to 250 VAC.

Insert the LED into the bread board so that the Cathode (shorter pin) is on the left hand side.

Insert a 220 Ohm Relay so that one pin is in line with the LED’s Anode (longer leg)

The 220 Ohm Relay has the bands: Red, Red, Black, Black, Brown.

Connect the Cathode to a Ground pin on the Arduino.

The Cathode is the negative (shorter) leg of the LED.

Connect the 3.3V Arduino Pin to the Relay.

The 3.3V jumper should go to the NC side of the screw socket.

Connect COM on the relay to the other side of the resistor.

int relay = 13; // Plug the relay into Digital Pin 13 void setup() { pinMode(relay, OUTPUT); } void loop() { digitalWrite(relay, HIGH); // Turn the relay on delay(1000); // Wait 1 second digitalWrite(relay, LOW); // Turn the relay Off delay(1000); // Wait 1 second }

Grab the code and upload it to your board.

You should start to hear the click, click of the relay switching on and off. There is also a red light on the module to tell you when it switches.

The Relay can be use to control up to 250VAC but we DO NOT recommend you play with this sort of voltage / current unless you are an electrician!

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.

Learn to turn high power devices on and off

Written By: Marcus Schappi

Difficulty

Easy

Steps

10

A relay is a device that is activated by a current in one circuit to open or close another circuit.

In this guide, we will learn to use a relay to turn an LED on and off. In this guide, a Little Bird Uno R3 board, the 5V 1-Channel relay board module is used. Please see parts list for a complete list of components required.

After learning to use the 5V 1-Channel relay component, you can use it to turn off lamps, fans, and other appliances up to 250 VAC.

Insert the LED into the bread board so that the Cathode (shorter pin) is on the left hand side.

Insert a 220 Ohm Relay so that one pin is in line with the LED’s Anode (longer leg)

The 220 Ohm Relay has the bands: Red, Red, Black, Black, Brown.

Connect the Cathode to a Ground pin on the Arduino.

The Cathode is the negative (shorter) leg of the LED.

Connect the 3.3V Arduino Pin to the Relay.

The 3.3V jumper should go to the NC side of the screw socket.

Connect COM on the relay to the other side of the resistor.

int relay = 13; // Plug the relay into Digital Pin 13 void setup() { pinMode(relay, OUTPUT); } void loop() { digitalWrite(relay, HIGH); // Turn the relay on delay(1000); // Wait 1 second digitalWrite(relay, LOW); // Turn the relay Off delay(1000); // Wait 1 second }

Grab the code and upload it to your board.

You should start to hear the click, click of the relay switching on and off. There is also a red light on the module to tell you when it switches.

The Relay can be use to control up to 250VAC but we DO NOT recommend you play with this sort of voltage / current unless you are an electrician!

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:

Relay Holding Circuit | Relay Latching Circuit Wiring | How to hold relay using push button switch
Relay Holding Circuit | Relay Latching Circuit Wiring | How to hold relay using push button switch

How the 5V Relay Works

The SRD-05VDC-SL-C relay has three high voltage terminals (NC, C, and NO) which connect to the device you want to control. The other side has three low voltage pins (Ground, Vcc, and Signal) which connect to the Arduino.

  • NC: Normally closed 120-240V terminal
  • NO: Normally open 120-240V terminal
  • C: Common terminal
  • Ground: Connects to the ground pin on the Arduino
  • 5V Vcc: Connects the Arduino’s 5V pin
  • Signal: Carries the trigger signal from the Arduino that activates the relay

Inside the relay is a 120-240V switch that’s connected to an electromagnet. When the relay receives a HIGH signal at the signal pin, the electromagnet becomes charged and moves the contacts of the switch open or closed.

Normally OpenNormally Closed

The relay has two different types of electrical contacts inside – normally open (NO) and normally closed (NC). The one you use will depend on whether you want the 5V signal to turn the switch on or turn the switch off. The 120-240V supply current enters the relay at the common (C) terminal in both configurations. To use the normally open contacts, use the NO terminal. To use the normally closed contacts, use the NC terminal.

Normally Open

In the normally open configuration, when the relay receives a HIGH signal the 120-240V switch closes and allows current to flow from the C terminal to the NO terminal. A LOW signal deactivates the relay and stops the current. So if you want the HIGH signal to turn ON the relay, use the normally open terminal:

Normally Closed

In the normally closed configuration, a HIGH signal opens the switch and interrupts the 120-240V current. A LOW signal closes the switch and allows current to flow from the C terminal to the NC terminal. Therefore, if you want the HIGH signal to turn OFF the 120-240V current, use the normally closed terminal:

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 really gets is power from Arduino +5V and GND.
The solenoid + is connected to the Relay COM pin while the Normally Open pin is connected to the external supply + wire. The solenoid GND is connected to the external power supply GND.
a control signal of High and LOW is send from Arduino pin 2 to connect and disconnect the relay COM pin and the solenoid (which is connected to the relay Normally Open pin)

I was wandering for two things:

I should not in any case connect the external power supply GND and arduino GND together right?

my setup right now is that the relay is making a connection between the external power supply + and the solenoid + , while the power supply GND and the solenoid GND are tied together.
What difference will it make to do the opposite? Tiding the power supply + to the solenoid + while using the relay to switch the power supply GND and the solenoid GND ?

Any other comments on my schematics I share? is it fine as it is?
Thanks.

That is a mistake. A 12V relay board would have been a better choice. That way, the relay coils would be powered directly by the 12V PSU. With a 5V relay module, the Arduino’s onboard regulator must supply power to the relay coils, and the onboard regulator can easily overheat, and it’s voltage can drop, causing the Arduino to reset or be damaged. And that’s just with one relay. With 4 relays activated at the same time…

Better even than 12V relays would be to use (logic-level, n-channel) MOSFETs such as stp16nf06l or irlz44.

So I will need three different power supply? 1 – to power the arduino (usually a computer) 2 – to power the relay module (VCC and GND) and 3 – to power the load (solenoid) via the relay COM and N/O pin ?

It depends on if the moving part is magnetised or not. Normally they are not so you just get an electro magnet being made which pulls the plunger into the field. When the current is removed the spring pulls the plunger out. So normally no polarity.

No, only one. The 12V supply would power the solenoids, relay coils and potentially the Arduino. If you want to continue to power the Arduino with a separate 5V PSU, for some reason, you can do that. But provided the Arduino is powering only itself and maybe a small number of low power components, the Arduino’s built-in regulator should be able to do that without overheating while powered by the 12V PSU.

However, if you continue to use 5V relays, you will definitely need a second, 5V PSU. The Arduino’s built in regulator will not be able to provide enough current for 4 relay coils from a 12V supply without overheating. Even one relay coil might be too much.

Yes, that’s very similar to one of my suggestions.

hello hk_jk
I have seen that discussion about relays and the relay Board.
my first question about that relay Board is, if the board has any logic on itself to sort out that four relays and define which relay has to be activated or are all control lines connected together and all relays are activated at the same time.
This ist important because the power consumtion of the relay coil is about 72mA of current . That is alredy a lot of current because the maximum is about 100mA on all digital outputs together, due tto the data sheets I found.
To reduce the needed power on the outputs and with that action reduce the heat on the processor chip istself you should think about an optical cupler that is able to switch a hiher amount of current and normally has a very low need of current to control the input side (5mA to 15 mA) Then you have eliminated the problem with the ground too.
The second option would be a sort of driver that is controled by nearly only the Voltage at the input and will consume less than 10 mA at the input side.
Always keep an attention on the power used at the outputs of the chip itself. As more power as you take in total ase more heat is produced in that chip and that will result in the end to an overheat situation that blows the chip.

Each relay will be activated at a different time (overlap will accrue) triggered by individual GPIO pin

Any schematics you can point me too?

I guess that the current need to drive a relay is not the same as needed to open a MOSFET/BJT for switching application. This may be the reason I got all confused. What specify the amount of current needs to activate a relay?

I have just found that card and I have to do some corrections to my forst entry.
it is correct, that the relay itself consumes that 70 mA of power but this is driven by an amplifier alreday and separated by an otical coupler. that means the power to drive the relays is taken from the external power supply. in that case you can use the unit with an external power supply. you can use the power of this power supply to put it to the arduino too if you ise the +Vin input.
I found this card as a 5V version, and on your photo I saw that the relays are 5V types . make shure that the poard is made for 12 V DC .

How do I connect a C7-A20X Relay 24 DC V (datasheet: https://www.releco.de/es/Reles-QRC/C7-A2x_es.pdf) to arduino?
I don’t know how to connect it because it is not similar to other relays that I’ve seen in other topics, as they have a IN pin which connects to any digital PIN on ARDUINO. I reefer to this type of relay:

Do not connect a relays directly to your arduino. Even if it has a 5V coil (which your relais does not have), don’t do it. It might not kill your Arduino instantly but it will eventually damage it.

Arduinos have 5V outputs (only Realys with a 5V coil voltage will work)

Arduino can drive up to 40mA on a single pin (but that’s really the maximum. If your relay needs more than 40mA it will damage your arduino)

Relay are an inductive load, if you connect that without the right protection circuit to your arduino it will generate a high voltage on swithcing off (the protection curtcuitry in your arduino will probably protect it from immediately being destroyed but it’s not made to withstand that kind of abuse for prolonged periods of time)

You might want to use a product like the TPL7407A or anything similar to drive your relay. This is a 7 channel triver with most of the protection circuitry already included (I’m still using a freewheeling diode tough, but I’m paranoid). See it’s datasheet here: https://www.ti.com/product/TPL7407LA

Don’t be discouraged by this response please. You’ll be getting there, I’m sure!

Hi, thanks for your answer, very useful, but the think is that I’m not really using an arduino board, I’m using CONTROLLINO MEGA, which works at 24 VDC (datasheet: https://www.controllino.com/wp-content/uploads/2019/02/CONTROLLINO-Instruction-Manual-V1.5-2018-12-14.pdf) (I said ARDUINO ’cause sometimes it’s easy for people to give answers when the read ARDUINO instead of CONTROLLINO). Both CONTROLLINO and my relay work at 24V, so I guess this is OK (isn’t it?). Do you think I still have to use a transistor like the one you suggested me?

Okay, I read the manual. CONTROLLINO has got relay outputs, so I guess I can use them, instead of using my external relay, right?
The manual says:
” The maximum permissible switching current per relay is 6A (at a maximum of 30V / DC). The relay outputs are potential free!”
What does that last sentence mean? If I use the relay outputs (I want the relay to control an electrovalve), How should I do the connection??

ps.: I have a 24VDC power supply for CONTROLLINO and the electrovalve which also works at 24 VDC

Looks like a quite cool and capable little device.

Sadly, neither the manual nor the datasheet mentions if the Digital Outputs are made to drive relais directly. I’d use at least a freewheeling diode parallel to the relais (look it up on how to install it) to protect the outputs.

It says that you can use two halfe bridge outputs to drive a motor in both directions. This could imply that there is some kind of protection against back EMF already built in. Maybe contact support and ask.

“The relay output are potential free” means, that they are galvanically isolated from the rest of the device. So to use them you have to connect both sides of the contact (like a light switch). The upside is, that this gives you a lot of freedom on what you want to drive with that output.

You should be able to easily control the valve from either the halfe bridge or the relais outputs (for the halfe bridge output check with the supplier first and post your findings here)

Arduino – Button Toggle Relay

In a previous tutorial, We have learned how to turn on the relay if the button is pressed, and turn off relay if the button is released. In this tutorial, We are going to learn how to toggle relay each time button is pressed.

By connecting relay to light bulb, led strip, motor or actuator… We can use the button to toggle light bulb, led strip, motor or actuator…

The tutorial includes two main parts:

Or you can buy the following sensor kit:

Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

If you do not know about relay and button (pinout, how it works, how to program …), learn about them in the following tutorials:

This image is created using Fritzing. Click to enlarge image

const int BUTTON_PIN = 7;
const int RELAY_PIN = 3;
int relayState = LOW;
int lastButtonState;
int currentButtonState;
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(RELAY_PIN, OUTPUT);
currentButtonState = digitalRead(BUTTON_PIN);
}
void loop() {
lastButtonState = currentButtonState;
currentButtonState = digitalRead(BUTTON_PIN);
if(lastButtonState == HIGH && currentButtonState == LOW) {
Serial.println(“The button is pressed”);
relayState = !relayState;
digitalWrite(RELAY_PIN, relayState);
}
}

  • Connect Arduino to PC via USB cable

  • Open Arduino IDE, select the right board and port

  • Copy the above code and open with Arduino IDE

  • Click Upload button on Arduino IDE to upload code to Arduino

  • Keep pressing the button several seconds and then release it.

  • See the change of relay’s state

Read the line-by-line explanation in comment lines of code!

In the code, relayState = !relayState is equivalent to the following code:

if(relayState == LOW)
relayState = HIGH;
else
relayState = LOW;

※ NOTE THAT:

In practice, the above code does not work correctly sometimes. To make it always work correctly, we need to debounce for the button. Debouncing for the button is not easy for beginners. Fortunately, thanks to the ezButton library, We can do it easily.

Why do we need debouncing? ⇒ see Arduino – Button Debounce tutorial

#include

const int BUTTON_PIN = 7;
const int RELAY_PIN = 3;
ezButton button(BUTTON_PIN);
int relayState = LOW;
void setup() {
Serial.begin(9600);
pinMode(RELAY_PIN, OUTPUT);
button.setDebounceTime(50);
}
void loop() {
button.loop();
if(button.isPressed()) {
Serial.println(“The button is pressed”);
relayState = !relayState;
digitalWrite(RELAY_PIN, relayState);
}
}

  • Install ezButton library. See

    How To

  • Copy the above code and open with Arduino IDE

  • Click Upload button on Arduino IDE to upload code to Arduino

  • Press button several times

  • See the change of relay’s state

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

Most electronic products have a reset button. Additionally, the button also keeps other functionalities in many products.

One of the most useful things you can do with an Arduino is control higher voltage (120-240V) devices like fans, lights, heaters, and other household appliances. Since the Arduino operates at 5V it can’t control these higher voltage devices directly, but you can use a 5V relay to switch the 120-240V current and use the Arduino to control the relay.

The Arduino can be programmed to turn on the relay when a certain event occurs, for example when the temperature of a thermistor gets higher than 30°C. Or when the resistance of a photoresistor drops below 400 Ohms. Almost any sensor can be used to trigger the relay to turn on or off. The trigger doesn’t even need to be from a sensor. It can occur at set time intervals, it can be triggered from the press of a button, or even when you get an email.

I’ll be using the SRD-05VDC-SL-C 5V relay in this tutorial because it’s very popular among Arduino and DIY electronics hobbyists. Let’s start with seeing how the 5V relay works, then I’ll show you how to set it up on the Arduino and give you some code to get it working.

Here’s the datasheet:

How to connect DC motor with relay || control with Arduino
How to connect DC motor with relay || control with Arduino

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.

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.

One button Push On Push Off ! The simplest circuit.
One button Push On Push Off ! The simplest circuit.

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.

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

LED Blinking is a very common and almost first program for every embedded learner or beginner. In which we blink an LED with having some delay. So today we are here with the same project but here we will use an AC bulb instead of normal LED and will blink an AC bulb.

Whenever we need to connect any AC Appliance in our embedded circuits, we use a Relay. So in this arduino relay control tutorial we will simply learn How to interface a Relay with Arduino. Here we are not using any Relay Driver IC like ULN2003 and will only use an NPN transistor to control relay.

Components Required:

  1. Arduino
  2. 5v or 6v relay
  3. AC appliance or Bulb
  4. BC547 transistor
  5. 1k resistor
  6. Breadboard or PCB
  7. Connecting jumper wire
  8. Power supply
  9. 1n4007 diode
  10. Screw terminal or terminal block

Relay:

Relay is an electromagnetic switch, which is controlled by small current, and used to switch ON and OFF relatively much larger current. Means by applying small current we can switch ON the relay which allows much larger current to flow. A relay is a good example of controlling the AC (alternate current) devices, using a much smaller DC current. Commonly used Relay is Single Pole Double Throw (SPDT) Relay, it has five terminals as below:

When there is no voltage applied to the coil, COM (common) is connected to NC (normally closed contact). When there is some voltage applied to the coil, the electromagnetic field produced, which attracts the Armature (lever connected to spring), and COM and NO (normally open contact) gets connected, which allow a larger current to flow. Relays are available in many ratings, here we used 6V operating voltage relay, which allows 7A-250VAC current to flow.

The relay is always configured by using a small Driver circuit which consists a Transistor, Diode and a resistor. Transistor is used to amplify the current so that full current (from the DC source – 9v battery) can flow through a coil to fully energies it. The resistor is used to provide biasing to the transistor. And Diode is used to prevent reverse current flow, when the transistor is switched OFF. Every Inductor coil produces equal and opposite EMF when switched OFF suddenly, this may cause permanent damage to components, so Diode must be used to prevent reverse current. A Relay module is easily available in the market with all its Driver circuit on the board or you can create it on perf board or PCB like below. Here we have used 6V Relay module.

Here to turn on the Relay with Arduino we just need to make that Arduino Pin High (A0 in our case) where Relay module is connected. Below given is Relay Driver Circuit to build your own Relay module:

Circuit Diagram and Working:

In this Arduino Relay Control Circuit we have used Arduino to control the relay via a BC547 transistor. We have connected transistor base to Arduino pin A0 through a 1k resistor. An AC bulb is used for demonstration. The 12v adaptor is used for powering the circuit.

Working is simple, we need to make the RELAY Pin (PIN A0) high to make the Relay module ON and make the RELAY pin low to turn off the Relay Module. The AC light will also turn on and off according to Relay.

We just programmed the Arduino to make the Relay Pin (A0) High and Low with a delay of 1 second:

void loop() { digitalWrite(relay, HIGH); delay(interval); digitalWrite(relay, LOW); delay(interval); }

Demonstration Video and complete code for Arduino Relay Control is given below.

#define interval 1000

pinMode(relay, OUTPUT);

digitalWrite(relay, HIGH);

delay(interval);

digitalWrite(relay, LOW);

delay(interval);

Time Delay Relay circuit using 555 timer IC | Off delay timer Switch | UTSOURCE
Time Delay Relay circuit using 555 timer IC | Off delay timer Switch | UTSOURCE

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

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:

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

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).

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

Tên linh kiện Số lượng Shopee
Arduino Uno R3 Mua ngay
Cáp nạp Mua ngay
Relay 5V/1 Kênh Mua ngay
Dây cắm (Đực – Cái) Mua ngay

Bạn sẽ học được gì

  • Có kiến thức cơ bản về Robotics
  • Chế tạo Robot dò đường thông minh
  • Đánh thức nhà khoa học bên trong bạn
  • Tìm hiểu thêm về Robotics, các thuật toán Robot tự động
  • Kiến thức nền tảng để chế tạo các máy móc tự động phục vụ đời sống sinh hoạt, lao động sản xuất
  • Kiến thức để chế tạo sản phẩm, tham gia các cuộc thi khoa học công nghệ trong nước và quốc tế
How to make motion detection light at home|Smart light|Arduino Projects|Automatic night light|
How to make motion detection light at home|Smart light|Arduino Projects|Automatic night light|

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.

Keywords searched by users: arduino on off relay

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 *