ESP32 is a microcontroller-based board that comes with Wi-Fi and Bluetooth support. It is an IoT based board that can be integrated with multiple sensors to execute instructions. ESP32 board has a wide application where wireless communication is required. Let’s discuss how we can configure ESP32 Bluetooth and transmit information using it.
ESP32 Bluetooth Classic with Arduino IDE
The ESP32 board comes with dual Bluetooth support one is Bluetooth Classic and the second one is BLE (Bluetooth Low Energy). Today we will be discussing Bluetooth Classic only. Only difference which exists between both is that Bluetooth Classic can handle a lot of data transfer but consumes battery at higher rate, however Bluetooth Low Energy is power conserving variant which is used for short distance communication. BLE remains in sleep mode until it is initialized for data transfer.
ESP32 Classic Bluetooth Serial Communication
ESP32 Bluetooth working is somehow similar to Arduino, like we did in Arduino as an external Bluetooth sensor is used like HC-05. Both Arduino and HC-05 sensors communicate over serial communication. Same is the case here with ESP32 but the difference is ESP32 comes with built-in Bluetooth modules which first receive data and then forward it to the Xtensa processor.
So, to establish this communication “BluetoothSerial” library is used which is similar to Arduino serial library, but it is just within ESP32. Following are some functions offered by Bluetooth serial library:
- begin()
- available()
- write()
- read()
Bluetooth Controlled LED using ESP32
Let’s write a simple code that can control a LED using mobile Bluetooth over Bluetooth wireless communication. Following is the hardware required to control LED using Bluetooth serial communication:
- ESP32
- LED
- Breadboard
- Android device
- Serial Bluetooth Terminal Application
Circuit
Connect LED at digital pin 15 of ESP32 with negative terminal connected at GND of ESP32 board. For a safe current limit, we can also connect the resistor (220 ohms) between them:
Code
Open Arduino IDE and select the ESP32 board in the Board Manager to see how to install ESP32 board in Arduino IDE click here. After selecting the board write the code below in the editor window:
#define LED_PIN 15 /*led pin initialized*/
BluetoothSerial SerialBT;
byte BT_INP;
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)/*Check for bluetooth in SDK*/
#error Bluetooth off--Run `make menuconfig` to enable it
#endif
void setup()
{
pinMode(LED_PIN, OUTPUT); /*led pin set as output*/
Serial.begin(115200); /*baud rate for serial communication*/
SerialBT.begin(); /*Bluetooth communication begins*/
Serial.println("Bluetooth is ready to Pair..."); /*when Bluetooth turn on*/
}
void loop()
{
if(SerialBT.available()) /*check for Bluetooth data availability*/
{
BT_INP = SerialBT.read(); /*read Bluetooth data from device*/
Serial.write(BT_INP); /*print the read data*/
}
if(BT_INP == '1') /*if condition for led state*/
{
digitalWrite(LED_PIN, HIGH); /*turn on led if 1 input is received*/
}
if(BT_INP == '0')
{
digitalWrite(LED_PIN, LOW);/*turn off led if 0 input is received*/
}
}
Here in the above code, we started by including the Bluetooth serial library for ESP32. Next we have included Bluetooth serial library functions that will enable the ESP32 Bluetooth.
Next LED pin 15 is initialized and using the pinMode() function LED pin is set as output.
In loop part of the code the program will check for serial Bluetooth data availability. If the input data is 1 LED will turn ON and if the received data is 0 LED will turn off.
Once the code is uploaded. Bluetooth of the ESP32 board will turn on and the following message will appear on the serial monitor.
Installing Serial Bluetooth Terminal
We need a Bluetooth device that can send instructions to ESP32 so we will be using an Android smartphone to interface it with ESP32 Bluetooth. First, we need to install a serial terminal in an Android phone. Follow the steps given below to interface Android phone with ESP32:
Step 1: Open Google Play Store on your smartphone and search Serial Bluetooth Terminal. Install the below shown application:
Step 2: After installing open mobile phone Bluetooth settings. Search for ESP32 Bluetooth and click to start pairing it with your smartphone by clicking on Pair:
Step 3: After tapping on a Pair, the mobile phone will start pairing with ESP32 Bluetooth:
Step 4: Now open the Serial Bluetooth Terminal Application and go to Devices from the side menu:
Step 5: Once the device option is opened it will ask for some permissions or press the REFRESH button at top right corner:
Step 6: Following popup will come click on Settings and allow the permission it asks for:
Step 7: Now ESP32 board is ready to take instructions over Bluetooth. Under Bluetooth Classic option select ESP32 board:
Step 8: Once ESP32 is selected it will start connecting and if successful, a Connected message will appear.
Step 9: Now we can send any instruction by typing it here. Type 1 and click the send button, LED on ESP32 board will turn On. Similarly, by typing 0 LED will turn OFF.
Similarly, we can see the output on the serial monitor of the Arduino IDE what it is receiving:
Output:
LED turns ON after sending 1:
LED turns OFF after sending 0:
Note: We can also configure buttons for specific instructions like shown in image below. To do this click the buttons and set the value you want to. Here we have set two buttons one for HIGH and other one for LOW state. You can also configure these shortcuts in Hexadecimal values.
Conclusion
ESP32 boards have on board WiFi and dual Bluetooth support with Classic Bluetooth and Low Energy Bluetooth. Classic is used for high data transfer while BLE (Bluetooth Low Energy) is used for short distances with less power requirements. This article covers the Classic Bluetooth data transfer and gives an idea of how Bluetooth communication is done using the ESP32 board.