Relays
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.
A relay is an electrically operated switch. It consists of a pair of input terminals (+ve and -ve terminals) on one side and a couple of mechanical contact switch terminals on the other side. One electrical circuit (control circuit) is connected on the input side, and another electrical circuit (controlled circuit) is connected on the other side. The control circuit can send a high voltage or an electrical signal to the input terminals, which will then change the position of the mechanical contact switch (opening it or closing it) to open or close the controlled circuit and turn the controlled device ON/OFF.
Generally, a low-voltage electronic circuit is used to control a high-voltage electrical circuit. Relays are therefore extensively used in IoT projects, where, using an electronic circuit with a microprocessor, you can control your electrical home light bulbs and other appliances. The traditional form of a relay uses an electromagnet to close or open the switch contacts. However, other operating principles have been invented, like in solid-state relays using semiconductor properties to control without relying on moving parts.
The switch (controlled) side of a relay is often designed to be available in two operating modes: Normally Open (NO) and Normally Closed (NC). In a normally open relay, the controlled circuit is open by default, so we can say a lightbulb connected in that circuit will be off. But when the control circuit sends a high voltage signal to indicate the switch is closed and the lightbulb turns on. In a normally closed relay, it works in a reverse manner. The controlled circuit is closed by default, so we can say a lightbulb connected in that circuit will be on. When the control circuit sends a high voltage signal, the switch is opened, and the lightbulb turns off. In an IoT solution, where an electronic circuit needs to control a physical object, for example, close a door, a combination of relays and actuators is used. The control signal from the electronic circuit will switch the actuator circuit on or off, and the actuator will perform the action for which it is designed.
The illustration shows how a relay works:
The relay module shown above has two channels (the blue cubes). Modules with one, four, and eight channels are also available. This module is powered with 5V, which makes it appropriate to use with an Arduino. You can also get relay modules powered with 3.3V, which is ideal for an ESP32.
Each channel has a high voltage side connected to the electrical appliance the relay is controlling and a low voltage side connected to the microcontroller controlling the relay.
The high-voltage side of each channel has three sockets/pins:
-
COM: Common pin to which you connect the neutral wire (in any electrical circuit there are two wires one the carries the electric current known as the live wire and the other is the neutral wire) to the electrical appliance.
-
NC (Normally Closed): the normally closed configuration is used when you want the relay to be closed by default, meaning the current is flowing (and the connected appliance is powered on) until you send a signal from the microcontroller to the relay module to open the circuit and stop the current (and switch off the connected appliance).
-
NO (Normally Open): This configuration works the other way around from NC. The relay is always open, so the circuit is broken (and the connected device is powered off) until you send a signal from the microcontroller to close the circuit (and switch on the connected device).
The low-voltage side of each channel has the VCC and GND pins to power up the module and a third pin which is the input pin to send the control signal for each channel (IN1 and IN2 in this module).
The module has a jumper cap connecting the VCC and JD-VCC pins. The jumper cap allows you to choose whether the circuit is physically connected to the Arduino circuit 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. Without the jumper cap, you need to provide an independent power source to power up the relay’s electromagnet through the JD-VCC pin. This configuration physically isolates the relays from the Arduino with the module’s built-in optocoupler.