Skip to content
Home » Arduino Uno Relay Control | Mains Voltage Connections

Arduino Uno Relay Control | Mains Voltage Connections

What is Relay? | How to control Relay using Arduino

Step 4: Procedure

Connect a 1K resistor (for current limiting when the transistor is energized) to pin 8 of the SunFounder Uno board, then to an NPN transistor whose collector is connected to the coil of a relay and emitter to GND; connect the normally open contact of the relay to an LED and then GND. Therefore, when a High level signal is given to pin 8, the transistor is energized, thus making the coil of the relay conductive. Then its normally open contact is closed, and the LED will light up. When pin 8 is given a Low level, the LED will stay dim.

Function of the freewheeling diode: When the voltage input changes from High (5V) to Low (0V), the transistor changes from saturation (three working conditions: amplification, saturation, and cut-off) to cut-off, the current in the coil suddenly has no way to flow through. At this moment, without the freewheeling diode, a counter-electromotive force (EMF) will be generated at the ends of the coil, with positive at the bottom and negative at the top, a voltage higher than 100V. This voltage plus that from the power at the transistor are big enough to burn it. Therefore, the freewheeling diode is extremely important in discharging this counter-EMF in the direction of the arrow in the figure above, so the voltage of the transistor to GND is no higher than +5V (+0.7V).

In this experiment, when the relay closes, the LED will light up; when the relay opens, the LED will go out.

Step 1:

Build the circuit.

Step 2:

Download the code from https://github.com/primerobotics/Arduino

Step 3:

Upload the sketch to the Arduino Uno board

Click the Upload icon to upload the code to the control board.

If “Done uploading” appears at the bottom of the window, it means the sketch has been successfully uploaded.

Now, send a High level signal, and the relay will close and the LED will light up; send a low one, and it will open and the LED will go out. In addition, you can hear a tick-tock caused by breaking the normally close contact and closing the normally open one.

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}

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

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.

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!

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:

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

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ế

Introduction: Relay With Arduino Uno R3

As we may know, relay is a device which is used to provide connection between two or more points or devices in response to the input signal applied. In other words, relays provide isolation between the controller and the device as devices may work on AC as well as on DC. However, they receive signals from a microcontroller which works on DC hence requiring a relay to bridge the gap. Relay is extremely useful when you need to control a large amount of current or voltage with small electrical signal.

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

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.

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.

Connecting a Relay Module to a Microcontroller
Connecting a Relay Module to a Microcontroller

Step 5: Code

//relay

//Email:[email protected]

//Website:www.primerobotics.in

/******************************************************/

const int relayPin = 8; //the base of the transistor attach to

/******************************************************/

void setup()

pinMode(relayPin, OUTPUT); //initialize the relayPin as an output

/******************************************************/

void loop()

digitalWrite(relayPin, HIGH); //drive relay closure conduction

delay(1000); //wait for a second

digitalWrite(relayPin, LOW); //drive the relay is closed off

delay(1000); //wait for a second

/******************************************************/

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

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

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]

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.

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 - Driving DC Motor (Water Pump) with Relay
Arduino – Driving DC Motor (Water Pump) with Relay

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.

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
Sinhala Arduino Tutorial 10 - Relay | Controll AC 230V current with bluetooth | ගෙදර light on කරන්න
Sinhala Arduino Tutorial 10 – Relay | Controll AC 230V current with bluetooth | ගෙදර light on කරන්න

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

Use an Arduino to Control a Relay

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.

SunFounder Kit Tutorial for Arduino - Relay
SunFounder Kit Tutorial for Arduino – Relay

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

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!

Lì xì đầu năm cho NĐT cầm cổ phiếu
Lì xì đầu năm cho NĐT cầm cổ phiếu

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:

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.

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

Step 2: Principle

There are 5 parts in every relay:

1. Electromagnet – It consists of an iron core wounded by coil of wires. When electricity is passed through, it becomes magnetic. Therefore, it is called electromagnet.

2. Armature – The movable magnetic strip is known as armature. When current flows through them, the coil is it energized thus producing a magnetic field which is used to make or break the normally open (N/O) or normally close (N/C) points. And the armature can be moved with direct current (DC) as well as alternating current (AC).

3. Spring – When no currents flow through the coil on the electromagnet, the spring pulls the armature away so the circuit cannot be completed.

4. Set of electrical contacts – There are two contact points:

. Normally open – connected when the relay is activated, and disconnected when it is inactive.

. Normally close – not connected when the relay is activated, and connected when it is inactive.

5. Molded frame – Relays are covered with plastic for protection.

Working of Relay

The working principle of relay is simple. When power is supplied to the relay, currents start flowing through the control coil; as a result, the electromagnet starts energizing. Then the armature is attracted to the coil, pulling down the moving contact together thus connecting with the normally open contacts. So the circuit with the load is energized. Then breaking the circuit would a similar case, as the moving contact will be pulled up to the normally closed contacts under the force of the spring. In this way, the switching on and off of the relay can control the state of a load circuit.

Transistor

Transistor is a semiconductor device that controls current by current. It functions by amplifying weak signal to larger amplitude signal and is also used for non-contact switch. A transistor is a three-layer structure composed of P-type and N-type semiconductors. They form the three regions internally. The thinner in the middle is the base region; the other two are both N-type or P-type ones – the smaller region with intense majority carriers is the emitter region, when the other one is the collector region. This composition enables the transistor to be an amplifier.

From these three regions, three poles are generated respectively, which are base (b), emitter (e), and collector (c). They form two P-N junctions, namely, the emitter junction and collection junction. The direction of the arrow in the transistor circuit symbol indicates that of the emitter junction. Based on the semiconductor type, transistors can be divided into two groups, the NPN and PNP ones. From the abbreviation, we can tell that the former is made of two N-type semiconductors and one P-type and that the latter is the opposite. See the figure below.

When a High level signal goes through an NPN transistor, it is energized. But a PNP one needs a Low level signal to manage it. Both types of transistor are frequently used for contactless switches, just like in this experiment.

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

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:

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.

Simple on/off Arduino timer (Aeroponics)
Simple on/off Arduino timer (Aeroponics)

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

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.

Keywords searched by users: arduino uno relay control

Điều Khiển Đèn 220V Bằng Rơ Le (Relay) Sử Dụng Arduino | Arduino Kit
Điều Khiển Đèn 220V Bằng Rơ Le (Relay) Sử Dụng Arduino | Arduino Kit
Guide For Relay Module With Arduino | Random Nerd Tutorials
Guide For Relay Module With Arduino | Random Nerd Tutorials
How To Use A Relay With Arduino - Makerguides.Com
How To Use A Relay With Arduino – Makerguides.Com
Interfacing Relay With Arduino Uno
Interfacing Relay With Arduino Uno
Relay With Arduino Uno R3 : 7 Steps - Instructables
Relay With Arduino Uno R3 : 7 Steps – Instructables
In-Depth: Interface One Channel Relay Module With Arduino
In-Depth: Interface One Channel Relay Module With Arduino
Mạch 4 Relay Arduino Shield 5V Kích Mức Cao Nguồn: 5Vdc, Dùng Cho Arduino,  Tải: 250Vac/30Vdc 3A
Mạch 4 Relay Arduino Shield 5V Kích Mức Cao Nguồn: 5Vdc, Dùng Cho Arduino, Tải: 250Vac/30Vdc 3A
Arduino Control Relay
Arduino Control Relay
How Many Relays Can I Connect To One Arduino? - Youtube
How Many Relays Can I Connect To One Arduino? – Youtube
How To Use A Relay Module With Arduino – Surtr Technology
How To Use A Relay Module With Arduino – Surtr Technology
Controlling A 230V Ac Bulb Using Arduino Uno And Relay - Youtube
Controlling A 230V Ac Bulb Using Arduino Uno And Relay – Youtube
Arduino - Relay | Arduino Tutorial
Arduino – Relay | Arduino Tutorial

See more here: kientrucannam.vn

Leave a Reply

Your email address will not be published. Required fields are marked *