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. To do this, you must 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:
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, use 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.
What are the benefits of using Bluetooth communication between the ESP32 and the Arduino? Cause we can stablish Wi-Fi communication, serial communication between esp32 and Arduino.
ReplyDeleteThere are several benefits of using Bluetooth communication between the ESP32 and the Arduino:
DeleteConvenience: Bluetooth communication allows the ESP32 and the Arduino to wirelessly communicate with each other, eliminating the need for physical connections.
Flexibility: Bluetooth communication provides the ability to add and remove devices from the network as needed, allowing for greater flexibility in project design.
Cost-effective: Bluetooth communication is less expensive compared to other wireless communication technologies such as Wi-Fi or Zigbee.
Portability: Bluetooth communication enables the ESP32 and the Arduino to be portable, making it easy to move the devices around without having to disconnect and reconnect them.
Low Power Consumption: Bluetooth communication uses very low power, making it ideal for battery-powered projects.
Compatibility: Bluetooth communication is widely supported and compatible with many different devices and platforms, making it a popular choice for IoT and wearable applications.
Easy to Use: Bluetooth communication is easy to set up and use, even for those with limited technical skills. The Bluetooth Serial library in the Arduino and the built-in Bluetooth capabilities of the ESP32 make it simple to establish a connection between the two devices.
How do I send data over Bluetooth between the ESP32 and an Arduino?
ReplyDeleteTo send data over Bluetooth between the ESP32 and an Arduino, you can use the Serial.write() and Serial.read() functions on the Arduino side and the BluetoothSerial.write() and BluetoothSerial.read() functions on the ESP32 side. Using the standard serial communication protocol, these functions allow you to send and receive data over a Bluetooth connection.
DeleteThis did not work. Is the Mac address needed to bind the ESP32 and HC05 on the UNO?
ReplyDeleteIn a typical setup involving an HC-05 Bluetooth module with an Arduino Uno and an ESP32, the MAC address is not typically needed to establish a basic Bluetooth connection. Instead, the HC-05 module relies on its name and PIN (password) for the pairing process. Initially, you configure the HC-05 module by setting its name and PIN using AT commands, such as naming it "HC-05" and assigning a PIN like "1234". Once you put the HC-05 module into pairing mode with an appropriate AT command, it becomes discoverable by other Bluetooth devices. When your ESP32 or another device discovers the HC-05 module (based on the name you've assigned), you initiate the pairing process and, if required, enter the PIN. The MAC address is typically used for more advanced Bluetooth features like specific device identification and security, rather than for basic communication and pairing.
DeleteYou can retrieve the HC-05 module's MAC address using AT commands if you have a particular use case that necessitates it, such as filtering or identifying devices by their MAC addresses.
thank you for the article. very helpful to increase my knowledge
ReplyDeletePost a Comment