Projects / ESP32 Bluetooth LED switch

ESP32 Bluetooth LED switch

An ESP32 takes text commands from a phone over classic Bluetooth serial to turn an LED on and off and report its state.

Beginner2 hGuided buildReviewed

Build it step by step

0 of 6 done
  1. ESP3218.6 mA

    Wire the LED

    GPIO26 through 330 ohms, cathode to GND.

  2. ESP32

    Upload the sketch

    The serial monitor says ready to pair.

  3. ESP3218.7 mA

    Pair your phone

    Look for ElectroLark-LED in Bluetooth settings.

  4. ESP32

    Open the terminal app

    Connect to the device.

  5. ESP3218.7 mA

    Send on and off

    The LED follows.

  6. ESP32

    Send state

    The board replies.

In short

How do you build an ESP32 Bluetooth LED switch?

An ESP32 takes text commands from a phone over classic Bluetooth serial to turn an LED on and off and report its state. You need ESP32 DevKit V1, LED 5 mm red, Resistor 330 Ω and Breadboard 830 Tie Points. It takes 6 steps and about 120 minutes.

Level
Beginner
Time
About 120 min
Parts
4

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
LED anode330 ohm resistor to ESP32 GPIO26Controlled LED
LED cathodeESP32 GNDReturn
USB cableComputer or USB powerPower

Code

Classic Bluetooth serial behaves like a wireless serial cable. The ESP32 appears to a phone as a device with a name, and once the phone is paired an app such as a Bluetooth serial terminal can send text lines to it. The program collects characters into a line, treats a newline as the end, and compares the line with a few commands. It limits the line length so a long string cannot fill memory. This works only on the original ESP32, because newer chips have Bluetooth Low Energy but not classic Bluetooth. The device name is visible to anyone nearby while it waits to be paired, so pair only your own phone, and do not use it to switch anything that matters for safety. A short, readable reply after each command, such as LED is on, makes it easy to see in the terminal that the board heard you.

Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// Works on the original ESP32 only (classic Bluetooth). Not for S2, S3, C3 or C6 boards.
#include "BluetoothSerial.h"

BluetoothSerial SerialBT;
const int LED = 26;
String line = "";

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(115200);
  SerialBT.begin("ElectroLark-LED");               // the name your phone will see
  Serial.println("Ready to pair as ElectroLark-LED");
}

void handle(String cmd) {
  cmd.trim();
  cmd.toLowerCase();
  if (cmd == "on")        { digitalWrite(LED, HIGH); SerialBT.println("LED is on"); }
  else if (cmd == "off")  { digitalWrite(LED, LOW);  SerialBT.println("LED is off"); }
  else if (cmd == "state"){ SerialBT.println(digitalRead(LED) ? "LED is on" : "LED is off"); }
  else if (cmd.length())  { SerialBT.println("Commands: on, off, state"); }
}

void loop() {
  while (SerialBT.available()) {
    char c = SerialBT.read();
    if (c == '\n' || c == '\r') { handle(line); line = ""; }
    else if (line.length() < 40) line += c;
  }
  delay(5);
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual ESP32 Dev Module with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

Ready to pair as ElectroLark-LED

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Send onLED lights and the reply says on
Send offLED turns off
Send stateReply matches the LED
Send something elseHelp text
Out of rangeConnection drops, the board keeps running

If it does not work

Device does not appear

Check the board is the original ESP32 and Bluetooth is enabled in the phone.

Cannot connect

Remove the old pairing and pair again.

No reply

Set the terminal app to send a newline at the end.

Compile error

Select ESP32 Dev Module and check Bluetooth classic is supported.

LED dark

Check polarity and GPIO26.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the ESP32 Bluetooth LED switch?

ESP32 DevKit V1, LED 5 mm red, Resistor 330 Ω and Breadboard 830 Tie Points.

How long does the ESP32 Bluetooth LED switch take to build?

About 120 minutes for a beginner maker, in 6 steps.

How do I know the ESP32 Bluetooth LED switch works?

Send on: LED lights and the reply says on. Send off: LED turns off. Send state: Reply matches the LED.

Device does not appear: what should I check?

Check the board is the original ESP32 and Bluetooth is enabled in the phone.

Cannot connect: what should I check?

Remove the old pairing and pair again.

No reply: what should I check?

Set the terminal app to send a newline at the end.

Compile error: what should I check?

Select ESP32 Dev Module and check Bluetooth classic is supported.