Learn to code

ARDUINO & CODES

Previous Post
Create LED strip effects with Arduino

How to use a relay module with Arduino

in Arduino Components

In this tutorial, you will learn how to use a relay in your Arduino Project. A relay is very suitable for a low voltage project such as Arduino Projects.

They are very simple electronic components and in essence, relays are switches. There is two metal contact inside the relay which are normally not connected. There is a switch inside the relay, just like a light switch, that connects the two metal contacts. If the two metal contacts are connected the circuit is completed and the current can flow. The difference between a light switch and a relay is that in case off the light switch you are flicking the switch manually while the relay is an electric switch. So by sending a signal to the relay, the two metal contacts are pulled together to form a circuit that allows current to flow on the other side of the relay.

Why do you want to use a relay?

So what is the point of using a relay to power a circuit that has to be powered with electricity? Why wouldn’t you just power the circuit directly?
The answer lies in the distinction between the power that is required to activate the relay compared to the outputs that are required in the output circuit. In most tutorials on this website, we are using a 5v relay. This means that you need to apply 5v to activate the relay and it draws around 60 milliamps to activate. On the output side, it can control up to 30 volts 10-ampere circuit.

Why use it with Arduino then? Well, Arduino Boards are more signal senders then power providers. So if you want to provide power to a component that, especially those that require more dan 5v, you can use a relay to power them instead of powering it directly from the Arduino Board. If you add a relay you can control circuits above 5v limited by the rating of the relay.

What you will need to know?

Before continuing with this guide, you may want to check out any topics from the list below that sound unfamiliar.

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Breadboard 400 point Breadboard 400 point × 1
Dupont Wires Dupont Wires × 11
220 ohm resistor 220 ohm resistor × 1
DC Barrel Jack DC Barrel Jack × 1
5v Relay 5v Relay × 1
12v Solenoid Lock 12v Solenoid Lock ×

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

5v Relay Information

Most of the times you will order a relay module for Arduino instead of a relay. The blue-colored box is the relay and we call the entire piece a relay module. They come in different shapes and forms.It is possible to get the relay separately however I recommend to always buy the relay module instead because it will take a lot of work to attach a raw relay to an Arduino Board.

There are three pins on the input side that can be connected to the Arduino Board. On the output side, you also have three connectors for wiring. The module also has an LED that will light up if the relay is activated and turns off when it is deactivated. This makes it easy for us to check if the code that you wrote is working. There is also a diode attached in the relay module.

The flyback diode helps sus with charging and discharging the module. When the relay is deactivated it has to discharge and what could happen is that voltage is sent back to the Arduino Board that is attached to the relay through the pin that is controlling the relay. This may damage your Arduino and the flyback diode prevents that.

5v Relay  Specifications

FeatureSpecification
Trigger Voltage5v
Trigger Current+/- 70 mA
Max Load Control220V

BreadBoard Layout

5v Relay Connections

5v RelayArduino Board / Breadboard
VCC5V rail
GNDGND rail
In PinPin10
COM OutputGND DC Jack
NO OutputVCC Solenoid

The Code

The code below is similar to the code that is used in Making a toggle switch button with Arduino. Therefore, I will not go in detail with the code example. The only difference between this code and the code in the toggle switch tutorial is that you are going to change the variable names from LED into Relay and that you are controlling a relay instead off a LED.

// Toggle Switch Button with solenoid and relay

// Define the pins being used
int pinRelay = 10;
int pinSwitch = 2;

// declaring variables to hold the new and old switch states
boolean oldSwitchState = LOW;
boolean newSwitchState = LOW;
boolean RELAYstatus = HIGH;
void setup()
{
  pinMode(pinRelay, OUTPUT);
  digitalWrite(pinRelay, HIGH);
  pinMode(pinSwitch, INPUT);
}
void loop()
{
  newSwitchState = digitalRead(pinSwitch);
  if ( newSwitchState != oldSwitchState )
  {
    // has the button switch been closed?
    if ( newSwitchState == HIGH )
    {
      if ( RELAYstatus == LOW ) {
        digitalWrite(pinRelay, HIGH);
        RELAYstatus = HIGH;
      }
      else                    {
        digitalWrite(pinRelay, LOW);
        RELAYstatus = LOW;
      }
    }
    oldSwitchState = newSwitchState;
  }
}

In this tutorial I have demonstrated how to use a 5v Relay with the Arduino Uno Board. We created a push button that controls a solenoid lock through a relay.

I hope that you enjoyed reading the tutorial and fount is useful and informative. If you did please subscribe to my Instagram or Pinterest account found at the top of this page.

In any case that you have questions, feel free to ask them in the comments.

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu