Jump to content

ESP32 Bluetooth: Difference between revisions

From cognifront
CogWikiEditor (talk | contribs)
No edit summary
CogWikiEditor (talk | contribs)
No edit summary
Line 1: Line 1:
===Interfacing LM35 Temperature Sensor, LED and Buzzer with ESP32 using Bluetooth===
===ESP32 Bluetooth Communication===


'''Interfacing LM35 Temperature Sensor, LED and Buzzer with ESP32 using Bluetooth''' demonstrates how an LM35 temperature sensor, LED and buzzer can be interfaced with an ESP32 board and controlled or monitored using Bluetooth commands.
'''ESP32 Bluetooth Communication''' demonstrates how the built-in Bluetooth functionality of the ESP32 can be used for wireless communication between the ESP32 board and a Bluetooth-enabled device such as a smartphone, tablet, or computer.


The ESP32 receives commands through Bluetooth and performs the corresponding operation such as controlling the LED, reading temperature, checking system status, blinking the LED and controlling the buzzer.
The ESP32 can send and receive data wirelessly using Bluetooth without requiring a physical serial connection.


<center>
<center>
'''Bluetooth Device → ESP32 → LED / LM35 / Buzzer'''
'''Bluetooth Device → Wireless Communication → ESP32'''
</center>
</center>


== Objectives ==
== Objectives ==


* To study the working of the LM35 temperature sensor.
* To study the Bluetooth functionality available in the ESP32.
* To interface the LM35 temperature sensor with the ESP32.
* To understand how to initialize Bluetooth communication.
* To interface an LED and buzzer with the ESP32.
* To pair the ESP32 with a Bluetooth-enabled device.
* To control the LED and buzzer using Bluetooth commands.
* To send data from the ESP32 to a Bluetooth device.
* To read temperature from the LM35 using the ESP32 ADC.
* To receive commands or data from a Bluetooth device.
* To transmit temperature and status information through Bluetooth.
* To control ESP32 peripherals using Bluetooth commands.


== Software ==
== Software ==


* Arduino IDE
* Arduino IDE
* ESP32 Board Package
* ESP32 board package
* BluetoothSerial Library
* Bluetooth-enabled smartphone, tablet, or computer


== Hardware Modules ==
== Hardware Modules ==


* ESP32 Board
* ESP32 Development Board
* LM35 Temperature Sensor
* Buzzer
* LED
* PC / Laptop
* PC / Laptop
* Bluetooth-enabled device
* Smartphone / Bluetooth-enabled device
* Connecting wires
* External sensors or actuators as required


== Theory ==
== Theory ==


=== Introduction to LM35 ===
'''Bluetooth''' is a wireless communication technology used for exchanging data between electronic devices over short distances.


The '''LM35''' is an analog temperature sensor used to measure temperature.
The ESP32 contains built-in Bluetooth hardware, allowing it to communicate with compatible Bluetooth devices without requiring an external Bluetooth module.


The output voltage of the LM35 changes according to the temperature.
The ESP32 can be programmed to operate as a Bluetooth device with a user-defined device name.


The LM35 provides an output of approximately '''10 mV per °C'''.
<center>
'''ESP32 Bluetooth → Wireless Data Transmission → Smartphone / PC'''
</center>
 
Bluetooth communication can be used for:
 
* Sending sensor readings
* Receiving control commands
* Controlling LEDs
* Controlling buzzers
* Monitoring system status
* Sending data to a mobile application
* Wireless serial communication
 
== ESP32 Bluetooth ==


The temperature can therefore be calculated from the sensor output voltage.
The ESP32 provides built-in Bluetooth functionality.


<center>
For Bluetooth Classic communication in the Arduino environment, the '''BluetoothSerial''' library can be used.
'''Temperature → LM35 → Analog Voltage → ESP32 ADC'''
</center>


The ESP32 reads the analog voltage from the LM35 using an analog input pin.
The library provides a serial-like interface for sending and receiving data wirelessly.


In this practical, the LM35 output is connected to:
The basic sequence is:


<center>
<center>
'''GPIO 34'''
'''Initialize Bluetooth → Set Device Name → Pair Device → Send/Receive Data'''
</center>
</center>


== LM35 Connections ==
== BluetoothSerial Library ==


{| class="wikitable"
The '''BluetoothSerial''' library provides functions for Bluetooth Classic communication.
! LM35 Pin
! ESP32
! Function
|-
| VCC
| 3.3V
| Power supply
|-
| OUT
| GPIO 34
| Analog temperature output
|-
| GND
| GND
| Ground
|}


== LM35 Temperature Calculation ==
The library is included using:
 
The ESP32 reads the analog value from the LM35 using:


<pre>
<pre>
analogRead(LM35_PIN);
#include "BluetoothSerial.h"
</pre>
</pre>


The ESP32 ADC value is converted into voltage using:
A Bluetooth serial object is then created:


<pre>
<pre>
float voltage = adcValue * (3.3 / 4095.0);
BluetoothSerial SerialBT;
</pre>
</pre>


The LM35 produces approximately 10 mV per °C.
Bluetooth communication is started using:
 
Therefore, the temperature is calculated using:


<pre>
<pre>
float temperatureC = voltage * 100.0;
SerialBT.begin("ESP32_Demo");
</pre>
</pre>


The calculated temperature is returned by the '''readTemperature()''' function.
Here, '''ESP32_Demo''' is the Bluetooth device name that appears when searching for the ESP32 from another Bluetooth device.


<center>
== Bluetooth Communication ==
'''ADC Value → Voltage → Temperature in °C'''
</center>


== ESP32 ADC ==
Bluetooth communication works in two directions.


The program uses GPIO 34 as the analog input for the LM35.
=== Sending Data ===


The calculation in the program uses a '''12-bit ADC''' range:
The ESP32 can send data to a connected Bluetooth device using:


<center>
<pre>
'''0 to 4095'''
SerialBT.print("Hello");
</center>
SerialBT.println("ESP32");
</pre>


The reference voltage used in the calculation is '''3.3 V'''.
The '''print()''' function sends data without automatically adding a new line.


The relationship used by the program is:
The '''println()''' function sends data followed by a new line.


<center>
<center>
'''Voltage = ADC Value × (3.3 / 4095)'''
'''ESP32 → Bluetooth → Smartphone / PC'''
</center>
</center>


The LM35 temperature is then calculated as:
=== Receiving Data ===


<center>
The ESP32 can check whether data has been received using:
'''Temperature (°C) = Voltage × 100'''
</center>


== LED Interface ==
<pre>
SerialBT.available()
</pre>


The built-in LED of the ESP32 is connected to:
Received characters can be read using:


<center>
<pre>
'''GPIO 2'''
char c = SerialBT.read();
</center>
</pre>
 
The LED is controlled through Bluetooth commands.
 
The following commands are used:
 
{| class="wikitable"
! Bluetooth Command
! Function
|-
| ON
| Turn LED ON
|-
| OFF
| Turn LED OFF
|-
| BLINK
| Blink LED 5 times
|}
 
When the '''ON''' command is received, GPIO 2 is made HIGH.
 
When the '''OFF''' command is received, GPIO 2 is made LOW.
 
== Buzzer Interface ==
 
The buzzer is connected to:
 
<center>
'''GPIO 25'''
</center>
 
The buzzer can be controlled using Bluetooth commands.
 
{| class="wikitable"
! Bluetooth Command
! Function
|-
| BUZZON
| Turn buzzer ON
|-
| BUZZOFF
| Turn buzzer OFF
|}


The buzzer is also automatically activated when the temperature measured by the LM35 is greater than '''35 °C'''.
The received characters can then be processed as commands or sensor-control instructions.


<center>
<center>
'''Temperature > 35 °C → Buzzer ON → High Temperature Warning'''
'''Smartphone / PC → Bluetooth → ESP32'''
</center>
</center>


== Bluetooth Communication ==
== Bluetooth Device Name ==
 
The ESP32 uses the '''BluetoothSerial''' library for Bluetooth communication.
 
The following library is included:


<pre>
The ESP32 Bluetooth device name can be specified while starting Bluetooth.
#include "BluetoothSerial.h"
</pre>


A Bluetooth Serial object is created using:
Example:
 
<pre>
BluetoothSerial SerialBT;
</pre>
 
Bluetooth is initialized using:


<pre>
<pre>
Line 209: Line 142:
</pre>
</pre>


Therefore, the ESP32 Bluetooth device appears with the name:
After Bluetooth is started, the device appears as:


<center>
'''ESP32_Demo'''
'''ESP32_Demo'''
</center>


A Bluetooth-enabled device can pair with the ESP32 and send commands.
on the Bluetooth device used for pairing.


== Bluetooth Commands ==
The name can be changed as required.


The program supports the following commands:
For example:
 
{| class="wikitable"
! Command
! Operation
|-
| ON
| Turn LED ON
|-
| OFF
| Turn LED OFF
|-
| TEMP
| Read and transmit LM35 temperature
|-
| STATUS
| Check ESP32 status
|-
| BLINK
| Blink LED 5 times
|-
| BUZZON
| Turn buzzer ON
|-
| BUZZOFF
| Turn buzzer OFF
|}
 
Commands are received as text through Bluetooth.
 
The received command is converted to uppercase using:


<pre>
<pre>
incomingData.toUpperCase();
SerialBT.begin("MyESP32");
</pre>
</pre>


This allows the program to compare the received command with the predefined commands.
== Connections ==


== Connections ==
Bluetooth communication itself does not require external communication wires between the ESP32 and the smartphone or computer.


{| class="wikitable"
{| class="wikitable"
! Device
! Device
! Pin
! Connection
! ESP32
! Function
! Function
|-
|-
| LM35
| ESP32
| VCC
| Built-in Bluetooth
| 3.3V
| Wireless communication
| Power supply
|-
| LM35
| OUT
| GPIO 34
| Analog temperature input
|-
| LM35
| GND
| GND
| Ground
|-
| Buzzer
| +
| GPIO 25
| Buzzer control
|-
| Buzzer
| −
| GND
| Ground
|-
|-
| Built-in LED
| Smartphone / PC
| LED
| Bluetooth
| GPIO 2
| Wireless communication
| LED control
|}
|}
External sensors and actuators can be connected to the ESP32 separately depending on the application.
== Basic Bluetooth Communication Flow ==
The Bluetooth communication process can be summarized as:
# Start the Arduino IDE.
# Include the '''BluetoothSerial''' library.
# Create a Bluetooth serial object.
# Start Bluetooth using '''SerialBT.begin()'''.
# Assign a name to the ESP32 Bluetooth device.
# Search for the ESP32 using a Bluetooth-enabled device.
# Pair/connect the device with the ESP32.
# Send data from the ESP32 using '''SerialBT.print()''' or '''SerialBT.println()'''.
# Check for incoming data using '''SerialBT.available()'''.
# Read incoming data using '''SerialBT.read()'''.
# Process the received data according to the application.


== Algorithm ==
== Algorithm ==
The following algorithm is used for interfacing the LM35, LED and buzzer with the ESP32 and controlling them through Bluetooth:


# Start the Arduino IDE.
# Start the Arduino IDE.
# Include the '''BluetoothSerial''' library.
# Include the '''BluetoothSerial''' library.
# Declare GPIO 2 for the LED.
# Create a '''BluetoothSerial''' object.
# Declare GPIO 34 for the LM35 analog input.
# Start Bluetooth communication with a suitable device name.
# Declare GPIO 25 for the buzzer.
# Display the Bluetooth status on the Serial Monitor.
# Create a Bluetooth Serial object.
# Wait for a Bluetooth device to connect.
# In the '''void setup()''' function, configure the LED and buzzer pins as '''OUTPUT'''.
# Check whether data is available from the Bluetooth device.
# Set the initial state of the LED and buzzer to LOW.
# Read the received data.
# Start Serial communication at '''115200 baud'''.
# Process the received command or data.
# Start Bluetooth using the device name '''ESP32_Demo'''.
# Send a response using Bluetooth.
# Display the available commands through Bluetooth.
# In the '''void loop()''' function, check whether Bluetooth data is available.
# Read the received characters.
# When a newline character is received, process the complete command.
# Remove unnecessary spaces from the command.
# Convert the command to uppercase.
# If the command is '''ON''', turn the LED ON.
# If the command is '''OFF''', turn the LED OFF.
# If the command is '''TEMP''', read the LM35 temperature and transmit it through Bluetooth.
# If the temperature is greater than '''35 °C''', turn the buzzer ON and transmit a warning.
# If the command is '''STATUS''', transmit the ESP32 status.
# If the command is '''BLINK''', blink the LED five times.
# If the command is '''BUZZON''', turn the buzzer ON.
# If the command is '''BUZZOFF''', turn the buzzer OFF.
# If an unknown command is received, transmit '''Unknown Command'''.
# Repeat the process continuously.
# Repeat the process continuously.
The overall operation is:


<center>
<center>
'''Bluetooth Command → ESP32 → Required Operation'''
'''Start → Initialize Bluetooth → Connect Device → Send/Receive Data → Process Data → Repeat'''
</center>
</center>


== Working ==
== Working ==


The ESP32 starts Bluetooth communication using the device name '''ESP32_Demo'''.
When the ESP32 program starts, the Bluetooth interface is initialized using the '''BluetoothSerial''' library.


A Bluetooth-enabled device can send commands to the ESP32.
The ESP32 becomes discoverable using the Bluetooth device name specified in the program.


The ESP32 receives the command and checks which operation has been requested.
After pairing or connecting a smartphone or computer, data can be exchanged wirelessly.


For LED control:
The ESP32 can send messages using:


<center>
<pre>
'''ON → LED ON'''
SerialBT.println("Hello from ESP32");
</center>
</pre>
 
<center>
'''OFF → LED OFF'''
</center>


For temperature measurement:
It can also receive data from the connected Bluetooth device.


<center>
For example, a smartphone can send:
'''TEMP → LM35 Reading → Temperature → Bluetooth Output'''
</center>
 
For buzzer control:
 
<center>
'''BUZZON → Buzzer ON'''
</center>
 
<center>
'''BUZZOFF → Buzzer OFF'''
</center>
 
The '''BLINK''' command makes the LED blink five times.
 
The '''STATUS''' command sends the message:


<pre>
<pre>
ESP32 Running OK
ON
</pre>
</pre>


== Temperature Alert ==
The ESP32 can receive this command and use it to control an output device such as an LED.
 
When the '''TEMP''' command is received, the ESP32 reads the LM35 sensor.


The temperature is calculated and transmitted through Bluetooth.
Similarly, a command such as:
 
If the temperature is greater than '''35 °C''', the buzzer is automatically switched ON.
 
The Bluetooth device receives:


<pre>
<pre>
WARNING: HIGH TEMPERATURE!
OFF
</pre>
</pre>


If the temperature is not greater than 35 °C, the buzzer is switched OFF.
can be used to turn the LED OFF.
 
The operation can be represented as:


<center>
<center>
'''TEMP → Read LM35 → Calculate Temperature → Temperature > 35 °C? → Buzzer Alert'''
'''Bluetooth Command → ESP32 → Program Processing → Output Control'''
</center>
</center>


== LED Blinking ==
== Example Program ==
 
When the '''BLINK''' command is received, the ESP32 blinks the built-in LED five times.


Each blink consists of:
The following program demonstrates basic Bluetooth communication with the ESP32.
 
<center>
'''LED HIGH → 300 ms → LED LOW → 300 ms'''
</center>
 
This sequence is repeated '''5 times'''.
 
After completing the sequence, the ESP32 transmits:
 
<pre>
Blink Complete
</pre>
 
== Serial Monitor ==
 
The Serial Monitor operates at '''115200 baud'''.
 
The ESP32 displays received Bluetooth commands on the Serial Monitor.
 
For example:


<pre>
<pre>
Received: ON
Received: TEMP
Received: STATUS
</pre>
When the temperature is requested, the measured temperature is also displayed on the Serial Monitor.
== Bluetooth Output ==
When the ESP32 starts, it sends:
<pre>
ESP32 Bluetooth Ready
Commands:
ON, OFF, TEMP, STATUS
BLINK, BUZZON, BUZZOFF
</pre>
For a temperature request, the output is in the format:
<pre>
Temperature: 28.45 °C
</pre>
If the temperature exceeds 35 °C:
<pre>
WARNING: HIGH TEMPERATURE!
</pre>
== Program ==
<pre>
/*
========================================
ESP32 Bluetooth + LM35 + Buzzer Demo
========================================
Bluetooth Commands:
ON        -> Turn LED ON
OFF      -> Turn LED OFF
TEMP      -> Read LM35 Temperature
STATUS    -> Check ESP32 Status
BLINK    -> Blink LED 5 Times
BUZZON    -> Turn Buzzer ON
BUZZOFF  -> Turn Buzzer OFF
========================================
Connections:
LM35:
VCC  -> 3.3V
OUT  -> GPIO34
GND  -> GND
Buzzer:
+ -> GPIO25
- -> GND
LED:
Built-in LED -> GPIO2
Bluetooth Device Name:
ESP32_Demo
========================================
*/
#include "BluetoothSerial.h"
#include "BluetoothSerial.h"


BluetoothSerial SerialBT;
BluetoothSerial SerialBT;
#define LED_PIN    2
#define LM35_PIN    34
#define BUZZER_PIN  25
String incomingData = "";


void setup() {
void setup() {
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
  digitalWrite(BUZZER_PIN, LOW);


   Serial.begin(115200);
   Serial.begin(115200);


  // Start Bluetooth
   SerialBT.begin("ESP32_Demo");
   SerialBT.begin("ESP32_Demo");


   Serial.println("Bluetooth Started");
   Serial.println("Bluetooth Started");
  Serial.println("Pair the ESP32 with a Bluetooth device.");


   SerialBT.println("ESP32 Bluetooth Ready");
   SerialBT.println("ESP32 Bluetooth Ready");
  SerialBT.println("Commands:");
  SerialBT.println("ON, OFF, TEMP, STATUS");
  SerialBT.println("BLINK, BUZZON, BUZZOFF");
}
}


void loop() {
void loop() {


  // Receive data from Bluetooth
   while (SerialBT.available()) {
   while (SerialBT.available()) {


     char c = SerialBT.read();
     char c = SerialBT.read();


     if (c == '\n') {
     Serial.print("Received: ");
    Serial.println(c);


      incomingData.trim();
    SerialBT.print("Received: ");
      incomingData.toUpperCase();
    SerialBT.println(c);
  }


      Serial.print("Received: ");
  // Send a message periodically
      Serial.println(incomingData);
  SerialBT.println("Hello from ESP32");


      // ======================
  delay(1000);
      // LED ON
}
      // ======================
</pre>
      if (incomingData == "ON") {


        digitalWrite(LED_PIN, HIGH);
== Example of Command-Based Communication ==


        SerialBT.println("LED ON");
Bluetooth can also be used to send commands to the ESP32.
      }


      // ======================
For example:
      // LED OFF
      // ======================
      else if (incomingData == "OFF") {


        digitalWrite(LED_PIN, LOW);
{| class="wikitable"
! Command
! Function
|-
| ON
| Turn an output ON
|-
| OFF
| Turn an output OFF
|-
| STATUS
| Send ESP32 status
|-
| SENSOR
| Send sensor reading
|}


        SerialBT.println("LED OFF");
A command received through Bluetooth can be compared in the program and the required operation can be performed.
      }


      // ======================
Example:
      // TEMP READ
      // ======================
      else if (incomingData == "TEMP") {


        float temperatureC = readTemperature();
<pre>
if (command == "ON") {


        SerialBT.print("Temperature: ");
  digitalWrite(LED_PIN, HIGH);
        SerialBT.print(temperatureC);
        SerialBT.println(" °C");


        Serial.print("Temperature: ");
  SerialBT.println("LED ON");
        Serial.println(temperatureC);
}


        // Auto buzzer alert
else if (command == "OFF") {
        if (temperatureC > 35) {


          digitalWrite(BUZZER_PIN, HIGH);
  digitalWrite(LED_PIN, LOW);
 
          SerialBT.println("WARNING: HIGH TEMPERATURE!");
        }
        else {
 
          digitalWrite(BUZZER_PIN, LOW);
        }
      }
 
      // ======================
      // STATUS
      // ======================
      else if (incomingData == "STATUS") {
 
        SerialBT.println("ESP32 Running OK");
      }
 
      // ======================
      // BLINK
      // ======================
      else if (incomingData == "BLINK") {
 
        SerialBT.println("Blinking LED");
 
        for (int i = 0; i < 5; i++) {
 
          digitalWrite(LED_PIN, HIGH);
          delay(300);
 
          digitalWrite(LED_PIN, LOW);
          delay(300);
        }
 
        SerialBT.println("Blink Complete");
      }
 
      // ======================
      // BUZZER ON
      // ======================
      else if (incomingData == "BUZZON") {
 
        digitalWrite(BUZZER_PIN, HIGH);
 
        SerialBT.println("Buzzer ON");
      }
 
      // ======================
      // BUZZER OFF
      // ======================
      else if (incomingData == "BUZZOFF") {
 
        digitalWrite(BUZZER_PIN, LOW);
 
        SerialBT.println("Buzzer OFF");
      }
 
      // ======================
      // UNKNOWN COMMAND
      // ======================
      else {
 
        SerialBT.println("Unknown Command");
      }
 
      incomingData = "";
    }
 
    else {


      incomingData += c;
  SerialBT.println("LED OFF");
    }
  }
}
}
</pre>


// =====================================
== Applications ==
// FUNCTION: Read LM35 Temperature
// =====================================
float readTemperature() {


  int adcValue = analogRead(LM35_PIN);
ESP32 Bluetooth communication can be used in many applications, including:


  // Convert ADC to Voltage
* Wireless LED control
  float voltage = adcValue * (3.3 / 4095.0);
* Home automation
* Sensor monitoring
* Robot control
* Wireless data logging
* Industrial control systems
* Smart appliances
* Mobile-controlled projects
* Wireless serial communication


  // LM35 = 10mV per °C
== Safety Precautions ==
  float temperatureC = voltage * 100.0;


  return temperatureC;
* Check the ESP32 power supply before starting the experiment.
}
* Use the correct GPIO pins for external sensors and actuators.
</pre>
* Do not exceed the GPIO current limits of the ESP32.
* Ensure that the Bluetooth device is properly paired before testing communication.
* Keep the Bluetooth-enabled devices within the appropriate communication range.
* Use suitable driver circuits when controlling high-current loads.


== Observation ==
== Observation ==


Observe the Bluetooth terminal and Serial Monitor while sending different commands.
After starting the program, the ESP32 appears as the configured Bluetooth device name.
 
The following operations can be observed:


{| class="wikitable"
After pairing the ESP32 with a smartphone or computer, messages can be transmitted wirelessly.
! Command
! Expected Output
|-
| ON
| LED turns ON
|-
| OFF
| LED turns OFF
|-
| TEMP
| Temperature is displayed
|-
| STATUS
| ESP32 status is displayed
|-
| BLINK
| LED blinks 5 times
|-
| BUZZON
| Buzzer turns ON
|-
| BUZZOFF
| Buzzer turns OFF
|}


When the temperature exceeds '''35 °C''' during a '''TEMP''' command, the buzzer turns ON and a high-temperature warning is transmitted.
Observe the transmitted and received data on the Serial Monitor and the connected Bluetooth device.


== Result ==
== Result ==


The ESP32 successfully interfaces with the '''LM35 temperature sensor, built-in LED and buzzer'''.
The ESP32 successfully communicates with a Bluetooth-enabled device using its built-in Bluetooth interface.
 
The ESP32 reads the LM35 temperature through GPIO 34 and communicates with a Bluetooth-enabled device using the Bluetooth device name '''ESP32_Demo'''.


The LED and buzzer can be controlled using Bluetooth commands, while the temperature can be read and transmitted wirelessly.
Data can be transmitted from the ESP32 to the connected device, and commands or data can be received by the ESP32 through Bluetooth.


The system also provides an automatic buzzer alert when the measured temperature is greater than '''35 °C'''.
The Bluetooth interface can therefore be used for wireless monitoring, control, and data communication in ESP32-based applications.

Revision as of 05:18, 4 September 2026

ESP32 Bluetooth Communication

ESP32 Bluetooth Communication demonstrates how the built-in Bluetooth functionality of the ESP32 can be used for wireless communication between the ESP32 board and a Bluetooth-enabled device such as a smartphone, tablet, or computer.

The ESP32 can send and receive data wirelessly using Bluetooth without requiring a physical serial connection.

Bluetooth Device → Wireless Communication → ESP32

Objectives

  • To study the Bluetooth functionality available in the ESP32.
  • To understand how to initialize Bluetooth communication.
  • To pair the ESP32 with a Bluetooth-enabled device.
  • To send data from the ESP32 to a Bluetooth device.
  • To receive commands or data from a Bluetooth device.
  • To control ESP32 peripherals using Bluetooth commands.

Software

  • Arduino IDE
  • ESP32 board package
  • Bluetooth-enabled smartphone, tablet, or computer

Hardware Modules

  • ESP32 Development Board
  • PC / Laptop
  • Smartphone / Bluetooth-enabled device
  • External sensors or actuators as required

Theory

Bluetooth is a wireless communication technology used for exchanging data between electronic devices over short distances.

The ESP32 contains built-in Bluetooth hardware, allowing it to communicate with compatible Bluetooth devices without requiring an external Bluetooth module.

The ESP32 can be programmed to operate as a Bluetooth device with a user-defined device name.

ESP32 Bluetooth → Wireless Data Transmission → Smartphone / PC

Bluetooth communication can be used for:

  • Sending sensor readings
  • Receiving control commands
  • Controlling LEDs
  • Controlling buzzers
  • Monitoring system status
  • Sending data to a mobile application
  • Wireless serial communication

ESP32 Bluetooth

The ESP32 provides built-in Bluetooth functionality.

For Bluetooth Classic communication in the Arduino environment, the BluetoothSerial library can be used.

The library provides a serial-like interface for sending and receiving data wirelessly.

The basic sequence is:

Initialize Bluetooth → Set Device Name → Pair Device → Send/Receive Data

BluetoothSerial Library

The BluetoothSerial library provides functions for Bluetooth Classic communication.

The library is included using:

#include "BluetoothSerial.h"

A Bluetooth serial object is then created:

BluetoothSerial SerialBT;

Bluetooth communication is started using:

SerialBT.begin("ESP32_Demo");

Here, ESP32_Demo is the Bluetooth device name that appears when searching for the ESP32 from another Bluetooth device.

Bluetooth Communication

Bluetooth communication works in two directions.

Sending Data

The ESP32 can send data to a connected Bluetooth device using:

SerialBT.print("Hello");
SerialBT.println("ESP32");

The print() function sends data without automatically adding a new line.

The println() function sends data followed by a new line.

ESP32 → Bluetooth → Smartphone / PC

Receiving Data

The ESP32 can check whether data has been received using:

SerialBT.available()

Received characters can be read using:

char c = SerialBT.read();

The received characters can then be processed as commands or sensor-control instructions.

Smartphone / PC → Bluetooth → ESP32

Bluetooth Device Name

The ESP32 Bluetooth device name can be specified while starting Bluetooth.

Example:

SerialBT.begin("ESP32_Demo");

After Bluetooth is started, the device appears as:

ESP32_Demo

on the Bluetooth device used for pairing.

The name can be changed as required.

For example:

SerialBT.begin("MyESP32");

Connections

Bluetooth communication itself does not require external communication wires between the ESP32 and the smartphone or computer.

Device Connection Function
ESP32 Built-in Bluetooth Wireless communication
Smartphone / PC Bluetooth Wireless communication

External sensors and actuators can be connected to the ESP32 separately depending on the application.

Basic Bluetooth Communication Flow

The Bluetooth communication process can be summarized as:

  1. Start the Arduino IDE.
  2. Include the BluetoothSerial library.
  3. Create a Bluetooth serial object.
  4. Start Bluetooth using SerialBT.begin().
  5. Assign a name to the ESP32 Bluetooth device.
  6. Search for the ESP32 using a Bluetooth-enabled device.
  7. Pair/connect the device with the ESP32.
  8. Send data from the ESP32 using SerialBT.print() or SerialBT.println().
  9. Check for incoming data using SerialBT.available().
  10. Read incoming data using SerialBT.read().
  11. Process the received data according to the application.

Algorithm

  1. Start the Arduino IDE.
  2. Include the BluetoothSerial library.
  3. Create a BluetoothSerial object.
  4. Start Bluetooth communication with a suitable device name.
  5. Display the Bluetooth status on the Serial Monitor.
  6. Wait for a Bluetooth device to connect.
  7. Check whether data is available from the Bluetooth device.
  8. Read the received data.
  9. Process the received command or data.
  10. Send a response using Bluetooth.
  11. Repeat the process continuously.

Start → Initialize Bluetooth → Connect Device → Send/Receive Data → Process Data → Repeat

Working

When the ESP32 program starts, the Bluetooth interface is initialized using the BluetoothSerial library.

The ESP32 becomes discoverable using the Bluetooth device name specified in the program.

After pairing or connecting a smartphone or computer, data can be exchanged wirelessly.

The ESP32 can send messages using:

SerialBT.println("Hello from ESP32");

It can also receive data from the connected Bluetooth device.

For example, a smartphone can send:

ON

The ESP32 can receive this command and use it to control an output device such as an LED.

Similarly, a command such as:

OFF

can be used to turn the LED OFF.

Bluetooth Command → ESP32 → Program Processing → Output Control

Example Program

The following program demonstrates basic Bluetooth communication with the ESP32.

#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

void setup() {

  Serial.begin(115200);

  SerialBT.begin("ESP32_Demo");

  Serial.println("Bluetooth Started");
  Serial.println("Pair the ESP32 with a Bluetooth device.");

  SerialBT.println("ESP32 Bluetooth Ready");
}

void loop() {

  // Receive data from Bluetooth
  while (SerialBT.available()) {

    char c = SerialBT.read();

    Serial.print("Received: ");
    Serial.println(c);

    SerialBT.print("Received: ");
    SerialBT.println(c);
  }

  // Send a message periodically
  SerialBT.println("Hello from ESP32");

  delay(1000);
}

Example of Command-Based Communication

Bluetooth can also be used to send commands to the ESP32.

For example:

Command Function
ON Turn an output ON
OFF Turn an output OFF
STATUS Send ESP32 status
SENSOR Send sensor reading

A command received through Bluetooth can be compared in the program and the required operation can be performed.

Example:

if (command == "ON") {

  digitalWrite(LED_PIN, HIGH);

  SerialBT.println("LED ON");
}

else if (command == "OFF") {

  digitalWrite(LED_PIN, LOW);

  SerialBT.println("LED OFF");
}

Applications

ESP32 Bluetooth communication can be used in many applications, including:

  • Wireless LED control
  • Home automation
  • Sensor monitoring
  • Robot control
  • Wireless data logging
  • Industrial control systems
  • Smart appliances
  • Mobile-controlled projects
  • Wireless serial communication

Safety Precautions

  • Check the ESP32 power supply before starting the experiment.
  • Use the correct GPIO pins for external sensors and actuators.
  • Do not exceed the GPIO current limits of the ESP32.
  • Ensure that the Bluetooth device is properly paired before testing communication.
  • Keep the Bluetooth-enabled devices within the appropriate communication range.
  • Use suitable driver circuits when controlling high-current loads.

Observation

After starting the program, the ESP32 appears as the configured Bluetooth device name.

After pairing the ESP32 with a smartphone or computer, messages can be transmitted wirelessly.

Observe the transmitted and received data on the Serial Monitor and the connected Bluetooth device.

Result

The ESP32 successfully communicates with a Bluetooth-enabled device using its built-in Bluetooth interface.

Data can be transmitted from the ESP32 to the connected device, and commands or data can be received by the ESP32 through Bluetooth.

The Bluetooth interface can therefore be used for wireless monitoring, control, and data communication in ESP32-based applications.