Arduino Relay Tutorial for Beginners

Arduino Relay Tutorial for Beginners

Digital pins on the Arduino can take high or low values. This property is used to control most external motors, sensors, etc.

But sometimes, there are limitations due to the fact that the devices require higher currents than the Arduino can provide. Judging by the characteristics, the Arduino boards only provide us with 20mA.

Arduino Relay for Beginners

If you work too often with currents that exceed these recommendations, you will not only have an unreliable electrical circuit, but you can also damage your Arduino controller.

Instead, you will need to connect the required amperage. One of the options is to use a relay. Sometimes you will also need transistors.

Working Principle of Relay

The working principle of the relay is as follows. Supply voltage to the electromagnetic coil. There is a field in the coil, which attracts a metal foot. In its turn, the foot mechanically closes the load contacts.

With the help of an Arduino and a relay, we control the process of switching on or off in the same way as we switch on or off the light in the house – by giving the command to short or open. The Arduino sends a signal, while the very closing or opening of the “powerful” circuit will relay special internal mechanisms. The relay can be imagined as a remote control, with which we perform the necessary actions using relatively “weak” signals.

The working principle of the relay

The following parameters characterize the relay:

  • Voltage or current of triggering.
  • Release voltage or current.
  • Triggering and releasing time.
  • Operating current and voltage.
  • Internal resistance.

Depending on the type of these internal disconnecting mechanisms and the device’s features, two main relay groups can be distinguished: electromechanical relays (switching by electromagnet) and solid-state relays (switching by special semiconductor components).

Electromagnetic and Solid-state Relays

Electromagnetic Relays

An electromagnetic relay is an electrical device that mechanically closes or opens a load circuit using a magnet. It consists of an electromagnet, a moving armature and a switch. An electromagnet is a wire that is wound on a ferromagnetic coil. The anchor is a plate made of magnetic material. Some models may have additional electronic components built-in: a resistor for more accurate relay operation, a capacitor to reduce interference, a diode to eliminate overvoltages.

An electromagnetic relay

The relay operates due to the electromagnetic force in the cores when the current is applied to the coil coils. The spring holds the armature at its original state. When the control signal is given, the magnet starts to attract the armature and close or open the circuit. When the voltage is cut, the armature returns to the initial position. Control voltage sources can be sensors (pressure, temperature, etc.), electrical circuits and other devices that supply low current or low voltage.

Electromagnetic relay is used in automation circuits when controlling various technological installations, electric drives and other devices. The relay is designed to regulate voltages and currents, be used as a storage or conversion device, and record deviations of typical values’ parameters.

Types of Electromagnetic Relays:

  • The control current can be either direct or alternating. In the first case, the device can be neutral or polarized. For alternating current, the armature is made of electrotechnical steel to reduce losses.
  • Reed relay. For the anchor, the process of closing and opening is carried out by moving the anchor, for the reed is characterized by the absence of a core, the magnetic field affects the electrode with contacts.
  • Speed – up to 50 ms, up to 150 ms and from 1 s.
  • Protective coating – sealed, shrouded and open.

Compared with semiconductor devices, the electromagnetic relay has advantages – it is inexpensive, switching high load with small device size and low heat release on the coil. Among the disadvantages can be distinguished by slow response, interference and complexity of switching inductive loads.

Solid-state Relays

Solid-state relays are considered an excellent alternative to electromagnetic. They are a modular semiconductor device, which is produced by hybrid technology. The relays have transistors, TRIACs or thyristors. Compared to electromagnetic devices, solid-state relays have several advantages:

  • Long service life.
  • Speed of operation.
  • Small size.
  • No extraneous noise, no acoustic interference, contact rattling.
  • Low power consumption.
  • High-quality insulation.
  • Resistant to vibration and shock.
  • No arc discharge, which allows working in hazardous areas.

Solid-state relays

Work on the following principle:

  • The control signal is fed to the LED (here guide how to connect LED to Arduino).
  • There is galvanic isolation of the control and switching circuits.
  • The signal is transferred to the photodiode matrix.

The power key regulates the voltage.

Solid-state relays also have several disadvantages. First, when switching, the device is heated. An increase in the device’s temperature leads to limiting the adjustable current – at temperatures above 60 degrees, the current decreases, the maximum operating temperature of 176 degrees.

The following features classify solid-state relays:

  • Load type – single-phase and three-phase.
  • Control method – switching occurs at the expense of direct voltage, alternating or manual control.
  • Switching method: control of transition over zero (used for low-inductive, capacitive and resistive loads), accidental switching (inductive and resistive loads that require instantaneous operation) and phase control (change of output voltage, power regulation, incandescent lamp control).

Relays in Arduino Projects

The Arduino board’s most common relay is executed as a module, for example, SONGLE SRD-05VDC. The device is controlled by a 5V voltage and can switch up to 10A 30V DC and 10A 250V AC.

SONGLE SRD-05VDC in my hands
SONGLE SRD-05VDC in my hands

The circuit diagram is shown in the picture. The relay consists of two unrelated circuits – control circuits A1 and A2 and controlled circuit 1, 2 and 3.

Relay scheme example

There is a metal core between A1 and A2. An anchor (2) will be attracted to it if an electric current is applied to it. 1, 3 – fixed contacts. If there is no current, the armature will be near contact 3.

Connecting the Relay to the Arduino

Let us consider a single-channel relay module. It has only 3 contacts. They are connected to Arduino Uno as follows: GND – GND, VCC – +5V, In – 3. Input relay – is inverted so that the high level on In turns off the coil, and low – turns on.

Connecting the relay to the Arduino

LEDs are needed for an indication – when the red LED1 lights up, voltage is applied to the relay. When the green LED2 lights up, a short circuit occurs. When the microcontroller is switched on, the transistor is closed. The transistor shall need a minus and shall be supplied with the function digitalWrite(pin, LOW) to open it. The transistor shall open, current shall flow through the circuit, and the relay shall trip. The transistor has to be opened, the current shall flow through the circuit, and the relay shall be triggered. The digitalWrite(pin, HIGH) shall be applied to the base to switch it off.

Arduino Relay Control: Sketch

The examples were tested on Arduino Uno, but they can be easily applied to other Arduino boards: Uno, Mega, Nano.

Connection Diagram

In this example, the standard Arduino relay module is used, on which all necessary elements for connection to. The wiring diagram is very simple: the relay module is connected to the 5 pins of the Arduino board. At the same time, for simplicity, we may not even connect the real load – the relay will click at each state change, we will hear these clicks, and we will understand that the sketch works.

Connection diagram

Sketch for Working with Relays

 * A sketch for Arduino relay control
 * We use SONGLE SRD-05VDC relay
 * The relay is OPEN when a low signal level (LOW) is applied to the control pin.
 * Relay CLOSES when a high-level signal (HIGH) is applied to the control pin.
 * In this example, we simply open and close the relay every 5 seconds.
 * PIN_RELAY contains the pin number to which the relay we will control is connected 
 * In the setup function, set the initial position of the relay (closed).
 * If a load (e.g., a light bulb) is connected to the relay, it will be on and off every 5 seconds after the sketch is started.
 * To change the blinking period, you need to change the delay() function parameter: set 1000 milliseconds and get 1 second of delay.
 * In real projects, the relay is switched on in response to the detection of any external events through the connection of sensors. 

#define PIN_RELAY 5 // Define the pin used to connect the relay

// In this function we define the initial settings
void setup()
  pinMode(PIN_RELAY, OUTPUT); // Declare pin relay as output
  digitalWrite(PIN_RELAY, HIGH); // Switch off relay - send high signal
void loop()
  digitalWrite(PIN_RELAY, LOW); // Switch on the relay - send low signal level
  digitalWrite(PIN_RELAY, HIGH); // Switch off relay - send high signal level

Relay Control Sketch with Motion Sensor

In real projects, the relay state change should take place in response to some medium reaction. For example, it is possible to switch on the light in response to the triggered motion sensor signal by closing the circuit with the relay. In this sketch, we will consider such a connection option.

Relay Connection Diagram

It should be understood that in real projects, they do without Arduino at all – just connecting the sensor signal output to the relay.

Schematic IR Motion Relay

Example of a Sketch

In this example, we will add to the loop check of the PIR sensor using the function digitalRead(). If we get HIGH, it means that the sensor is triggered, and we perform the action – switch on the relay. If we connect a light bulb to it, it will light up. But, like in the previous example, you can just listen to the clicks.

   Sketch for Arduino relay control with PIR sensor

   PIN_RELAY contains the pin number to which the relay we will control is connected.
   PIN_PIR contains pin number with connected PIR sensor

   In the setup function, we set the initial position of the relay (closed).
   In the body of the loop function, we check the presence of a high level of the signal from the sensor using the digitalRead function.

   For debugging, the current sensor value is displayed in the port monitor window.

#define PIN_RELAY 8 // Define the pin used to connect the relay
#define PIN_PIR 5 // Define the pin used to connect the PIR sensor

// In this function, we define the initial settings
void setup()
  pinMode(PIN_RELAY, OUTPUT); // Declare pin relay as output
  digitalWrite(PIN_RELAY, HIGH); // Switch off relay - send high signal
void loop()

  int val = digitalRead(PIN_PIR); // Read the value from the motion sensor into a separate variable

  if (val == HIGH) {
    Serial.println ("Sensor triggered");
    digitalWrite(PIN_RELAY, LOW); // Switch on the relay - send low level signal
  } else {
    digitalWrite(PIN_RELAY, HIGH); // Switch off relay - send high signal level

  delay(1000); // Check the values once per second.

Disconnect your USB cable from your personal computer and connect an external power supply to the Arduino and relay. Give your Microcontroller time to reboot. If everything has been done correctly, you should hear the characteristic click of a relay that will close and open the contact every two seconds.

Leave your comments, questions and share your personal experience below. New ideas and projects are often born in the discussion!