Control Relay Module Remotely with ESP32 Web Server and 220V Lamp

One of the best projects you can consider doing with ESP32 is to control a 220-volt lamp from a web server. But you can also use the same procedure to control fans, lights, AC, or other electrical devices that you want to control remotely. This tutorial is about controlling a relay and a 220-volt lamp from a Web Server using the ESP32 Dev board. First, we will see how to interface a relay module and a bulb with ESP32, and after that, we will move towards controlling the relay module and the lamp via a web server.

remotely control 220 volt lamp with ESP32

Introduction to a relay module

Relay is an electromechanical device that is used as a switch between high current and low current devices. It has two main parts, a coil and the contacts. The working of a relay is simple, we generally connect DC voltage to the coil and the load to normally open or close contacts. When we supply DC voltage across the coil, it starts to energize. When the coil gets fully energized, the contact shifts from the normally open position to the normally closed position. If you want to learn more about relays and their working, we have linked these articles below.

Why do we need to use a relay?

Light bulbs usually operate on 120V or 220V AC power supply. We cannot interface these AC loads directly with the ESP32 development board, or it will damage the board. We have to use a relay between the ESP32 and the lamp. You can use a relay as a switch to control 120-220 volt lamps. We will show you how to control the relay with ESP32. We will use a button on a web page to control the relay module. The ESP32 will give a signal to the relay module depending on the button state from the web page. You can use any relay module compatible with ESP32 for this project. However, for this tutorial, we will be using the SRD-05VDC-SL-C 5V Relay module. It works on 5V DC voltage and can be used to control the output loads from 120V-240V and current range up to 10A.

How 5V relay module work?

A relay module has 3 contact sockets and 3 pins. The three contact sockets are used to connect with high-voltage loads, i.e. electrical loads and a 220-volt power supply, and the three pins are used for the low-voltage side, namely VCC, GND, and the signal/control pin of the ESP32 Dev board.

Relay module pinouts

Relay module pinout ESP32

Two Tables are given below, one provides details of sockets and the other provides details of pin of relay module

Socket NameSocket Function
NCNormally closed terminal (120-240V)
NONormally open terminal (120-240V)
CommonCommon terminal to connect one led of load
Table 1: Relay module sockets and their functions.
Pin Name Pin Function
VDD5V pin (connect with ESP32 Vin pin)
GNDConnect with ground pin of ESP32
Signal pinControl pin for relay
Table 2: Relay module pins and their functions.

When we apply an active high signal to the signal pin of the relay module from any microcontroller like ESP32, the relay contact moves from the normally open to the normally closed position. It makes the circuit complete, and the output load turns on.

There are two contact sockets that are normally open (middle and bottom socket) and two contact sockets that are normally closed (middle and top socket). You always use the common socket along with either the normally open socket or the normally closed socket to connect the main output load. Depending on the NC or NO socket, you will either use an active high or active low signal to control the relay.

Normally open: If you use a normally open socket, the relay will activate with an active high signal on the control pin, and an active low signal will turn off the relay as shown in the figure below.

5 volt relay normally open

Normally closed: If you use a normally closed socket, the active low signal will activate the relay, and an active high signal on the signal pin will deactivate the relay.

5 volt relay normally close

So, you can choose either pin according to whether the active signal is high or low. Choose the type of signal through which you want to turn on or turn off the relay.

Components Required for this Project

You will require the following components to build ESP32 remote control relay project:

  • ESP32 development board
  • Relay module
  • Lamp
  • Lamp holder
  • Jumper wires
  • Bread board
  • Power supply connector

Connection diagram of relay and lamp with ESP32

Warning: Before making this circuit, make sure you have not connected ESP32 and the lamp with the main power supply. You are dealing with HIGH voltage in this project, please take all necessary precautions to avoid any mishaps before following the schematic provided below.

Now make all the connections on the breadboard according to the connection diagram provided below:

Web Server control a Relay module with ESP32

We will be using the normally open configuration in this project because we want to light up the bulb with an active high signal. But, you can also use the normally closed configuration based on your preference. Here, we will use the COM (common) socket and the NO (normally open) socket on the high voltage side of the relay. Normally, there is no contact between the COM and NO sockets, so the lamp will remain off. As soon as we provide 5V on the signal pin of the relay module through the GPIO pin of the ESP32, the relay will become active and the lamp will light up. In short, when we send an active high signal from the ESP32, the relay turns on. When we send an active low signal from the ESP32, the relay will turn off, and similarly, the lamp will turn on and off based on the relay’s state.

Note: Some relay modules have transistors that work on inverted logic. Inverted logic means an active low signal will activate the relay and an active high signal will deactivate the relay. You should check your relay module’s functionality or datasheet before using it to avoid any issues. The SRD-05VDC-SL-C 5V relay module does not work on inverted logic.

Steps to Complete the Connection Diagram

Here we have listed the steps to assemble the circuit on the breadboard as showcased on the figure in the previous section

  • Connect the 220V connector’s first terminal with one terminal of the lamp. The other terminal of the connector will attach to NO (Normally Open) Socket of the relay module.
  • The second terminal of lamp will connect to COM (Common) socket of the relay module.
  • Now, connect the GND and VDD pin of relay module with GND and VCC/VIN pin of ESP32 Dev board respectively.
  • In this tutorial we are using GPIO22 of ESP32 to provide active high signal to the relay. Connect GPIO22 with signal pin of the relay. You can read ESP32 GPIO pins tutorial for further details on GPIO pins.

Note: We are using SRD-05VDC-SL-C 5V Relay module and it works on 5 volt. If you connect ESP32 3.3 volts pin with relay’s VCC pin, relay will not work. Therefore, we recommend that you use VIN pin of the ESP32 Dev board to supply 5 volts to the relay VCC pin.

Controlling relay module remotely with ESP32

Now, we will see how to control the relay module to turn the lamp on and off from a web server.  We will create a web server that displays a web page with buttons (ON and OFF) along with a picture of a bulb. Web server will look similar to the web page shown below. When you press the ‘ON’ button, you will see this web page on web browser with bulb glowing representing on state.

ESP32 LAMP ON WEB SERVER

After pressing the ‘OFF’ button, you will see a web page with bulb off picture, this tells us that the lamp is currently off.

ESP32 LAMP OFF WEB SERVER

Sketch

Now copy this sketch and upload it to your ESP32 Dev board. But make sure to replace the SSID and password in the sketch according to your WiFi network’s SSID and password. Otherwise, the ESP32 will not be able to connect with the web server and your code will not work properly.

#include <WiFi.h>
#include <WebServer.h>

const char index_html1[] PROGMEM={"<!DOCTYPE html> \n<html> \n<head>\n<style>\n.button {\n background-color: #4CAF50;\n border: 2px solid #4CAF50;;\n color: white;\n padding: 15px 32px;\n text-align: center;\n text-decoration: none;\n display: inline-block;\n font-size: 16px;\n margin: 4px 2px;\n cursor: pointer;\n}\n</style>\n</head>\n<body> \n<center><h1 style=\"color:blue;\">ESP32 Web server LED controlling example</h1></center> \n<center><h2 style=\"color:black;\">Web Server Example Microcontrollerslab.com</h2></center> \n<center><h2 style=\"color:Green;\">Press \"ON\" button to turn on led and \"OFF\" button to turn off LED</h3></center>\n<center>\n<a href=\"/26/on\"><button class=\"button\">ON</button></a>\n\n<img src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAC0CAYAAABi3Il7AAASXElEQVR4Xu1da7qjIAy1a3LW5F1TXVNdk/OFE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8Xe7+7vB4XeIOiyp6kg95H6qiU1iLs1VIK6QXrATfYtdbAYkcZBzDh6DfJPJ4t4Mn6UyWOQ6QcGhzJBILywEkuN86UHn8ecEusL05AZ23KYhKOywJsL6IJqUBctnRdfR9VR3gGGelpOT4rEMj/zZMrIqbXVTKL2zvETe7pE6XVg91sIMAqvRMtUoTMClCTPr71q4JXDGvWX5Lsd7olwYASsaZN6Q5b0dEAMMLeeSu0vpFCdI5J6WaawYle3zDyRrmEd2Ixi3miwLiI5JYubHE3j6JisV8rT+IHo3jgT35MiR6L+GLifPbSZL3siuoncYSOrNGxlUE2/QegY/js4a0o3IipYCE89xl27wsRgHENIUkxIPpEO8jKEsoGPLvuS5yXovzjgu/jebqmE0xGMKpFUgTomlUEIxihF1ZoaMCaQkIr+SsHE3ubghrRuGIfzmlAcGLrEa1JTFc1YIEUqDGJdaDgF6d723HBw5HUMCQjesNl5i13e3Sp6bIcfltXqA55ARIoo3vAun+Fv7lvice383VS6m3BgcA5Xtf3//zNOfF5Utx24OSJzAurosJdWMwUYF27SqXcDT9Hw+va4NqffV8rihGQI/fFme+8qqVHDnkx5lY57IAdDZj1ixnR5J3f9kyN8+5E3JGbgsy76qqN5uVRaLu06ZydFGUgDj+Xw1fSjLFQa9BSB6U44FxKCB/5A6fgTIqKZqWLc3NrskS1RbStreJkxXDJBlWaZ17bvfI5ctb8EQ+lHzPJv+I+Y9hyPat3Iq2J0hGf73Dn+DfDjBDSYtCUQUJu4OMWR0U/V2JksCo7Y+4/4nrSHwf+wex/svgpnKaS6Qa2JqH/8f8TsYLbP4YsEAAAAASUVORK5CYII=' alt=''>\n <a href=\"/26/off\"><button class=\"button button2\">OFF</button></a>\n</center> \n</body> \n</html>"};
const char index_html2[] PROGMEM={"<!DOCTYPE html> \n<html> \n<head>\n<style>\n.button {\n background-color: #4CAF50;\n border: 2px solid #4CAF50;;\n color: white;\n padding: 15px 32px;\n text-align: center;\n text-decoration: none;\n display: inline-block;\n font-size: 16px;\n margin: 4px 2px;\n cursor: pointer;\n}\n</style>\n</head>\n<body> \n<center><h1 style=\"color:blue;\">ESP32 Web server LED controlling example</h1></center> \n<center><h2 style=\"color:black;\">Web Server Example Microcontrollerslab.com</h2></center> \n<center><h2 style=\"color:Green;\">Press \"ON\" button to turn on led and \"OFF\" button to turn off LED</h3></center> \n<center>\n<a href=\"/26/on\"><button class=\"button\">ON</button></a>\n\n<img src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAC0CAYAAABi3Il7AAASkElEQVR4Xu1da5qjOAwkZ0rOlD5TcqbOmdjPBhGh6OkXTGfnx/ZOMMaoVFWSIT2X6f8/p4rA5VSrcS7m9/d3tobebrd/8t5OvegU+BRYCgB8Zv2UQDszWKcEBADAYFjBp8cBDOk8DNaZADoFIJwEcWBYMiWBQD9Pf+cAPAMwhwJCmaBlcw+GaKzygt963HBAJD/gslj6zAIHM4D+f+Q6cG7roGvzDQWkJSO0m5LkTpIpWjwcKWdDANFMWtN3GvSIyVsssUCT1tWbLd0BkbKPM9bSgNPS2Aq2xhRrXb1lrCsgml9w/YWU1VovQstXi43WXNa6KNitGdMFEAyER7etjNXKWetcL1s0Zlj30BKU5oBEM9QjNyVB1TzEAlGqzEb4SjNAIkBYZavmJZ4y1gLQkjlrfdwaWjWVTQDxGLdV9WgVlVaJeYIfAdjyEIt5tfJVDUgNGJFMLA1q7XnSGnsBUwWIt4qSspgzUqsv8XiOh40lzIK1WUlYw5JiQPCiSm5O21C0TLc0a61AWj5IexCumsQJVQJMFSAWba0btALvnb83q7gEoMGW7jUKShgQKStKWaJpfGROa6wFvvd8qVPXFCMCShEgtTqunR/J9kiQvWzT/I6bAwdbS1YvKCFAtA7cyjApIDV+YPUTXNFQu046Jxdoble7OSCWH5RkttZ7RALnGRsBz8smSb7S51wF6gHFxRALjIh0RHoPT2AsMGoZ6PG4NIYzeU8xQM9zA2LdeIQh3FhP8OkY65qtrkM9z/ISuG4JS0xAIlWVlymerPMAZCVJq+twXuTxEpA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7c7YOCJ7O6tlF1ITn+/p18gDmcqg8FLPjuKt6twDDm7VP1zkkWBWba6Xus33vf/UgJmUZKpyC8XiEpM6/H/AXpoFmnISz9wAAAAAElFTkSuQmCC' alt=''>\n <a href=\"/26/off\"><button class=\"button button2\">OFF</button></a>\n</center>\n</body> \n</html>"};

const char* ssid = "PTCL-BB"; 
const char* password = "5387c614";

WebServer server(80);

void handleRoot() {
server.send_P(200, "text/html", index_html2);
}

void handleBulbOn()
{
server.send_P(200, "text/html", index_html1);
digitalWrite(22, HIGH);


}

void handleBulbOff()
{
server.send_P(200, "text/html", index_html2);
digitalWrite(22, LOW);


}

void handleNotFound(){
String message = "File Not Found\n\n";
server.send(404, "text/plain", message);
}

void setup(void){
Serial.begin(115200);
pinMode(22, OUTPUT);
WiFi.begin(ssid, password);
Serial.println("");

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("IP address:"); 
Serial.println(WiFi.localIP());

server.on("/", handleRoot);
server.on("/26/off",handleBulbOff);
server.on("/26/on",handleBulbOn);


server.onNotFound(handleNotFound);

server.begin();
Serial.println("HTTP server started");
}

void loop(void){
server.handleClient();
}

How to access Web Server to control lamp

  • Now after you are done with uploading the code, you need to get the IP address of ESP32. We will use this IP address to access the web server.
  • To get this IP address, open the serial monitor and click on EN/reset button of ESP32 board.
  • If everything works well, you will see this message on serial monitor along with the IP address. Note down or copy this IP address,  you will need it to access the web server.
  • Now open the web page using a dynamically allocated IP address which we got in last step. ESP32 web server will show the web page file with two buttons, picture of a bulb and some text as shown in the figure below.
control 220 volt lamp from a webserver using ESP32
  • As soon as you press the “ON” button on the web page, the lamp will light up and you will also see a picture of glowing bulb on the web page.
control 220 volt lamp from a webserver using ESP32 on state

Code detailed working

The first step in creating the sketch for the relay that can be controlled remotely involves using two libraries.

WiFi.h: Consists of the functions used to connect ESP32 to a WiFi network.

WebServer.h : This library consists of procedures which handles HTTP requests from the server and also serve HTML or CSS files to the web page.

This is a HTML part of web server with ESP32. The image is displayed using an image URL. When the user presses the ‘ON’ button, on lamp image will be shown, and otherwise off lamp image will be shown. You can show the image by converting the image into URL format. We have two html files, one file is for on state and the other html file is for off state. We added these HTML files to the code by converting it into c strings. HTML code is shown below.

HTML Page Code

<!DOCTYPE html> 
<html> 
<head>
<style>
.button {
background-color: #4CAF50;
border: 2px solid #4CAF50;;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
</style>
</head>
<body> 
<center><h1 style="color:blue;">ESP32 Web server LED controlling example</h1></center> 
<center><h2 style="color:black;">Web Server Example Microcontrollerslab.com</h2></center> 
<center><h2 style="color:Green;">Press "ON" button to turn on led and "OFF" button to turn off LED</h3></center> 
<center>
<a href="/26/on"><button class="button">ON</button></a>

<img src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAC0CAYAAABi3Il7AAASXElEQVR4Xu1da7qjIAy1a3LW5F1TXVNdk/OFEAgBFBCQPvrnzrRWLYeTk4QQH9PvNdQIPIa6m4Ob2ffXjh9v0zSt+u/R3c/TNC36gHl6PP69xW8d9iYRABj8v8pzBkACgP6G/O1D3VQ7EDimwBwAml4A0DIMg4YAZN9nzYbKZMg+HYD1vBWcWwDxmSBnbfZIVvoC3Qfqzx1mrSsgCARoAjcZlcay6mn4BAHG9NObboDs+1ODUXXkOp2snylrDgiy4l+ngWt9GTBja9Mxa3ryfV92jBk+6dVWX5oBgmCAVoyuF+WT5fGYqo9f9RN+lok6A6u+tlQF5DNN1Bko01STKVUAeR939nxwy44ApkA65rrgVwJk2uGGPlkv0oC6HrNcAuS9Y4u0IXZzXuSgHDkq11zjYkC+S7wRFtCKtElYDkoRIN8IBppkAGVTY3YOTBkohYCMkp3NNTtXj5egQBaCFstCZixfU7IB2XcQ8G9+uaDQSMQD4VdWOj8LkG+NM8LTzzdJ4XUdcIfRzKW8kg88t5kpl/u0Y3yTFJ606aAkAfKdIp46eXyTFGZKmsgnA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3//zNOfF5Utx24OSJzAurosJdWMwUYF27SqXcDT9Hw+va4NqffV8rihGQI/fFme+8qqVHDnkx5lY57IAdDZj1ixnR5J3f9kyN8+5E3JGbgsy76qqN5uVRaLu06ZydFGUgDj+Xw1fSjLFQa9BSB6U44FxKCB/5A6fgTIqKZqWLc3NrskS1RbStreJkxXDJBlWaZ17bvfI5ctb8EQ+lHzPJv+I+Y9hyPat3Iq2J0hGf73Dn+DfDjBDSYtCUQUJu4OMWR0U/V2JksCo7Y+4/4nrSHwf+wex/svgpnKaS6Qa2JqH/8f8TsYLbP4YsEAAAAASUVORK5CYII=' alt=''>
<a href="/26/off"><button class="button button2">OFF</button></a>
</body> 
</html>

Configuration of WIFI and Web Server

These two string variables are used to store WiFi name and password. Enter your WiFi name and password by replacing these placeholders.

const char* ssid = "PTCL-BB"; 
const char* password = "5387c614";

This defines the web server at port 80 which is a default port. Now you will not need to mention port number in the IP address while accessing the web server.

WebServer server(80);

Inside the setup() function,  these lines define the GPIO22 as the digital output pin and initialize the serial communication at the baud rate of 115200. The WiFi.begin() function starts the connection of ESP32 with WiFi router and upon successful connection it displays the IP address on the serial monitor.

Serial.begin(115200);

 pinMode(22, OUTPUT);

 WiFi.begin(ssid, password);

 Serial.println("");

while (WiFi.status() != WL_CONNECTED) {

    delay(500);

    Serial.print(".");

  }

  Serial.print("IP address:"); 

  Serial.println(WiFi.localIP());

When you open this IP-based URL, the ESP32 gets a request from the server. The `Server.on()` function deals with these requests. This line means that if the ESP32 gets an HTTP request on the main path (“/”), it will use the `handleroot()` function. The same goes for other requests, where specific functions are called based on what’s happening.

server.on("/", handleRoot)

server.onNotFound(handle_NotFound)

 server.on("/26/off",handleBulbOff);

 server.on("/26/on",handleBulbOn);

This way you can control a 220-volt lamp remotely from a web page using ESP32. By using this method, you can control it from an existing local area network only. If you want to control a lamp from anywhere in the world, you can check this guide on accessing ESP32 web server from anywhere in the world.

Video demo

If you are interested in ESP32 Web server projects, you can check related projects in the list below:

Leave a Comment