Use Relay with Arduino  Step by Step Guide ▷ 2020

If you want to know how to use a relay on Arduino to control the voltage of your electronic projects, you will have to continue reading this article to the end.

We will show you in the following paragraphs what are these components for in electronics and what are the criteria that you must take into account when choosing a model and use it on your arduino kit.

Don’t miss anything because We will also explain, in an easy way, the steps you must do to use a relay on your badge Arduino. Lastly, you will see the best relay projects you can use.

What is a relay and what are these components used for in electronics?

A relay is an electromagnetic device that works as a switch and that is responsible for activating one or more contacts, which allow opening or closing circuits from the plate Arduino. In this way a signal can be controlled in low or high power, the latter being very important for the management of motors or other loads.

It is composed of a coil (that surrounds a nucleus) that allows activating, through a armor, to one or more contacts. These transmit the intensity of the load to the terminals so that the relay is connected to the Arduino.

What should I keep in mind when choosing and using a relay for my Arduino kit?

What should I keep in mind when choosing and using a relay for my Arduino kit?

The first thing you should know is that a relay has 2 circuits inside. The first works by receiving the signal and then connecting it to the low voltage circuit of the Arduino board, while the second circuit turns the current on and off.

This means that the operation of a relay requires a switching time, so it cannot be considered as a PWM type signal. That is, with a medium-high frequency. From this it follows that, although it is true that a relay can work with direct and alternating current loads, it cannot always be used in all types of projects, as it will depend on the signal strength with which you have to work.

Another issue that you should take into account when choosing a relay to suit your needs is the physical dimension what’s wrong with it. This being an important factor when choosing one or the other model. By last, the normal life of a relay is related to the amount of switching. This means that it has a limited useful life, so you should keep in mind a possible replacement in the future.

Learn step by step how to use a relay with your Arduino board without making mistakes

The step by step you will have to do to use a relay on your Arduino board without making mistakes is the following:

Assemble all the components

Assemble all the components

The first thing you should do is connect the pins Vcc and GND to same board connectors from Arduino. Then you will have to assemble the load to the terminal block, or to the connector (depending on the model), of 3 connections, taking into account that the positive pole must be connected to terminal C.

Then the other load town you will have to connect it to NO or NC connector. In this step You will have to choose if you want the secondary to deactivate when the relay stay open or closed. Finally, you will have to connect the pin that belongs to a signal to digital connector of the plate.

Enter the programming codes

Once you have assembled all the components with the Arduino board You will have to schedule it so that it works automatically.

The IDE codes are:

const int pin = 9;

void setup() {

Start the serial port and then define a pin as the output:

begin(9600);

pinMode(pin, OUTPUT);

}

Next, you will have to include pin up, and then on low.

For this you must write:

void loop(){

digitalWrite(pin, HIGH);

delay(10000);

digitalWrite(pin, LOW);

delay(10000);

}

Get to know the best Arduino projects with relays that you can make yourself to practice

Get to know the best Arduino projects with relays that you can make yourself to practice

We show you below Best Arduino Projects You Can Use With Relay so that you can practice what you have learned yourself.

Let’s get started:

Turn on an LED with relay

This very simple project will help you take your first steps and practice incorporating a relay to turn off and on a led with Arduino, then you can modify it and turn on another 220V element. For this job you will need connect the Arduino’s ports to the ground and 5 volt pin. You will also need to assemble the digital pin with the relay signal, the common terminal will be connected to 5V, the positive of the led will connect with NO and the negative with NC.

After this, you will have to write the following codes to finish the programming:

int rele=8;

void setup() {

pinMode(8, OUTPUT);

}

This will make the loop function I know always run.

Then you must enter:

void loop() {

digitalWrite(rele, HIGH);

delay(1000);

digitalWrite(rele, LOW);

delay(1000);

}

Candy Vendor

In this project you can, through an Arduino board, create a candy dispenser automatically. You will need one Arduino MKR WiFi 1010 board, a servo motor, an LCD screen, a relay and a generic buzzer.

The programming code is long, so we leave it in the following link: https://github.com/ronibandini/CleanAlfajor/blob/master/CleanAlfajor.ino

Fire extinguisher

Fire extinguisher

With Arduino you will be able to create a fire extinguisher that will activate automatically when its sensors detect smoke. You’re going to need a fire extinguisher, a Arduino UNO, relay and photocells. This project is a bit complicated for people who are just starting to explore the Arduino world, but it does not imply that it is impossible. It has several plans that you must execute as you go, so you will have to be careful to perform all the steps perfectly.

You can copy all the Arduino programming codes below:

int lightPin = 0;

int latchPin = 11;

int clockPin = 9;

int dataPin = 12;

int lightPin2 = 1;

int lightPin3 = 2;

int lightPin4 = 3;

int lightPin5 =4;

int ledsLit = 0;

int leds = 0;

int readingEnough1 = 0;

int readingEnough2 = 0;

int readingEnough3 = 0;

int readingEnough4 = 0;

int bias = 0;

int ringerlight = 8;

int readAll = 0;

void setup()

{

pinMode(ringerlight, OUTPUT);

begin(9600);

digitalWrite(ringerlight, LOW);

}

void loop()

{

readAll = 0;

int reading5 = analogRead(lightPin5); /

bias = reading5;

int reading = analogRead(lightPin);

if (reading > (150+bias)) readingEnough1 = readingEnough1 + 1;

delay(20);

int reading2 = analogRead(lightPin2);

if (reading2 > (150+bias)) readingEnough2 = readingEnough2 + 1;

delay(20);

int reading3 = analogRead(lightPin3);

if (reading3 > (150+bias)) readingEnough3 = readingEnough3 + 1;

delay(20);

int reading4 = analogRead(lightPin4);

if (reading4 > (150+bias)) readingEnough4 = readingEnough4 + 1;

delay(20);

print(reading);

print(" : ");

print(reading2);

print(" : ");

print(reading3);

print(" : ");

println(reading4);

print("BIAS: ");

println(reading5);

print(readingEnough1);

print(" : ");

print(readingEnough2);

print(" : ");

print(readingEnough3);

print(" : ");

println(readingEnough4);

readAll = readingEnough1 + readingEnough2 + readingEnough3 + readingEnough4; //count to see if every photocell has received a bright light

print("readAll : ");

println(readAll);

delay(50);

if (readAll >= 12)

{

ringBuzzerLight();

}

}

void ringBuzzerLight()

{

int threshold = 5;

println("You hit all Photocells");

for (int r = 0; r <=5; r++) {

println(r);

if (r == threshold){

digitalWrite(ringerlight,LOW);

readingEnough1 = 0;

readingEnough2 = 0;

readingEnough3 = 0;

readingEnough4 = 0;

break;

}

digitalWrite(ringerlight, HIGH);

delay(500);

digitalWrite(ringerlight,LOW);

delay(500);

}

}int reading3 = analogRead(lightPin3);

if (reading3 > (150+bias)) readingEnough3 = readingEnough3 + 1;
delay(20);
int reading4 = analogRead(lightPin4);
if (reading4 > (150+bias)) readingEnough4 = readingEnough4 + 1;
delay(20);
print(reading);
Serial.print(" : ");
Serial.print(reading2);
Serial.print(" : ");
Serial.print(reading3);
Serial.print(" : ");
Serial.println(reading4);
Serial.print("BIAS: ");
Serial.println(reading5);
Serial.print(readingEnough1);
Serial.print(" : ");
Serial.print(readingEnough2);
Serial.print(" : ");
Serial.print(readingEnough3);
Serial.print(" : ");
Serial.println(readingEnough4);
readAll = readingEnough1 + readingEnough2 + readingEnough3 + readingEnough4; //count to see if every photocell has received a bright light
Serial.print("readAll : ");
Serial.println(readAll);
delay(50);
if (readAll >= 12)
{
ringBuzzerLight();
}
}
void ringBuzzerLight()
{
int threshold = 5;
Serial.println("You hit all Photocells");
for (int r = 0; r <=5; r++) {
Serial.println(r);
if (r == threshold){
digitalWrite(ringerlight,LOW);
readingEnough1 = 0;
readingEnough2 = 0;
readingEnough3 = 0;
readingEnough4 = 0;
break;
}
digitalWrite(ringerlight, HIGH);
delay(500);
digitalWrite(ringerlight,LOW);
delay(500);
}
}

If you have any questions, leave them in the comments, we will answer you as soon as possible, and it will surely be of great help to more members of the community. Thank you! 😉

You may be interested:

Leave a Comment