ESP32 Web Server - Control AC Appliances using Relay Module

ESP32 is an excellent microcontroller to remotely control AC appliances through a Wi-Fi network. This tutorial will explain how to control AC appliances using a relay module. We will use ESP32, relay module, Arduino IDE, and AC appliance. I will discuss how can we make an ESP32 web server to control AC Appliances using the relay module.

ESP32 Webserver Relay Module control
ESP32 Web Server Relay Control

Learn how to Installing the ESP32 Board add-on in Arduino IDE: 

Installing the ESP32 Board add-on in Arduino IDE (Windows, Linux, Mac OS)alert-info

Relay Module:

Relays are electrical switches that can be used to on or off any electrical equipment using 3.3 volt or 5-volt logic signals. The relay can control DC voltages like 5volt, 9volt, 12volt, 24volt, and AC 230volt or 120volt. But it runs in a low voltage like 3.3volt, 5volt, or 12volt. 

Different channels Relay Module:

Different channel relay modules can be found with varying switches of the relay. Some standard relay modules are 1, 2, 4, 8, 16 Channels. 

Different Relay Module

We have to select a relay module depending on the number of appliances we control. The relay modules can be powered by a 3.3volt, 5volt, 12volt, or 24volt DC power supply. Mostly magnetic relays are low power consuming. 

Relay Pinout:

Let's look at the 1 channel relay to understand the pinout of a relay module. 

Single-channel Relay

First, look at the DC connection and signal input 

The Signal Pin is the main control pin of a relay. The state of the relay depends on the signal received on this pin. The signal can be high or low.

The VCC Pin is the power input to the relay module. In our case, we can power up our relay module from the esp32 3.3volt or Vin pin. Some relays have a power isolation mechanism that isolates esp32 power to relay power.

The GND pin is the relay module's ground or negative reference pin. This pin goes to the esp32 GND pin. 

Important note: if we want to use an individual power supply for the relay module, we must use a common ground for both esp32 and relay modules. 

If there is a power isolation system in any relay module, a jumper connects two pins. The pin is called the JD-VCC pin. If we want to use different power for the relay, we have to remove the jumper and connect the power input to the JD-VCC pin of the relay module. 

Now let's look at the Main Voltage power connections.

Warning: This article will deal with the main power sources, which are very high voltage. So, if you don't have any experience in electrical connections, get help from an adult or professional person. Don't try to deal with the main AC power if you are inexperienced. While programming esp32, make sure everything is disconnected from the main voltage. alert-warning 

There are three pins in the main power panel. The pins are rated as NO COM and NC pins.

The COM pin is the main power input. This pin is also known as Common contact. 

The NO pin is also called the Normally Open pin. The pin is usually closed in the LOW signal, and the current will flow. If we set the Signal Pin to HIGH and connect the output pin to the NO pin, the current will not flow to the connected appliance.

The NC pin is also called the Normally Close pin. That means in the LOW signal, the pin usually is open, current will not flow. If we set the Signal Pin to HIGH and connect the output pin to the NC pin, the current will flow to the connected appliance.

Note: If the electrical appliance we want to control is occasionally used, we can use NO pin. The electricity will generally flow in the LOW signal. alert-info

Using ESP32 Web Server Control Multiple Relays

Using ESP32 Web Server Control Multiple Relays

Let's control multiple relays using the esp32 webserver. For this, we will need two libraries. First, download the necessary Library and install them.

Installing the Async TCP Arduino Library for ESP32

To download the Async TCP Library for Arduino IDE, click on the Download/download/button. After downloading the Async TCP Library in zip format, extract the file in your Arduino IDE installation libraries folder. Rename the folder name from AsyncTCP-master to AsyncTCP.

You can also install any Arduino library Alternatively. Go to Sketch > Include Library> Add . ZIP library… and select the Library you've just downloaded. alert-info 

Installing the ESPAsyncWebServer Arduino Library for ESP32

To download the ESPAsyncWebServer Library for Arduino IDE, click the Download/download/button. After downloading the Async TCP Library in zip format, extract the file in your Arduino IDE installation libraries folder. Rename the folder name from ESPAsyncWebServer-master to ESPAsyncWebServer.

After installing the libraries, copy the following code and upload it to the esp32 board.

Raw Code/link/button/#ff3300

Setup relay configuration

To select normally open NO or normally close NC, change the variable RELAY_NO. For normally open mode, change the variable to true. Set to false for normally closed.   

#define RELAY_NO true code-box 

Set number of relays

You can select a custom number of relays depending on your needs. Select a relay module of 1, 2, 4, 8, 16 channel relays depending on the number of AC appliances you want to control.

#define NUM_RELAYS 4 code-box 

Assign esp32 pins for relay channels

Select esp32 pins to send signals to the relay module's individual channels. The GPIO pin assignment must be the same as the number of relays defined in the NUM_RELAYS variable. In the example, I am going to use the following esp32 GPIO pins: 

int relayGPIOs[NUM_RELAYS] = {5, 18, 19, 21}; code-box

WIFI Network Settings for esp32

const char* ssid = "REPLACE_WITH_YOUR_SSID";code-box
const char* password = "REPLACE_WITH_YOUR_PASSWORD"; code-box

Video instructions


Esp32 is a powerful Wi-Fi-enabled microcontroller that is really awesome to control any AC home application remotely through Wi-Fi. We can control relay modules through an esp32 webserver. 

You can do a DIY project yourself and make an esp32 based webserver to control any home appliances using relay modules. Thanks for reading the article on "ESP32 Web Server Relay Control-Control AC Appliances using Relay Module". 

2/Post a Comment/Comments

  1. What is the maximum working range of ESP32? and how to add a display, voice control, IR control & Manual on & off switch in this circuit?

    1. The maximum ESP32 Wi-Fi range is 10km if you use a Directional Antenna. The usual range without an antenna is around 100 meters (300 ft.) for 2.4G Wi-Fi. If you want to add a display, you can use the LCD or TFT display with ESP32. Also, you can add IR remote control to control the light using the remote by using an IR receiver. And adding an on off switch is also possible. Use pull up or pull down to read digital switch state whether it's on or off. Then perform any action using the switch state in your ESP32 program.


Post a Comment