Back to All Blog Posts
Tutorial β€’EltroNerd Engineering

Wi-Fi and Bluetooth Controlled Relay Switch Board using ESP32

Smart homes often rely on connected devices that can be controlled in multiple ways. In this project, we will design a relay switch board.

Wi-Fi and Bluetooth Controlled Relay Switch Board using ESP32

Smart homes often rely on connected devices that can be controlled in multiple ways. In this project, we will design a relay switch board using the ESP32 microcontroller, which allows you to control appliances through both Wi-Fi and Bluetooth.

This flexibility makes it a powerful solution for situations where sometimes Wi-Fi may not be available but you still want wireless control.


πŸ”Ή Key Idea: The ESP32 has both Wi-Fi and Bluetooth built-in.
This allows us to make a dual-control relay system without any extra modules.


πŸ› οΈ Step 1: Hardware Setup

  • Relay Module: Connect the relay input to GPIO 23 of the ESP32.
  • Power Supply: ESP32 is powered by USB or 5V adapter.
  • Load (Bulb/Fan/Appliance): Connect via the relay just like a normal switch.

Connections:

  • Relay IN β†’ GPIO23
  • Relay VCC β†’ 5V
  • Relay GND β†’ GND

πŸ—£οΈ Step 2: How It Works

  1. Wi-Fi Mode:
    The ESP32 runs a small web server. You can connect to it from your phone’s browser and press buttons to turn the relay ON/OFF.

  2. Bluetooth Mode:
    The ESP32 also listens for Bluetooth commands. You can send characters like 1 (ON) and 0 (OFF) from any Bluetooth terminal app to control the relay.


πŸ’» Step 3: Arduino Code Example

c
#include <WiFi.h>
#include <BluetoothSerial.h>

#define RELAY_PIN 23

// Wi-Fi credentials
const char* ssid = "Your_SSID";
const char* password = "Your_PASSWORD";

WiFiServer server(80);
BluetoothSerial SerialBT;

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

  // Start Wi-Fi
  WiFi.begin(ssid, password);
  Serial.print("Connecting to Wi-Fi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWi-Fi connected!");
  Serial.println(WiFi.localIP());
  server.begin();

  // Start Bluetooth
  SerialBT.begin("ESP32_Relay");
  Serial.println("Bluetooth device ready to pair");
}

void loop() {
  // --- Wi-Fi Control ---
  WiFiClient client = server.available();
  if (client) {
    String request = client.readStringUntil('\r');
    client.flush();

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

    // Send simple web page
    client.println("HTTP/1.1 200 OK");
    client.println("Content-type:text/html");
    client.println();
    client.println("<h1>ESP32 Relay Control</h1>");
    client.println("<a href=\"/ON\"><button>ON</button></a>");
    client.println("<a href=\"/OFF\"><button>OFF</button></a>");
    client.println();
    client.stop();
  }

  // --- Bluetooth Control ---
  if (SerialBT.available()) {
    char cmd = SerialBT.read();
    if (cmd == '1') {
      digitalWrite(RELAY_PIN, HIGH);
      Serial.println("Relay ON (Bluetooth)");
    }
    else if (cmd == '0') {
      digitalWrite(RELAY_PIN, LOW);
      Serial.println("Relay OFF (Bluetooth)");
    }
  }
}

Step 4: Testing

Wi-Fi: Connect your phone to the same network β†’ open browser β†’ type the ESP32’s IP β†’ tap ON/OFF buttons.

Bluetooth: Pair your phone with ESP32_Relay β†’ open any Bluetooth terminal app β†’ send 1 to turn ON, 0 to turn OFF.

🌟 Why This Project is Useful

Works even if Wi-Fi goes down (thanks to Bluetooth fallback).

Can be extended to control multiple relays for a switchboard.

Great project for students, DIY home automation, and IoT labs.

Try this project in your browser

Compile code and simulate hardware output with EltroNerd Cloud IDE.

Launch Cloud IDE