ESP32 - Relay

In a previous tutorial, we have learned how to turn on/off an LED. In this tutorial, we are going to learn how to turn on/off some kind of devices that use the high voltage power supply(such as a light bulb, fan, electromagnetic lock, linear actuator...).

What are the common and difference between controlling LED and controlling a light bulb?

The common: Just like controlling LED, we use the ESP32's output pin to turn on/off them.

The difference:

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×Relay
1×LED Strip
1×12V Power Adapter
1×Breadboard
n×Jumper Wires
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About Relay

A relay is a programmable electrical switch, which can be controlled by ESP32 or any micro-controller. It is used to programmatically control on/off the devices, which use the high voltage and/or high current.

It is a bridge between ESP32 and high voltage devices.

WARNING

When you are making projects that are connected to mains voltage, you need to know what you are doing, otherwise, you may shock yourself. This is a serious topic, and we want you to be safe. If you’re NOT 100% sure what you are doing, do yourself a favor and don’t touch anything. Ask someone who knows!

Although some kinds of relays support both DC and AC devices, We highly recommend you to use a DC device (≤24V) for testing.

Relay Pinout

Relay Pinout

Relay has two groups of pins: low voltage group and high voltage group.

  • Pins in the low voltage group are connected to ESP32, including three pins:
    • GND pin: needs to be connected to GND (0V)
    • VCC pin: needs to be connected to VCC (5V)
    • IN pin: receives the control signal from ESP32
  • Pins in the high voltage group are connected to high voltage a device, including three pins (usually in screw terminal):
    • COM pin: is the common pin. It is used in both normally open mode and normally closed mode
    • NO pin: is normally open pin. It is used in the normally open mode
    • NC pin: is normally closed pin. It is used in the normally closed mode

In practice, we usually do NOT use all of the pins in the high voltage group. We use only two of them:

  • We use only COM pin and NO pin if we use normally open mode.
  • We use only COM pin and NC pin if we use normally closed mode.

※ NOTE THAT:

The order of the relay's pins can vary between manufacturers. ALWAYS use the labels printed on the relay. Look closely!

Some relay module only has two pin in high voltage group.

How to Connect the High Voltage Device to Relay

How to connect relay

How It Works

The relay can work with two modes: normally open mode and normally closed mode. These modes are the opposite.

The “normally” means “if IN pin is connected to LOW (0V)”.

Normally Open Mode

To use this mode, we need to connect the high voltage device to the COM pin and NO pin.

  • If the IN pin is connected to LOW (0V), the switch is open. The device is OFF (or inactive).
  • If the IN pin is connected to HIGH (5V or 3.3V), the switch is closed. The device is ON (or active).
  • How Relay Works - Normally Open

Normally Closed Mode

To use this mode, we need to connect the high voltage device to the COM pin and NC pin.

  • If the IN pin is connected to LOW (0V), the switch is closed. The device is ON (or active).
  • If the IN pin is connected to HIGH (5V or 3.3V), the switch is open. The device is OFF (or inactive).
  • How Relay Works - Normally Closed
Which mode should I use?

You can choose any of them.

ESP32 - Relay

ESP32 controls a high voltage device by controlling a relay.

Controlling a relay is simple. We just need:

  • Connect an ESP32's pin to the IN pin of the relay
  • Control the relay by programming the pin to LOW or HIGH

Wiring Diagram

ESP32 Relay Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

How To Program For Relay

  • Configure an ESP32's pin to the digital output mode by using pinMode() function. For example, pin GIOP27:
pinMode(27, OUTPUT);
digitalWrite(27, LOW);
digitalWrite(27, HIGH);

ESP32 Code

/* * Created by esp32io.com * * This example code is in the public domain * * Tutorial page: https://esp32io.com/tutorials/esp32-relay */ // constants won't change const int RELAY_PIN = 27; // ESP32 pin GIOP27 connected to the IN pin of relay // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin as an output. pinMode(RELAY_PIN, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(RELAY_PIN, HIGH); delay(500); digitalWrite(RELAY_PIN, LOW); delay(500); }

Quick Steps

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE
  • Copy the above code and paste it to Arduino IDE
  • Click Upload button on Arduino IDE to compile and upload code to ESP32 board
  • See LED strip state: blinking

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

Challenge Yourself

  • Automatically turn on the light when you enter into your room and turn off the light after you leave 30 seconds. Hint: Refer to ESP32 - Motion Sensor.

Alternative Relays

The above code also works with the following relays:

Function References

※ NOTE THAT:

Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.

※ NOTICES

  • We are AVAILABLE for HIRE. See how to hire us to do your project
  • If this tutorial is useful for you, please support us to make more tutorials.
  • We spent a lot of time and effort to create the content for this tutorial, please respect our work! Please do not copy the content to share on other websites. Howerver, please feel free to share the link of this tutorial anywhere