ESP32 Web Server - Control AC Appliances using Relay Module

ESP32 is a really amazing microcontroller to control AC appliances remotely through a Wi-Fi network. In this tutorial, I am going to explain how to control AC appliances using a relay module. We are going to use ESP32, relay module, Arduino IDE, and any AC appliance. I will discuss how can we make an ESP32 webserver 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 also 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 a different number of relay switches. Some common 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 want to 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 have a 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. In some relays, there is a power isolation mechanism that isolates esp32 power to relay power.

The GND Pin is the ground or negative reference pin for the relay module. 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: In this article, we are going to 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 an inexperienced person. 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. That means in the LOW signal, the pin is normally closed, 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 is normally 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 flow normally 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 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 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. 

Now you can make a DIY project by 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". 

0/Post a Comment/Comments