A relay is an electrically operated switch that harnesses the power of an electromagnet to either open or close a circuit, while a switch is a mechanical device that opens or closes a circuit physically. Switches can be activated manually, but relays are typically controlled by electrical signals.
# Why Relay
You can connect an LED directly to ESP32 but you cannot connect a high voltage devices (such as an electric lamp, pump, electromagnetic lock, linear actuator, or even a big machine...) directly to ESP32. We need a relay in between ESP32 and the high voltage devices. Without relay, the high voltage will destroy ESP32.
The common between controlling a LED and an electric lamp: ESP32 code programs the ESP32's output pin to HIGH/LOW to turn THEM on/off .
The difference between controlling a LED and an electric lamp: Controlling LED: LED can works with 3.3v or lower ⇒ can connect LED directly to ESP32's pin. Controlling the electric lamp: the electric lamp works with high voltage (e.g. 12V)⇒ CANNOT connect the electric lamp directly to ESP32's pin. It needs to use a relay between ESP32's pin and the electric lamp. If not, ESP32 will be burned.
Some relays can work with both DC and AC voltage, we extremely recommend you NOT to use AC voltage. Use a DC device (≤24V) only.
# How Relay Works (esp32 relay)
The relay pins can be categorized two groups: input (low voltage) and output (high voltage).
The input pins: including three pins:
- DC- pin: connect this pin to GND (0V)
- DC+ pin: connect this pin to VCC (5V)
- IN pin(s signal): receives the control signal
The output pins: are interfaced to high-voltage device, including three pins (usually in screw terminal):
- NO pin: Normally Open pin
- NC pin: Normally Closed pin
- COM pin: the common pin
Normally, we do NOT use all high voltage pins. We usually use only two, depending on the operation mode we choose:
Normally open mode: Use only COM pin and NO pin
Normally closed mode: Use only COM pin and NC pin
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 pin order of the relay can be different between manufacturers. Please check the labels printed on the relay carefully!
There are several ways that a relay module can work, depending on our choices.
- In the input side, we can choose one of the two below opposite input modes:
- LOW level trigger mode
- HIGH level trigger mode
- In the output side, we can choose one of the two below opposite output modes:
- normally open mode
- normally closed mode. These modes are the opposite.
The “normally” means “if the IN/signal pin is connected to LOW (0V)”.
Before going into detail, let's see some quick information:
- 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
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
Connect the high-voltage device to the COM pin and NO pin. Just like a switch:
- If the IN pin is connected to LOW (0V), the switch is open. The device is OFF (or deactivated).
- If the IN pin is connected to HIGH (5V or 3.3V), the switch is closed. The device is ON (or activated).
# HIGH Level Trigger - Normally Closed Mode
Connect the the high-voltage device to the COM pin and NC pin. Just like a switch:
- If the IN pin is connected to LOW (0V) ⇒ The switch is closed ⇒ The device is ON (or activated).
- If the IN pin is connected to HIGH (5V or 3.3V) ⇒ The switch is open ⇒ The device is OFF (or deactivated).
Which mode should we use?
⇒ It depends on application.
Relay - Explained and animated - how relay works https://www.youtube.com/watch?v=Ca20ktPygY8