In this in-depth, step-by-step guide, I’ll show you exactly how I built a Wi-Fi-controlled smart relay module for under $5 — using cheap, readily available parts. I’ll cover schematics, wiring, code, safety, and even some tips to make it robust enough for daily use.

Are you looking for an affordable way to automate your home? With skyrocketing prices of off-the-shelf smart switches, many hobbyists and DIYers are turning to ESP32, ESP8266, or even plain microcontrollers to build their own smart home devices — at a fraction of the cost.

Let’s get started!

Advertisements

Working with electricity involves serious risk. Ensure you have the necessary skills and take proper safety precautions before attempting any electrical projects. Proceed at your own risk — the author assumes no responsibility for any damage, injury, or issues resulting from the use or misuse of the information provided.

All content on this website is original and protected by copyright. Please do not copy or reproduce content without permission. While most of the resources shared here are open-source and freely accessible for your learning and benefit, your respect for our intellectual effort is appreciated.

If you find our tutorials helpful, consider supporting us by purchasing related materials or sharing our work — it helps keep the content flowing.

Need help or have questions? Leave a comment below — the author is always happy to assist!


Why Build Your Own Smart Relay Module?

Smart Home Relay Module under $5

Smart relays let you:

✅ Switch appliances ON/OFF from your phone
✅ Automate fans, lights, pumps, and more
✅ Integrate with Google Home, Alexa, or Home Assistant
✅ Learn practical IoT skills!

But why spend $20–50 on a smart plug when you can build your own for $5 or less?


Safety First

⚠️ IMPORTANT:
Working with mains voltage is DANGEROUS. This tutorial shows the electronics part only — if you’re not experienced with safe wiring practices, get help from a certified electrician for the mains side. For testing, I recommend using a low-voltage load (like a 12V bulb) or a lamp with proper insulation.


Parts List (Under $5)

Here’s what I used:

ItemPrice (Approx.)
ESP8266 (e.g., NodeMCU)$2.00
5V Relay Module (1 channel)$1.00
Jumper Wires$0.50
PCB/Breadboard$0.50
USB cable$0.50
Misc. resistor/LED (optional)$0.50
Total:$4.50 – $5.00

✅ If you already have a USB cable and breadboard, it’s even cheaper!


How It Works

The ESP8266 connects to your home Wi-Fi.
You send a command from your phone or browser.
The ESP8266 switches the relay ON/OFF.
The relay switches your appliance.


Step 1: Wiring the Smart Relay Module

A 1-channel relay module usually has 3 control pins: VCC, GND, and IN.

Typical connections:

  • VCC → 5V
  • GND → GND
  • IN → GPIO pin

Circuit

Relay module:

  • VCC → NodeMCU 5V pin
  • GND → NodeMCU GND
  • IN → D1 (GPIO 5)

Optional LED Indicator:

  • Anode → D2 (GPIO 4) via 220Ω → GND

Step 2: Basic Relay Control Code

Smart Home Relay Module under $5

We’ll start with a simple sketch that turns the relay ON/OFF via a web browser.


Example Code:

<code>#include &lt;ESP8266WiFi.h&gt;

// Replace with your network credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";

WiFiServer server(80);

#define RELAY_PIN 5 // D1

void setup() {
  Serial.begin(115200);
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW); // Relay off by default

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected");
  Serial.println(WiFi.localIP());

  server.begin();
}

void loop() {
  WiFiClient client = server.available();
  if (!client) return;

  Serial.println("New Client.");
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();

  if (request.indexOf("/ON") != -1) {
    digitalWrite(RELAY_PIN, HIGH);
  }
  if (request.indexOf("/OFF") != -1) {
    digitalWrite(RELAY_PIN, LOW);
  }

  client.println("HTTP/1.1 200 OK");
  client.println("Content-type:text/html");
  client.println();

  client.print("&lt;h1&gt;ESP8266 Smart Relay&lt;/h1&gt;");
  client.print("&lt;p&gt;&lt;a href=\"/ON\"&gt;&lt;button&gt;ON&lt;/button&gt;&lt;/a&gt;&lt;/p&gt;");
  client.print("&lt;p&gt;&lt;a href=\"/OFF\"&gt;&lt;button&gt;OFF&lt;/button&gt;&lt;/a&gt;&lt;/p&gt;");

  client.println();
  delay(1);
  Serial.println("Client disconnected");
}
</code>

Step 3: Upload and Test

✅ Upload the sketch via Arduino IDE.
✅ Open Serial Monitor to find your ESP’s IP address.
✅ Open that IP in your browser.
✅ Click ON/OFF — you’ll hear a click as the relay toggles!


Step 4: Control from Phone

You can access the same local IP address from your phone’s browser, as long as it’s on the same Wi-Fi network.


Step 5: Making It Safer & Smarter

Let’s make this simple relay module better:

Add status feedback: Return the relay’s current state on the webpage.

Add authentication: Protect the relay from anyone toggling it on your network.

Use MQTT: Integrate with Home Assistant.

Use OTA updates: Update code wirelessly.

Use a solid-state relay: For silent, long-life switching.


Improved Example: With Status

<code>#include &lt;ESP8266WiFi.h&gt;

const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";

WiFiServer server(80);

#define RELAY_PIN 5

bool relayState = LOW;

void setup() {
  Serial.begin(115200);
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, relayState);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
  Serial.println(WiFi.localIP());

  server.begin();
}

void loop() {
  WiFiClient client = server.available();
  if (!client) return;

  String request = client.readStringUntil('\r');
  client.flush();

  if (request.indexOf("/ON") != -1) {
    relayState = HIGH;
  }
  if (request.indexOf("/OFF") != -1) {
    relayState = LOW;
  }
  digitalWrite(RELAY_PIN, relayState);

  client.println("HTTP/1.1 200 OK");
  client.println("Content-type:text/html");
  client.println();

  client.print("&lt;h1&gt;Smart Relay&lt;/h1&gt;");
  client.print("&lt;p&gt;Status: ");
  client.print(relayState ? "ON" : "OFF");
  client.println("&lt;/p&gt;");
  client.print("&lt;p&gt;&lt;a href=\"/ON\"&gt;&lt;button&gt;ON&lt;/button&gt;&lt;/a&gt;&lt;/p&gt;");
  client.print("&lt;p&gt;&lt;a href=\"/OFF\"&gt;&lt;button&gt;OFF&lt;/button&gt;&lt;/a&gt;&lt;/p&gt;");

  client.println();
}
</code>

Step 6: Enclosure Tips

  • Mount the relay on a perf board or custom PCB.
  • Use screw terminals for easy wiring.
  • Use a plastic case with proper insulation.
  • Add strain relief to cables.
  • Label input/output properly.

Integrating with Home Assistant (Optional)

The above is a stand-alone web server, but many people integrate with Home Assistant or OpenHAB via MQTT.


Basic MQTT Example:

<code>#include &lt;ESP8266WiFi.h&gt;
#include &lt;PubSubClient.h&gt;

const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
const char* mqtt_server = "BROKER_IP";

WiFiClient espClient;
PubSubClient client(espClient);

#define RELAY_PIN 5

void callback(char* topic, byte* payload, unsigned int length) {
  String msg;
  for (int i = 0; i &lt; length; i++) {
    msg += (char)payload[i];
  }
  if (msg == "ON") {
    digitalWrite(RELAY_PIN, HIGH);
  } else {
    digitalWrite(RELAY_PIN, LOW);
  }
}

void setup() {
  pinMode(RELAY_PIN, OUTPUT);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) delay(500);

  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);

  while (!client.connected()) {
    client.connect("ESP8266Client");
  }
  client.subscribe("home/relay1");
}

void loop() {
  client.loop();
}
</code>

Now, publish ON/OFF to home/relay1 from your broker, or control it via Home Assistant!


Cost Breakdown: How It’s Under $5

  • ESP8266: AliExpress bulk packs, $2–$3
  • Relay module: $1–$1.50
  • Jumper wires & USB: Usually free or leftover
  • Enclosure: 3D printed or reused box

That’s it — the whole smart switch for pocket change.


Common Pitfalls

❌ Using 5V relay with 3.3V logic — use a relay board with transistor driver.
❌ Not isolating high-voltage — keep mains wiring safe.
❌ Not using a flyback diode — good relay modules have them.
❌ Hard-coding credentials — store securely if possible.


Where To Go Next

✅ Add a physical button to toggle the relay manually.
✅ Add OTA updates to push new code wirelessly.
✅ Build multiple channels on a single NodeMCU.
✅ Add sensors: temperature, motion, etc.
✅ Integrate with Alexa or Google Assistant.

Smart Home Relay Module under $5


Conclusion

Building a smart relay module for under $5 is possible — and is a perfect way to get started with DIY home automation. You learn about hardware, IoT, web servers, MQTT, and safe AC switching basics — all with cheap, beginner-friendly parts.

Caution Reminder

Never plug high-voltage appliances into DIY modules unless you’re confident it’s safely enclosed and meets your country’s electrical code. Always test with low voltage first!

Read more:


Ready to Automate?

Give it a try — and once you’re comfortable, you’ll never look at overpriced smart plugs the same way again. Happy hacking!

Liked this article? Subscribe to our newsletter:

Loading

or,

Visit LabProjectsBD.com for more inspiring projects and tutorials.

Thank you!


MKDas

Mithun K. Das. B.Sc. in Electrical and Electronic Engineering (EEE) from KUET. Senior Embedded Systems Designer at a leading international company. Welcome to my personal blog! I share articles on various electronics topics, breaking them down into simple and easy-to-understand explanations, especially for beginners. My goal is to make learning electronics accessible and enjoyable for everyone. If you have any questions or need further assistance, feel free to reach out through the Contact Us page. Thank you for visiting, and happy learning!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *