Arduino TutorialsESP8266 Projects

How to use Bluetooth with the ESP32

Bluetooth with the ESP32

Bluetooth is ubiquitous and your ESP32 can handle it too. In this tutorial you will learn how to use Bluetooth Classic and exchange data between a smartphone and your ESP32.

For this tutorial you only need an ESP32* and an Android smartphone on which you can install the free app Serial Bluetooth Terminal.

How to make your ESP32 available in the Arduino IDE

If you have not yet programmed your ESP32 with the Arduino IDE, please do the following steps first

Open the IDE settings and enter the following link in the Additional Boards Manager URLs field.

https://dl.espressif.com/dl/package_esp32_index.json

Then open the Boards Manager under Tools/Board. Search there for ESP32 and install the latest version of the espressif Systems package of the same name. And that’s it – you should now be able to select and connect your ESP32 like any other board in the Arduino IDE.

The sketch for your ESP32

For a basic experiment with Bluetooth you only need a bit of code:

#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32test"); //Name des ESP32
  Serial.println("Der ESP32 ist bereit. Verbinde dich nun über Bluetooth.");
}

void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {
    Serial.write(SerialBT.read());
  }
  delay(25);
}

Let’s take a closer look at the most important parts: First of all, you include the library BluetoothSerial.h, which is already available in your Arduino IDE and does not need to be installed separately. Then you create an instance called SerialBT.

In the setup function you start the serial communication with a baud rate of 115200 – make sure that your serial monitor is set to this rate. Then initialize Bluetooth and give your ESP32 the name ESP32test.

There are two requests in the Loop: The first one checks if you have entered something into the Serial Monitor and sent it.

If this is the case, send the data to the ESP32:

SerialBT.write(Serial.read());

The second request is to check if the ESP32 has received anything via Bluetooth. This data is then output to the serial monitor.

A first test

So let’s move on to practice. For this test, connect an Android smartphone to your ESP32 and send messages from one device to the other.

First load the above sketch to your ESP32 as usual and start the serial monitor with a baud rate of 115200. If nothing happens, press the Reset or Enable button on ESP32. After that, after some hardware information the following sentence from your sketch should be displayed: The ESP32 is ready. Now connect via Bluetooth.

In the Bluetooth settings of your smartphone you do exactly that. You should now see your ESP32 named ESP32test and be able to connect.

Pairing ESP32 with your smartphone

If you haven’t already done so, download the Serial Bluetooth Terminal app from the Play Store and open it. Under the menu item Devices/Bluetooth Classic your ESP32 will now also appear, and you can connect to it with a tap.

Select ESP32 in the Serial Bluetooth Terminal

Time for some conversation! Write a message to your ESP32 in the app and send it. Your text is received on the other side and displayed on the serial monitor.

Send message to an ESP32

It works the same way in the other direction. Write something in the input field at the top of your serial monitor and click Send. This message will be sent from your ESP32 to your smartphone and displayed there:

Receive a message from your ESP32

And that was your first test. If you can send messages back and forth, you can of course do that with all other data.

Next, you can connect a sensor to the ESP32 and send its data to your smartphone at regular intervals.

Or the other way round, use your smartphone to switch an LED connected to your microcontroller on and off.

You may also like

Comments are closed.