Bluetooth Communication Between the ESP32 and the Arduino

Setting up Bluetooth communication between an ESP32 and an Arduino can be a great way to wirelessly connect your projects. This guide will show you how to establish a Bluetooth connection between an ESP32 and an Arduino using the popular and widely supported Bluetooth Serial library.

The ESP32 and the Arduino Uno can communicate over Bluetooth
The ESP32 and the Arduino Uno can communicate over Bluetooth.
The ESP32 and the Arduino Uno can communicate over Bluetooth. To do this, you will need to use a Bluetooth module compatible with Arduino. We are going to use the HC-05 Bluetooth module.

Connecting Arduino Uno and HC-05 Bluetooth module

Connect the HC-05 Bluetooth module to the Arduino board. Connect the VCC, GND, RX, and TX pins of the module to the corresponding pins of the Arduino as follows:

Bluetooth%20Communication%20Between%20the%20ESP32%20and%20the%20Arduino
Arduino UNO and HC-05 Bluetooth module circuit diagram

Coding Arduino Uno and ESP32 for Bluetooth communication

Connect the ESP32 and the Arduino to your computer using the USB cable. We are going to use the SoftwareSerial library. To install the SoftwareSerial library, go to the "Sketch" menu and select "Include Library" > "SoftwareSerial."

Arduino Uno code for Bluetooth communication

Copy and upload the following Arduino code to the Arduino Uno board.

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(2,3);  // RX, TX
void setup() {
  Serial.begin(9600);
  BTSerial.begin(38400);
}
void loop() {
  if (BTSerial.available()) {
    Serial.write(BTSerial.read());
  }
  if (Serial.available()) {
    BTSerial.write(Serial.read());
  }
}code-box

ESP32 code for Bluetooth communication using Arduino IDE

On the ESP32, using the BluetoothSerial library to set up a serial connection over Bluetooth. Let's install the Bluetooth Serial library on the Arduino by going to the "Sketch" menu and selecting "Include Library" > "BluetoothSerial".

Here is the example code for the ESP32:

#include <BluetoothSerial.h>
BluetoothSerial SerialBT;
void setup() {
  SerialBT.begin("ESP32"); // give the ESP32 a name
}
void loop() {
  if (SerialBT.available()) {
    char c = SerialBT.read();
    Serial.write(c);
  }
  if (Serial.available()) {
    char c = Serial.read();
    SerialBT.write(c);
  }
}code-box

Summary

In summary, Bluetooth communication between an ESP32 and an Arduino can be established using the popular and widely supported Bluetooth Serial library on the Arduino, and the ESP32 can be configured as a Bluetooth server. An HC-05 Bluetooth module can establish a wireless connection between the ESP32 and the Arduino. To pair the HC-05 Bluetooth module with another device, you can use AT commands to change the name and password of the module, put the module in pairing mode, locate other nearby Bluetooth devices, and bind and establish a connection with the desired device. You should consult the specific documentation of your HC-05 module and consult the AT commands set that the module supports.

0/Post a Comment/Comments