Switch a lamp or any small load from your phone with an ESP32 BLE relay switch: no Wi-Fi router or internet needed, a local push button for manual control, and live ON/OFF feedback in a free BLE terminal app.
Need it ready-made? Buy Ready Project
Most phone-controlled switches need a Wi-Fi router, an internet connection and a cloud app. Sometimes you just want to switch a lamp, a fan or a pump from your phone while you are standing in the same room, even where there is no Wi-Fi at all. This ESP32 BLE relay switch uses the Bluetooth Low Energy radio that is already built into every ESP32, so the phone talks directly to the board.
You send simple text commands such as ON, OFF or T (toggle) from a free BLE terminal app, and the ESP32 replies with the new state, so you always know whether the load is really on. A push button next to the relay works as a normal manual switch, and the phone is updated when you press it. It is a great first BLE project and a building block for bigger home automation builds.

How It Works
- BLE UART service: the ESP32 advertises as APH-BLE-Relay and offers the Nordic UART Service (NUS). This is a standard “serial port over BLE” that most terminal apps understand, so you do not need to build your own app.
- RX characteristic: text written by the phone lands in the RX characteristic. The callback cleans it up (trims spaces, converts to upper case) and hands it to
loop(), so the relay is never switched from inside the Bluetooth task. - TX characteristic: after every change the ESP32 sends
RELAY:ONorRELAY:OFFas a notification. SendSat any time to ask for the current state. - Push button: the button on GPIO27 uses the internal pull-up. A 50 ms debounce stops one press from toggling the relay twice.
- Connection LED: the on-board blue LED on GPIO2 is on while a phone is connected. When the phone disconnects, the ESP32 starts advertising again so you can reconnect without resetting it.
- Safe start: the relay is always OFF when the board powers up or resets.
Components Required
- ESP32 DevKit V1 (ESP-WROOM-32) – 1
- 5V single-channel relay module (active-LOW input, with optocoupler) – 1
- Tactile push button – 1
- Breadboard and jumper wires – 1 set
- 5V USB power supply or USB cable – 1
- Small lamp, fan or DC load for testing (use a 12V DC load first) – 1
- Android or iPhone with a BLE terminal app (Serial Bluetooth Terminal / Bluefruit Connect) – 1
Wiring Table
| Module pin | ESP32 pin | Notes |
|---|---|---|
| Relay VCC | VIN (5V from USB) | Relay coil needs 5V |
| Relay GND | GND | Common ground |
| Relay IN | GPIO26 | Active-LOW: LOW = relay ON (change RELAY_ACTIVE_LOW if yours is active-HIGH) |
| Push button leg 1 | GPIO27 | Internal pull-up, no resistor needed |
| Push button leg 2 | GND | Pressed = LOW |
| Status LED | GPIO2 (on-board) | Already on the DevKit board |
| Relay COM / NO | Supply + / load | Load ON when relay is energised |
Connection Diagram
The illustration above shows a typical breadboard layout and is a layout guide only. Follow the pin-accurate drawing below, which matches the wiring table and the code.
Code
Install the esp32 board package (version 3.x) in the Arduino IDE. The BLE library is included with it, so no extra library is needed. Select ESP32 Dev Module, choose the correct COM port and upload. The sketch compiles with ESP32 Arduino core 3.3 and uses about 1.1 MB of flash, which fits the default partition scheme.
/*
ESP32 BLE Relay Switch with Push Button & Status LED
Arduino Project Hub - https://ardiunoprojecthub.online
Control one relay from your phone over Bluetooth Low Energy (BLE).
The ESP32 acts as a "BLE UART" (Nordic UART Service), so any BLE terminal
app (for example "Serial Bluetooth Terminal" on Android or "Bluefruit Connect"
on iPhone) can send text commands:
ON -> relay ON
OFF -> relay OFF
T -> toggle relay
S -> ask for the current state
A local push button also toggles the relay, and every change is sent back
to the phone as a notification ("RELAY:ON" / "RELAY:OFF").
Board: ESP32 Dev Module (ESP32 Arduino core 3.x, built-in BLE library)
*/
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
// ----- Pins -----
const int RELAY_PIN = 26; // relay module IN
const int BUTTON_PIN = 27; // push button to GND (internal pull-up)
const int LED_PIN = 2; // on-board blue LED = BLE connected
const bool RELAY_ACTIVE_LOW = true; // most blue relay modules switch ON with LOW
// ----- Nordic UART Service UUIDs (understood by most BLE terminal apps) -----
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
#define RX_UUID "6E400002-B5A3-F393-E0A9-E50E24DCCA9E" // phone -> ESP32
#define TX_UUID "6E400003-B5A3-F393-E0A9-E50E24DCCA9E" // ESP32 -> phone
BLECharacteristic *txChar = nullptr;
volatile bool deviceConnected = false;
bool lastConnected = false;
bool relayOn = false;
volatile bool commandWaiting = false;
String pendingCommand = "";
// Drive the relay and remember the state
void setRelay(bool on) {
relayOn = on;
bool level = RELAY_ACTIVE_LOW ? !on : on;
digitalWrite(RELAY_PIN, level ? HIGH : LOW);
Serial.printf("Relay %s\n", on ? "ON" : "OFF");
}
// Send the current state to the phone (if connected)
void sendState() {
if (deviceConnected && txChar != nullptr) {
String msg = relayOn ? "RELAY:ON\n" : "RELAY:OFF\n";
txChar->setValue(msg.c_str());
txChar->notify();
}
}
// Connection events
class ServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer *server) override {
deviceConnected = true;
}
void onDisconnect(BLEServer *server) override {
deviceConnected = false;
}
};
// Text written by the phone arrives here
class RxCallbacks : public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *ch) override {
String value = ch->getValue(); // String in ESP32 core 3.x
value.trim();
value.toUpperCase();
if (value.length() > 0) {
pendingCommand = value; // handled in loop(), not inside the BLE task
commandWaiting = true;
}
}
};
void handleCommand(const String &cmd) {
if (cmd == "ON" || cmd == "1") setRelay(true);
else if (cmd == "OFF" || cmd == "0") setRelay(false);
else if (cmd == "T" || cmd == "TOGGLE") setRelay(!relayOn);
else if (cmd != "S" && cmd != "STATUS") {
if (deviceConnected) {
txChar->setValue("Use ON, OFF, T or S\n");
txChar->notify();
}
return;
}
sendState();
}
void setup() {
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
setRelay(false); // start with the load OFF
BLEDevice::init("APH-BLE-Relay"); // name shown in the phone app
BLEServer *server = BLEDevice::createServer();
server->setCallbacks(new ServerCallbacks());
BLEService *service = server->createService(SERVICE_UUID);
txChar = service->createCharacteristic(TX_UUID, BLECharacteristic::PROPERTY_NOTIFY);
txChar->addDescriptor(new BLE2902());
BLECharacteristic *rxChar = service->createCharacteristic(
RX_UUID, BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_WRITE_NR);
rxChar->setCallbacks(new RxCallbacks());
service->start();
BLEAdvertising *adv = BLEDevice::getAdvertising();
adv->addServiceUUID(SERVICE_UUID);
adv->setScanResponse(true);
BLEDevice::startAdvertising();
Serial.println("Advertising as APH-BLE-Relay. Connect with a BLE terminal app.");
}
void loop() {
// 1) Commands from the phone
if (commandWaiting) {
commandWaiting = false;
handleCommand(pendingCommand);
}
// 2) Local push button with simple debounce (toggle on press)
static bool lastButton = HIGH;
static unsigned long lastChange = 0;
bool reading = digitalRead(BUTTON_PIN);
if (reading != lastButton && millis() - lastChange > 50) {
lastChange = millis();
lastButton = reading;
if (reading == LOW) { // pressed
setRelay(!relayOn);
sendState();
}
}
// 3) Connection LED and restart advertising after a disconnect
digitalWrite(LED_PIN, deviceConnected ? HIGH : LOW);
if (deviceConnected && !lastConnected) {
delay(300); // let the app subscribe first
sendState();
}
if (!deviceConnected && lastConnected) {
delay(300);
BLEDevice::startAdvertising();
Serial.println("Disconnected - advertising again");
}
lastConnected = deviceConnected;
delay(5);
}
Connecting from Your Phone
- Android: install Serial Bluetooth Terminal, open Devices → Bluetooth LE, scan and tap APH-BLE-Relay. You do not pair it in the phone’s Bluetooth settings.
- iPhone: install Bluefruit Connect (or any NUS-compatible app), connect to APH-BLE-Relay and open the UART screen.
- Type
ONand send: the relay clicks and the app showsRELAY:ON. SendOFF,TorSto test the other commands. - Tip: in Serial Bluetooth Terminal you can save
ONandOFFas macro buttons for one-tap control.
Troubleshooting
| Problem | Fix |
|---|---|
| APH-BLE-Relay does not appear in the scan | Scan from inside the app, not the phone’s Bluetooth settings. Turn on Location on Android (needed for BLE scans) and check the Serial Monitor shows “Advertising”. |
| Relay works the opposite way | Your module is active-HIGH. Set RELAY_ACTIVE_LOW = false in the code. |
| Relay clicks but ESP32 resets | The relay coil is pulling the USB supply down. Use a good 5V 1A supply and a module with an optocoupler, and keep the load wiring away from the ESP32. |
| Commands are ignored | Send plain text with a newline or nothing after it; the code trims it. Check that the app is writing to the RX characteristic (6E400002…). |
| No RELAY:ON reply in the app | Enable notifications on the TX characteristic (6E400003…). Most terminal apps do this automatically after connecting. |
| Cannot reconnect after leaving the room | Wait 2–3 seconds; the ESP32 restarts advertising after a disconnect. Close and reopen the app if it still shows the old connection. |
| Upload fails with “Sketch too big” | Choose a partition scheme with at least 1.2 MB app space (the default one works) under Tools → Partition Scheme. |
Ideas to Extend
- Add three more relays and commands like
R2 ONfor a 4-channel BLE switch board. - Add an auto-off timer command such as
TIMER 30to switch the load off after 30 minutes. - Save the last state in Preferences so the relay returns to it after a power cut.