The ESP32 microcontroller is a powerful and versatile tool for building IoT projects. One of its standout features is the ability to support Bluetooth communication, which opens up a world of possibilities for wireless projects. In this guide, we’ll focus on getting started with ESP32 Bluetooth Classic using the Arduino IDE, providing you with the foundational knowledge needed to implement Bluetooth communication in your own projects.
What is Bluetooth Classic?
Bluetooth Classic is a standard wireless protocol used for short-range communication between devices. Unlike Bluetooth Low Energy (BLE), Bluetooth Classic is suited for applications that require continuous, higher data rates, such as audio streaming or serial data transmission. The ESP32 supports both Bluetooth Classic and BLE, making it a flexible option for various applications.
Prerequisites
Before diving into the setup, ensure you have the following:
- An ESP32 development board
- A USB cable for programming
- The latest version of Arduino IDE installed on your computer
- Basic understanding of Arduino programming
Step 1: Install the ESP32 Board in Arduino IDE
To begin, you need to install the ESP32 board package in your Arduino IDE:
- Open Arduino IDE.
- Go to File > Preferences.
- In the “Additional Board Manager URLs” field, enter the following URL:
https://dl.espressif.com/dl/package_esp32_index.json
- Click OK.
- Next, navigate to Tools > Board > Board Manager.
- Search for “ESP32” and install the latest version of the board package.
Step 2: Set Up Bluetooth Classic on ESP32
Now that your Arduino IDE is set up for ESP32, let’s enable Bluetooth Classic:
- Open a new sketch in Arduino IDE.
- Include the necessary library by adding
#include "BluetoothSerial.h"
at the top of your sketch. - Initialize a BluetoothSerial object:
BluetoothSerial SerialBT;
- In the
setup()
function, start Bluetooth with a custom name:void setup() { Serial.begin(115200); SerialBT.begin("ESP32_BT"); // Replace "ESP32_BT" with your desired Bluetooth name Serial.println("The device started, now you can pair it with Bluetooth!"); }
- In the
loop()
function, use the following code to send and receive data via Bluetooth:void loop() { if (Serial.available()) { SerialBT.write(Serial.read()); } if (SerialBT.available()) { Serial.write(SerialBT.read()); } delay(20); }
Step 3: Upload and Test Your Code
- Connect your ESP32 to the computer using a USB cable.
- Select the correct board and port in the Arduino IDE under Tools > Board and Tools > Port.
- Click the Upload button to transfer the code to your ESP32.
- Open the Serial Monitor (Ctrl + Shift + M) in Arduino IDE to view the Bluetooth pairing status.
Step 4: Pairing with a Bluetooth Device
After uploading the code, your ESP32 will broadcast its Bluetooth name. On your smartphone or computer:
- Turn on Bluetooth and scan for new devices.
- Select the ESP32 device name (e.g., “ESP32_BT”) to pair.
- Once paired, you can open a terminal application on your smartphone or computer to send and receive data from the ESP32 via Bluetooth.
Congratulations! You’ve successfully set up Bluetooth Classic on your ESP32 using Arduino IDE. With this foundation, you can now explore a wide range of wireless projects, from simple data transmission to more complex applications like remote controls or Bluetooth-enabled sensors.