Introduction
The ESP32 has built-in Classic Bluetooth and Bluetooth Low Energy (BLE), making it incredibly versatile for wireless communication. In this tutorial, we use Bluetooth Serial to create a wireless terminal — allowing you to send commands from your phone to control GPIO pins.
What We Will Build
A Bluetooth-controlled LED system. Using a free Android app (Serial Bluetooth Terminal), you will type commands on your phone and the ESP32 will respond by toggling an LED.
No Extra Hardware Needed
Unlike the older HC-05/HC-06 Bluetooth modules, the ESP32 has Bluetooth built directly into the chip — saving you money and simplifying your circuit.
The Code
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
#define LED_PIN 2
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
SerialBT.begin("ESP32_BT"); // Bluetooth device name
Serial.println("Bluetooth started! Pair with 'ESP32_BT'");
}
void loop() {
if (SerialBT.available()) {
String cmd = SerialBT.readStringUntil('\n');
cmd.trim();
if (cmd == "ON") {
digitalWrite(LED_PIN, HIGH);
SerialBT.println("LED is ON");
} else if (cmd == "OFF") {
digitalWrite(LED_PIN, LOW);
SerialBT.println("LED is OFF");
} else {
SerialBT.println("Unknown command. Send ON or OFF.");
}
}
}
How to Connect
- Upload the code to your ESP32.
- On your Android phone, go to Settings > Bluetooth and pair with “ESP32_BT”.
- Download Serial Bluetooth Terminal from the Play Store.
- Connect to ESP32_BT in the app and type
ONorOFF.
Conclusion
Bluetooth Serial on the ESP32 is a powerful and cable-free way to control your projects. It is perfect for robotics, smart home gadgets, and any project where you need a quick wireless debugging terminal.