ESP32 Relay Module – Control AC Appliances (Web Server)
Learn how to use Relay Module with ESP32 to control AC household appliances using Arduino IDE. You'll also build a web server to control your devices remotely.
- Suitable forB.Tech/Diploma IoT/ESP32 students
- Build time1-2 weeks
- LevelBeginner
- Available atAjvika Labs, Kakinada
About this project
Using a relay with the ESP32 is a great way to control AC household appliances remotely. This tutorial explains how to control a relay module with the ESP32. We’ll take a look at how a relay module works, how to connect the relay to the ESP32 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 ESP8266 board:
Guide for ESP8266 Relay Module
– Control AC Appliances + Web Server Example
Watch the following video tutorial or keep reading this page for the written instructions and all the resources.
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 ESP32 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 ESP32 – 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 ESP32 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)
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.
On the left side, there are two sets of three sockets to connect high voltages, and the pins on the right side (low-voltage) connect to the ESP32 GPIOs.
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 ESP32 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 ESP32 to close the circuit.
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 flowing
LOW signal – current is
not
flowing
Normally Open configuration (NO)
:
HIGH signal – current is
not
flowing
LOW signal – current in flowing
You 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).
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 ESP32 power pin, so the relay module and the ESP32 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 ESP32 with the module’s built-in optocoupler, which prevents damage to the ESP32 in case of electrical spikes.
Wiring a Relay Module to the ESP32
Connect the relay module to the ESP32 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 26
, you can use any other suitable GPIO. See
ESP32 GPIO Reference Guide
Controlling a Relay Module with the ESP32 – Arduino Sketch
The code to control a relay with the ESP32 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.
The following code will light up your lamp for 10 seconds and turn it off for another 10 seconds.
/*********
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*********/
const int relay = 26;
void setup() {
Serial.begin(115200);
pinMode(relay, OUTPUT);
}
void loop() {
// Normally Open configuration, send LOW signal to let current flow
// (if you're usong Normally Closed configuration send HIGH signal)
digitalWrite(relay, LOW);
Serial.println(\"Current Flowing\");
delay(5000);
// Normally Open configuration, send HIGH signal stop current flow
// (if you're usong Normally Closed configuration send LOW signal)
digitalWrite(relay, HIGH);
Serial.println(\"Current not Flowing\");
delay(5000);
}
How the Code Works
Define the pin the relay IN pin is connected to.
const int relay = 26;
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);
Control Multiple Relays with ESP32 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 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.
Installing the Web Server Libraries
We’ll build the web server using the following libraries:
ESPAsyncWebServer
by ESP32Async
AsyncTCP
by ESP32Async
You can install these libraries in the Arduino Libra
Learn how to control a relay module with ESP8266 board:
Guide for ESP8266 Relay Module
– Control AC Appliances + Web Server Example
Watch the following video tutorial or keep reading this page for the written instructions and all the resources.
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 ESP32 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 ESP32 – 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 ESP32 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)
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.
On the left side, there are two sets of three sockets to connect high voltages, and the pins on the right side (low-voltage) connect to the ESP32 GPIOs.
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 ESP32 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 ESP32 to close the circuit.
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 flowing
LOW signal – current is
not
flowing
Normally Open configuration (NO)
:
HIGH signal – current is
not
flowing
LOW signal – current in flowing
You 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).
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 ESP32 power pin, so the relay module and the ESP32 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 ESP32 with the module’s built-in optocoupler, which prevents damage to the ESP32 in case of electrical spikes.
Wiring a Relay Module to the ESP32
Connect the relay module to the ESP32 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 26
, you can use any other suitable GPIO. See
ESP32 GPIO Reference Guide
Controlling a Relay Module with the ESP32 – Arduino Sketch
The code to control a relay with the ESP32 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.
The following code will light up your lamp for 10 seconds and turn it off for another 10 seconds.
/*********
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*********/
const int relay = 26;
void setup() {
Serial.begin(115200);
pinMode(relay, OUTPUT);
}
void loop() {
// Normally Open configuration, send LOW signal to let current flow
// (if you're usong Normally Closed configuration send HIGH signal)
digitalWrite(relay, LOW);
Serial.println(\"Current Flowing\");
delay(5000);
// Normally Open configuration, send HIGH signal stop current flow
// (if you're usong Normally Closed configuration send LOW signal)
digitalWrite(relay, HIGH);
Serial.println(\"Current not Flowing\");
delay(5000);
}
How the Code Works
Define the pin the relay IN pin is connected to.
const int relay = 26;
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);
Control Multiple Relays with ESP32 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 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.
Installing the Web Server Libraries
We’ll build the web server using the following libraries:
ESPAsyncWebServer
by ESP32Async
AsyncTCP
by ESP32Async
You can install these libraries in the Arduino Libra
Technologies used
Hardware components
- ESP32
- ESP8266
- Arduino
- Relay
Software & tools
- Arduino IDE
What you get
- Working project / hardware kit, tested before delivery
- Complete source code with explanation
- Project demo and guidance for your viva
- Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed.
Payment is in two parts:
• 50% advance to start the project
• Remaining 50% before delivery
Note:
• Documentation and PPT are not included by default
• If you need documentation/PPT, it can be arranged for extra time and cost
We'll confirm the exact timeline and cost with you after reviewing your requirements.