A relay module is an electrically operated switch that allows the ESP8266 to control high-voltage devices like lamps, fans, and home appliances using its low-voltage GPIO pins. Since ESP8266 works at 3.3V logic, it cannot directly switch high-power loads. That’s why we use a single channel relay module, which acts as a bridge between the ESP8266 and AC/DC appliances.
Relays are widely used in home automation systems, industrial control projects, and IoT applications. By connecting a relay to ESP8266, you can control electrical devices remotely via Wi-Fi, mobile apps, or IoT dashboards.
In this tutorial, we will connect a single channel relay to the ESP8266 and control it with simple Arduino code.
⚠️ Safety Note: Be very careful when connecting AC appliances to a relay. If you’re a beginner, practice with a low-voltage DC load (like an LED strip or 12V bulb) before working with AC mains.
// Single Channel Relay with ESP8266
int relayPin = D1; // Relay connected to GPIO5 (D1)
void setup() {
pinMode(relayPin, OUTPUT); // Set relay pin as output
digitalWrite(relayPin, HIGH); // Keep relay OFF initially
Serial.begin(115200);
}
void loop() {
// Turn relay ON
digitalWrite(relayPin, LOW); // Active LOW relay
Serial.println("Relay ON");
delay(2000);
// Turn relay OFF
digitalWrite(relayPin, HIGH);
Serial.println("Relay OFF");
delay(2000);
}
Relay Pin Declaration
int relayPin = D1;
We connect the relay input pin to D1 (GPIO5) of ESP8266.
Setup Function
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH);
The relay pin is set as an output.
By default, the relay is kept OFF (HIGH), since most relay modules are active LOW.
Loop Function
digitalWrite(relayPin, LOW);
delay(2000);
digitalWrite(relayPin, HIGH);
delay(2000);
Relay turns ON for 2 seconds and then OFF for 2 seconds repeatedly.
When ON, it connects COM → NO, allowing current to flow to the load.
HIGH
for ON and LOW
for OFF).Yes, ESP8266 can control a relay module using its GPIO pins. However, always use a relay module with a driver circuit for safety.
Most single channel relay modules are active LOW, meaning the relay turns ON when the GPIO output is set to LOW.
Yes, you can connect AC appliances (like bulbs or fans), but always take proper safety precautions when working with high voltage.
Yes, with ESP8266 Wi-Fi, you can control the relay via Blynk, MQTT, or Arduino IoT Cloud for remote access.
You can use GPIO5 (D1), GPIO4 (D2), or GPIO14 (D5) for stable relay operation.