ESP8266 - Relay

In a prior tutorial, we discovered how to switch on/off an LED using ESP8266. This tutorial instructs you how to activate/deactivate certain devices that make use of a high voltage power source (e.g. a light bulb, fan, electromagnetic lock, linear actuator...) by using ESP8266.

? What are the commons and differences between controlling an LED and controlling a light bulb by using Arduino?

The common: We use the Arduino's output pin to turn on and off, The common: Similar to controlling an LED.

The difference:

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Relay
1×LED Strip
1×12V Power Adapter
1×DC Power Jack
1×Breadboard
1×Jumper Wires
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) Screw Terminal Expansion Board for ESP8266

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Relay

A relay is an electrical switch that can be programmed by ESP8266 or any micro-controller. It is utilized to control devices that use high voltage and/or high current in a programmatic way.

It acts as a bridge between ESP8266 and high voltage components.

WARNING

When you are creating projects that involve mains voltage, it is important to be aware of the potential danger. We urge you to be careful and not to proceed if you are not completely sure of what you are doing. It is better to ask someone who knows than to risk getting shocked.

Although some relays can be used with both DC and AC devices, we strongly suggest using a DC device with a voltage of 24V or less for testing purposes.

Relay Pinout

Relay pinout

Relay has two sets of pins: one for the input (low voltage) and another for the output (high voltage).

  • The input group pins are connected to ESP8266, including three pins:
    • DC- pin: must be connected to GND (0V)
    • DC+ pin: must be connected to VCC (5V)
    • IN pin: receives the control signal from ESP8266
  • The output group pins are connected to the high voltage device, including three pins (usually in screw terminal):
    • NO pin: is the normally open pin. It is used in the normally open mode
    • NC pin: is the normally closed pin. It is used in the normally closed mode
    • COM pin: is the common pin. It is used in both normally open and normally closed modes

    Generally, we do not utilize all of the pins in the high voltage group. We just use two of them:

    • If we use the normally open mode, we only need the COM pin and NO pin.
    • If we use the normally closed mode, we only need the COM pin and NC pin.

    In addition, if the relay has both LOW and HIGH level triggers, there is usually a jumper to choose between them.

    ※ NOTE THAT:

    The arrangement of the pins on a relay module can differ among manufacturers. It is crucial to always refer to the labels printed on the relay module when working with it. Pay close attention.

    How to Connect the High Voltage Device to Relay

    How to connect relay

    How It Works

    The way a relay functions may vary depending on the manufacturer and how it is installed by the user.

    For the IN pin, there are two input modes that make the relay operate in opposite ways:

    • Triggering at a LOW level, called LOW level trigger mode.
    • Triggering at a HIGH level, called HIGH level trigger mode.

    The output mode (for output pins): There are two modes that make the relay work in a different way:

    • normally open mode
    • normally closed mode. These modes are opposite to each other.

    The term "normally" implies "if the IN pin is linked to LOW (0V)".

    Before delving into the details, here is some quick information:

    • The normally open and normally closed modes operate inversely
    • Most relay modules support both normally open and normally closed modes
    • The LOW level trigger and HIGH level trigger modes work inversely
    • Not all relay modules support both LOW level trigger and HIGH level trigger modes
    • At any given time, the relay module can only be in one of the two LOW level trigger and HIGH level trigger modes

    The combination of input modes and output modes generates a range of applications. If you are just starting out, we suggest using HIGH level trigger mode and normally open mode.

    The HIGH level trigger mode will be explained in detail. As the LOW level trigger works in an opposite manner, this will be discussed separately.

    HIGH Level Trigger - Normally Open Mode

    In order to employ this mode, it is necessary to attach the high voltage device to the COM and NO pins.

    When the IN pin is linked to LOW (0V), the switch is open and the device is OFF (or inactive).

    Conversely, when the IN pin is linked to HIGH (5V), the switch is closed and the device is ON (or active).

    How Relay Works - Normally Open

    HIGH Level Trigger - Normally Closed Mode

    In order to utilize this mode, the high voltage device must be connected to the COM pin and NC pin.

    When the IN pin is linked to LOW (0V), the switch is shut. This means the device is ON (or active).

    Conversely, if the IN pin is linked to HIGH (5V), the switch is open. This implies the device is OFF (or inactive).

    How Relay Works - Normally Closed

    Summary

    Input modes Output Modes IN pin (programmable) Output pins Relay state Device state
    HIGH Trigger Normally Open LOW COM and NO pin ⇒ open OFF
    HIGH Trigger Normally Open HIGH COM and NO pin ⇒ closed ON
    HIGH Trigger Normally Closed LOW COM and NC pin ⇒ closed ON
    HIGH Trigger Normally Closed HIGH COM and NC pin ⇒ open OFF
    LOW Trigger Normally Open LOW COM and NO pin ⇒ closed ON
    LOW Trigger Normally Open HIGH COM and NO pin ⇒ open OFF
    LOW Trigger Normally Closed LOW COM and NC pin ⇒ open OFF
    LOW Trigger Normally Closed HIGH COM and NC pin ⇒ closed ON

    There are a maximum of 8 use cases. It may be overwhelming. However, if you are a beginner, you only need to pay attention to the first two cases, which involve HIGH level trigger and normally open. The remainder of this tutorial will focus on those two use cases.

    ESP8266 - Relay

    ESP8266 manages a relay, which in turn, controls a high voltage device.

    Managing a relay is straightforward. All that is required is:

    • Linking an Arduino's pin to the IN pin of the relay
    • Programming the pin to LOW or HIGH to control the relay

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and Relay

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

How To Program For Relay

  • Set an Arduino's pin to digital output mode by utilizing the pinMode() function. As an example, pin 3:
pinMode(3, OUTPUT);
  • Set the pin to 0V by utilizing the digitalWrite() function:
digitalWrite(3, LOW);
  • Set the pin to 5V by utilizing the digitalWrite() function:
digitalWrite(3, HIGH);

ESP8266 Code

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-relay */ #define RELAY_PIN D8 // The ESP8266 pin connected to the IN pin of relay // The setup function runs once on reset or power-up void setup() { // initialize digital pin as an output. pinMode(RELAY_PIN, OUTPUT); } // The loop function repeats indefinitely void loop() { digitalWrite(RELAY_PIN, HIGH); delay(1000); digitalWrite(RELAY_PIN, LOW); delay(1000); }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Copy the code above and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the ESP8266.
  • Check out the LED strip, which should be blinking.

Video Tutorial

Challenge Yourself

  • When you enter your room, the light will be automatically switched on. After you leave, it will turn off after 30 seconds. Refer to ESP8266 - Motion Sensor for more information.

Function References

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!