ESP8266 Relay Module – Control AC Appliances (Web Server)

Introduction: ESP8266 Relay Module – Control AC Appliances (Web Server)

Using a relay with the ESP8266 is a great way to control AC household appliances remotely. This tutorial explains how to control a relay module with the ESP8266. We’ll take a look at how a relay module works, how to connect the relay to the ESP8266 and build a web server to control a relay remotely (or as many relays as you want).

Learn how to control a relay module with ESP32 board: Guide for ESP32 Relay Module – Control AC Appliances + Web Server Example.

Step 1: Introducing Relays

A relay is an electrically operated switch and like any other switch, it that can be turned on or off, letting the current go through or not. It can be controlled with low voltages, like the 3.3V provided by the ESP8266 GPIOs and allows us to control high voltages like 12V, 24V or mains voltage (230V in Europe and 120V in the US).

1, 2, 4, 8, 16 Channels Relay Modules There are different relay modules with a different number of channels. You can find relay modules with one, two, four, eight and even sixteen channels. The number of channels determines the number of outputs we’ll be able to control.

There are relay modules whose electromagnet can be powered by 5V and with 3.3V. Both can be used with the ESP8266 – you can either use the Vin pin (that provides 5V) or the 3.3V pin.
Additionally, some come with built-in optocoupler that add an extra “layer” of protection, optically isolating the ESP8266 from the relay circuit. Get a relay module: 5V 2-channel relay module (with optocoupler)5V 1-channel relay module (with optocoupler)5V 8-channel relay module (with optocoupler)5V 16-channel relay module (with optocoupler)3.3V 1-channel relay module (with optocoupler)

Step 2: Relay Pinout

For demonstration purposes, let’s take a look at the pinout of a 2-channel relay module. Using a relay module with a different number of channels is similar.

The two connectors (with three sockets each) on the left side of the relay module connect high voltage, and the pins on the right side (low-voltage) connect to the ESP8266 GPIOs.

Step 3: Mains Voltage Connections

The relay module shown in the previous photo has two connectors, each with three sockets: common (COM), normally closed (NC), and normally open (NO).

COM: connect the current you want to control (mains voltage).NC (Normally Closed): the normally closed configuration is used when you want the relay to be closed by default. The NC are COM pins are connected, meaning the current is flowing unless you send a signal from the ESP8266 to the relay module to open the circuit and stop the current flow.NO (Normally Open): the normally open configuration works the other way around: there is no connection between the NO and COM pins, so the circuit is broken unless you send a signal from the ESP8266 to close the circuit.

Step 4: Control Pins

The low-voltage side has a set of four pins and a set of three pins. The first set consists of VCC and GND to power up the module, and input 1 (IN1) and input 2 (IN2) to control the bottom and top relays, respectively.

If your relay module only has one channel, you’ll have just one IN pin. If you have four channels, you’ll have four IN pins, and so on. The signal you send to the IN pins, determines whether the relay is active or not. The relay is triggered when the input goes below about 2V. This means that you’ll have the following scenarios: Normally Closed configuration (NC):HIGH signal – current is flowingLOW signal – current is not flowingNormally Open configuration (NO):HIGH signal – current is not flowingLOW signal – current is flowingYou should use a normally closed configuration when the current should be flowing most of the times, and you only want to stop it occasionally.

Use a normally open configuration when you want the current to flow occasionally (for example, turn on a lamp occasionally).

Step 5: Power Supply Selection

The second set of pins consists of GND, VCC, and JD-VCC pins. The JD-VCC pin powers the electromagnet of the relay. Notice that the module has a jumper cap connecting the VCC and JD-VCC pins; the one shown here is yellow, but yours may be a different color.

With the jumper cap on, the VCC and JD-VCC pins are connected. That means the relay electromagnet is directly powered from the ESP8266 power pin, so the relay module and the ESP8266 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. That configuration physically isolates the relays from the ESP8266 with the module’s built-in optocoupler, which prevents damage to the ESP8266 in case of electrical spikes.

Step 6: ESP8266 Safest Pins to Use With Relays

ESP8266 Safest Pins to Use with Relays

Some ESP8266 pins output a 3.3V signal when the ESP8266 boots. This may be problematic if you have relays or other peripherals connected to those GPIOs. Additionally, some pins must be pulled HIGH or LOW in order to boot the ESP8266. Taking this into account, the safest ESP8266 pins to use with relays are: GPIO 5, GPIO 4, GPIO 14, GPIO 12 and GPIO 13. For more information about the ESP8266 GPIOs read: ESP8266 Pinout Reference: Which GPIO pins should you use? Wiring a Relay Module to the ESP8266 Connect the relay module to the ESP8266 as shown in the following diagram. The diagram shows wiring for a 2-channel relay module, wiring a different number of channels is similar. Warning: in this example, we’re dealing with mains voltage. Misuse can result in serious injuries. If you’re not familiar with mains voltage ask someone who is to help you out. While programming the ESP or wiring your circuit make sure everything is disconnected from mains voltage. Alternatively, you can use a 12V power source to control 12V appliances.

In this example, we’re controlling a lamp. We just want to light up the lamp occasionally, so it is better to use a normally open configuration.
We’re connecting the IN1 pin to GPIO 5, you can use any other suitable GPIO. See ESP8266 GPIO Reference Guide.

Step 7: Controlling a Relay Module With the ESP8266 – Arduino Sketch

The code to control a relay with the ESP8266 is as simple as controlling an LED or any other output. In this example, as we’re using a normally open configuration, we need to send a LOW signal to let the current flow, and a HIGH signal to stop the current flow.

Step 8: Code

The following code will light up your lamp for 10 seconds and turn it off for another 10 seconds.

Attachments

Step 9: How the Code Works

Define the pin the relay IN pin is connected to.

const int relay = 5; In the setup(), define the relay as an output.

pinMode(relay, OUTPUT); In the loop(), send a LOW signal to let the current flow and light up the lamp.

digitalWrite(relay, LOW); If you’re using a normally closed configuration, send a HIGH signal to light up the lamp. Then, wait 5 seconds.

delay(5000); Stop the current flow by sending a HIGH signal to the relay pin. If you’re using a normally closed configuration, send a LOW signal to stop the current flow.

digitalWrite(relay, HIGH);

Step 10: Control Multiple Relays With ESP8266 Web Server

In this section, we’ve created a web server example that allows you to control as many relays as you want via web server whether they are configured as normally opened or as normally closed. You just need to change a few lines of code to define the number of relays you want to control and the pin assignment.

To build this web server, we use the ESPAsyncWebServer library.

Installing the ESPAsyncWebServer library

Follow the next steps to install the ESPAsyncWebServer library:

Click here to download the ESPAsyncWebServer library. You should have a .zip folder in your Downloads folder Unzip the .zip folder and you should get ESPAsyncWebServer-master folder Rename your folder from ESPAsyncWebServer-master to ESPAsyncWebServer Move the ESPAsyncWebServer folder to your Arduino IDE installation libraries folder Alternatively, in your Arduino IDE, you can go to Sketch > Include Library > Add .ZIP library… and select the library you’ve just downloaded.

Installing the ESPAsyncTCP Library for ESP8266

The ESPAsyncWebServer library requires the ESPAsyncTCP library to work. Follow the next steps to install that library:

Click here to download the ESPAsyncTCP library. You should have a .zip folder in your Downloads folder Unzip the .zip folder and you should get ESPAsyncTCP-master folder Rename your folder from ESPAsyncTCP-master to ESPAsyncTCP Move the ESPAsyncTCP folder to your Arduino IDE installation libraries folder Finally, re-open your Arduino IDE Alternatively, in your Arduino IDE, you can go to Sketch > Include Library > Add .ZIP library… and select the library you’ve just downloaded.

After installing the required libraries, copy the following code to your Arduino IDE.

Step 11: Upload Code to Arduino

copy the following code to your Arduino IDE.

Step 12: Relay Configuration

Define Relay Configuration

Modify the following variable to indicate whether you’re using your relays in normally open (NO) or normally closed (NC) configuration. Set the RELAY_NO variable to true for normally open os set to false for normally closed.

#define RELAY_NO true

Define Number of Relays (Channels)

You can define the number of relays you want to control on the NUM_RELAYS variable. For demonstration purposes, we’re setting it to 5. #define NUM_RELAYS 5

Define Relays Pin Assignment

In the following array variable you can define the ESP8266 GPIOs that will control the relays.

int relayGPIOs[NUM_RELAYS] = {5, 4, 14, 12, 13}; The number of relays set on the NUM_RELAYS variable needs to match the number of GPIOs assigned in the relayGPIOs array.

Network Credentials Insert your network credentials in the following variables.

const char* ssid = "REPLACE_WITH_YOUR_SSID"; const char* password = "REPLACE_WITH_YOUR_PASSWORD";

Step 13: Wiring 8 Channel Relay to ESP8266

Step 14: Demonstration

After making the necessary changes, upload the code to your ESP8266.

Open the Serial Monitor at a baud rate of 115200 and press the ESP8266 RST button to get its IP address.

Then, open a browser in your local network and type the ESP8266 IP address to get access to the web server.

You should get something as follows with as many buttons as the number of relays you’ve defined in your code.

Now, you can use the buttons to control your relays remotely using your smartphone.

Be the First to Share

    Recommendations

    • Tinkercad Student Design Contest

      Tinkercad Student Design Contest
    • Organization Contest

      Organization Contest
    • Space Contest

      Space Contest

    Comments

    0
    sebasdj2
    sebasdj2

    Question 5 months ago on Step 14

    hello, how can I do to modify the names of the switches in the esp web server