ESP32 Bluetooth: Difference between revisions
No edit summary |
|||
| Line 1: | Line 1: | ||
===Interfacing | ===Interfacing LM35 Temperature Sensor, LED and Buzzer with ESP32 using Bluetooth=== | ||
'''Interfacing | '''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. | ||
The ESP32 | 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. | ||
<center> | <center> | ||
''' | '''Bluetooth Device → ESP32 → LED / LM35 / Buzzer''' | ||
</center> | </center> | ||
== Objectives == | == Objectives == | ||
* To study the working of the | * To study the working of the LM35 temperature sensor. | ||
* To interface the | * To interface the LM35 temperature sensor with the ESP32. | ||
* To | * To interface an LED and buzzer with the ESP32. | ||
* To | * To control the LED and buzzer using Bluetooth commands. | ||
* To transmit | * To read temperature from the LM35 using the ESP32 ADC. | ||
* To transmit temperature and status information through Bluetooth. | |||
== Software == | == Software == | ||
| Line 26: | Line 27: | ||
* ESP32 Board | * ESP32 Board | ||
* | * LM35 Temperature Sensor | ||
* Buzzer | |||
* LED | |||
* PC / Laptop | * PC / Laptop | ||
* Bluetooth-enabled device | * Bluetooth-enabled device | ||
| Line 33: | Line 36: | ||
== Theory == | == Theory == | ||
=== Introduction to | === Introduction to LM35 === | ||
''' | The '''LM35''' is an analog temperature sensor used to measure temperature. | ||
The output voltage of the LM35 changes according to the temperature. | |||
The | The LM35 provides an output of approximately '''10 mV per °C'''. | ||
The temperature can therefore be calculated from the sensor output voltage. | |||
<center> | <center> | ||
''' | '''Temperature → LM35 → Analog Voltage → ESP32 ADC''' | ||
</center> | </center> | ||
The ESP32 reads the analog voltage from the LM35 using an analog input pin. | |||
The ESP32 reads the analog voltage using an analog input pin. | |||
In this practical, the | In this practical, the LM35 output is connected to: | ||
<center> | <center> | ||
''' | '''GPIO 34''' | ||
</center> | </center> | ||
== LM35 Connections == | |||
== Connections == | |||
{| class="wikitable" | {| class="wikitable" | ||
! | ! LM35 Pin | ||
! ESP32 | ! ESP32 | ||
! Function | ! Function | ||
|- | |- | ||
| VCC | | VCC | ||
| 3.3V | | 3.3V | ||
| Power supply | | Power supply | ||
|- | |- | ||
| | | OUT | ||
| GPIO 34 | |||
| Analog temperature output | |||
|- | |||
| GND | | GND | ||
| GND | | GND | ||
| Line 99: | Line 78: | ||
|} | |} | ||
== | == LM35 Temperature Calculation == | ||
The ESP32 | The ESP32 reads the analog value from the LM35 using: | ||
<pre> | |||
analogRead(LM35_PIN); | |||
</pre> | |||
The | The ESP32 ADC value is converted into voltage using: | ||
<pre> | <pre> | ||
float voltage = adcValue * (3.3 / 4095.0); | |||
</pre> | </pre> | ||
The LM35 produces approximately 10 mV per °C. | |||
Therefore, the temperature is calculated using: | |||
<pre> | <pre> | ||
float temperatureC = voltage * 100.0; | |||
</pre> | </pre> | ||
The | The calculated temperature is returned by the '''readTemperature()''' function. | ||
<center> | |||
'''ADC Value → Voltage → Temperature in °C''' | |||
</center> | |||
== ESP32 ADC == | |||
The program uses GPIO 34 as the analog input for the LM35. | |||
The calculation in the program uses a '''12-bit ADC''' range: | |||
<center> | |||
'''0 to 4095''' | |||
</center> | |||
The reference voltage used in the calculation is '''3.3 V'''. | |||
The relationship used by the program is: | |||
<center> | |||
'''Voltage = ADC Value × (3.3 / 4095)''' | |||
</center> | |||
The LM35 temperature is then calculated as: | |||
<center> | |||
'''Temperature (°C) = Voltage × 100''' | |||
</center> | |||
== LED Interface == | |||
The built-in LED of the ESP32 is connected to: | |||
<center> | |||
'''GPIO 2''' | |||
</center> | |||
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> | <center> | ||
''' | '''GPIO 25''' | ||
</center> | </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'''. | |||
<center> | |||
'''Temperature > 35 °C → Buzzer ON → High Temperature Warning''' | |||
</center> | |||
== Bluetooth | == Bluetooth Communication == | ||
The ESP32 uses the '''BluetoothSerial''' library for Bluetooth communication. | |||
The | The following library is included: | ||
<pre> | <pre> | ||
#include "BluetoothSerial.h" | |||
</pre> | </pre> | ||
A Bluetooth Serial object is created using: | |||
<pre> | <pre> | ||
BluetoothSerial SerialBT; | |||
</pre> | </pre> | ||
Bluetooth is initialized using: | |||
<pre> | <pre> | ||
SerialBT.begin("ESP32_Demo"); | |||
</pre> | </pre> | ||
The | Therefore, the ESP32 Bluetooth device appears with the name: | ||
<center> | |||
'''ESP32_Demo''' | |||
</center> | |||
A Bluetooth-enabled device can pair with the ESP32 and send commands. | |||
== Bluetooth Commands == | |||
The program supports the following commands: | |||
{| 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 | The received command is converted to uppercase using: | ||
<pre> | <pre> | ||
incomingData.toUpperCase(); | |||
</pre> | </pre> | ||
This allows the program to compare the received command with the predefined commands. | |||
== Connections == | |||
{| class="wikitable" | |||
! Device | |||
! Pin | |||
! ESP32 | |||
! Function | |||
|- | |||
| LM35 | |||
| VCC | |||
| 3.3V | |||
| Power supply | |||
|- | |||
| LM35 | |||
| OUT | |||
| GPIO 34 | |||
| Analog temperature input | |||
|- | |||
| LM35 | |||
| GND | |||
| GND | |||
| Ground | |||
|- | |||
| Buzzer | |||
| + | |||
| GPIO 25 | |||
| Buzzer control | |||
|- | |||
| Buzzer | |||
| − | |||
| GND | |||
| Ground | |||
|- | |||
| Built-in LED | |||
| LED | |||
| GPIO 2 | |||
| LED control | |||
|} | |||
== Algorithm == | == Algorithm == | ||
The following algorithm is used | 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. | ||
# Declare GPIO | # Declare GPIO 34 for the LM35 analog input. | ||
# Declare | # Declare GPIO 25 for the buzzer. | ||
# Create a Bluetooth Serial object. | # Create a Bluetooth Serial object. | ||
# In the '''void setup()''' function, | # In the '''void setup()''' function, configure the LED and buzzer pins as '''OUTPUT'''. | ||
# Start Bluetooth | # Set the initial state of the LED and buzzer to LOW. | ||
# | # Start Serial communication at '''115200 baud'''. | ||
# | # Start Bluetooth using the device name '''ESP32_Demo'''. | ||
# | # 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. | ||
| Line 200: | Line 330: | ||
<center> | <center> | ||
''' | '''Bluetooth Command → ESP32 → Required Operation''' | ||
</center> | </center> | ||
== Working == | == Working == | ||
The ESP32 | The ESP32 starts Bluetooth communication using the device name '''ESP32_Demo'''. | ||
A Bluetooth-enabled device can send commands to the ESP32. | |||
The ESP32 receives the command and checks which operation has been requested. | |||
For LED control: | |||
<center> | |||
'''ON → LED ON''' | |||
</center> | |||
<center> | |||
'''OFF → LED OFF''' | |||
</center> | |||
For temperature measurement: | |||
<center> | <center> | ||
''' | '''TEMP → LM35 Reading → Temperature → Bluetooth Output''' | ||
</center> | </center> | ||
For buzzer control: | |||
<center> | |||
'''BUZZON → Buzzer ON''' | |||
</center> | |||
<center> | <center> | ||
''' | '''BUZZOFF → Buzzer OFF''' | ||
</center> | </center> | ||
The '''BLINK''' command makes the LED blink five times. | |||
The '''STATUS''' command sends the message: | |||
<pre> | |||
ESP32 Running OK | |||
</pre> | |||
== Temperature Alert == | |||
When the '''TEMP''' command is received, the ESP32 reads the LM35 sensor. | |||
The temperature is calculated and transmitted through Bluetooth. | |||
If the temperature is greater than '''35 °C''', the buzzer is automatically switched ON. | |||
The Bluetooth device receives: | |||
<pre> | |||
WARNING: HIGH TEMPERATURE! | |||
</pre> | |||
If the temperature is not greater than 35 °C, the buzzer is switched OFF. | |||
The operation can be represented as: | |||
<center> | <center> | ||
''' | '''TEMP → Read LM35 → Calculate Temperature → Temperature > 35 °C? → Buzzer Alert''' | ||
</center> | </center> | ||
== LED Blinking == | |||
When the '''BLINK''' command is received, the ESP32 blinks the built-in LED five times. | |||
Each blink consists of: | |||
<center> | |||
'''LED HIGH → 300 ms → LED LOW → 300 ms''' | |||
</center> | |||
This sequence is repeated '''5 times'''. | |||
After completing the sequence, the ESP32 transmits: | |||
<pre> | <pre> | ||
Blink Complete | |||
</pre> | </pre> | ||
== Serial Monitor == | |||
The Serial Monitor operates at '''115200 baud'''. | |||
The ESP32 displays received Bluetooth commands on the Serial Monitor. | |||
For example: | For example: | ||
<pre> | <pre> | ||
Received: ON | |||
Received: TEMP | |||
Received: STATUS | |||
</pre> | </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 == | == Program == | ||
<pre> | <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; | |||
#define LED_PIN 2 | |||
#define LM35_PIN 34 | |||
#define BUZZER_PIN 25 | |||
String incomingData = ""; | |||
void setup() { | |||
pinMode(LED_PIN, OUTPUT); | |||
pinMode(BUZZER_PIN, OUTPUT); | |||
digitalWrite(LED_PIN, LOW); | |||
digitalWrite(BUZZER_PIN, LOW); | |||
Serial. | Serial.begin(115200); | ||
// Start Bluetooth | |||
SerialBT.begin("ESP32_Demo"); | |||
Serial.println("Bluetooth Started"); | |||
SerialBT.println("ESP32 Bluetooth Ready"); | |||
SerialBT.println("Commands:"); | |||
SerialBT.println("ON, OFF, TEMP, STATUS"); | |||
SerialBT.println("BLINK, BUZZON, BUZZOFF"); | |||
} | } | ||
void loop() | void loop() { | ||
{ | |||
while (SerialBT.available()) { | |||
char c = SerialBT.read(); | |||
if (c == '\n') { | |||
incomingData.trim(); | |||
incomingData.toUpperCase(); | |||
Serial.print("Received: "); | |||
Serial.println(incomingData); | |||
// ====================== | |||
// LED ON | |||
// ====================== | |||
if (incomingData == "ON") { | |||
digitalWrite(LED_PIN, HIGH); | |||
SerialBT.println("LED ON"); | |||
} | |||
// ====================== | |||
// LED OFF | |||
// ====================== | |||
else if (incomingData == "OFF") { | |||
digitalWrite(LED_PIN, LOW); | |||
SerialBT.println("LED OFF"); | |||
} | |||
// ====================== | |||
// TEMP READ | |||
// ====================== | |||
else if (incomingData == "TEMP") { | |||
float temperatureC = readTemperature(); | |||
SerialBT.print("Temperature: "); | |||
SerialBT.print(temperatureC); | |||
SerialBT.println(" °C"); | |||
Serial.print("Temperature: "); | |||
Serial.println(temperatureC); | |||
// Auto buzzer alert | |||
if (temperatureC > 35) { | |||
digitalWrite(BUZZER_PIN, HIGH); | |||
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; | |||
} | |||
} | |||
} | |||
// ===================================== | |||
// FUNCTION: Read LM35 Temperature | |||
// ===================================== | |||
float readTemperature() { | |||
int adcValue = analogRead(LM35_PIN); | |||
// Convert ADC to Voltage | |||
float voltage = adcValue * (3.3 / 4095.0); | |||
// LM35 = 10mV per °C | |||
float temperatureC = voltage * 100.0; | |||
return temperatureC; | |||
} | } | ||
</pre> | </pre> | ||
| Line 309: | Line 669: | ||
== Observation == | == Observation == | ||
Observe the | Observe the Bluetooth terminal and Serial Monitor while sending different commands. | ||
The | The following operations can be observed: | ||
{| class="wikitable" | |||
! 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. | |||
== Result == | |||
The ESP32 successfully interfaces with the '''LM35 temperature sensor, built-in LED and buzzer'''. | |||
The ESP32 reads the LM35 temperature through GPIO 34 and communicates with a Bluetooth-enabled device using the Bluetooth device name '''ESP32_Demo'''. | |||
The | The LED and buzzer can be controlled using Bluetooth commands, while the temperature can be read and transmitted wirelessly. | ||
The | The system also provides an automatic buzzer alert when the measured temperature is greater than '''35 °C'''. | ||
Revision as of 05:17, 4 September 2026
Interfacing LM35 Temperature Sensor, LED and Buzzer with ESP32 using Bluetooth
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.
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.
Bluetooth Device → ESP32 → LED / LM35 / Buzzer
Objectives
- To study the working of the LM35 temperature sensor.
- To interface the LM35 temperature sensor with the ESP32.
- To interface an LED and buzzer with the ESP32.
- To control the LED and buzzer using Bluetooth commands.
- To read temperature from the LM35 using the ESP32 ADC.
- To transmit temperature and status information through Bluetooth.
Software
- Arduino IDE
- ESP32 Board Package
- BluetoothSerial Library
Hardware Modules
- ESP32 Board
- LM35 Temperature Sensor
- Buzzer
- LED
- PC / Laptop
- Bluetooth-enabled device
- Connecting wires
Theory
Introduction to LM35
The LM35 is an analog temperature sensor used to measure temperature.
The output voltage of the LM35 changes according to the temperature.
The LM35 provides an output of approximately 10 mV per °C.
The temperature can therefore be calculated from the sensor output voltage.
Temperature → LM35 → Analog Voltage → ESP32 ADC
The ESP32 reads the analog voltage from the LM35 using an analog input pin.
In this practical, the LM35 output is connected to:
GPIO 34
LM35 Connections
| LM35 Pin | ESP32 | Function |
|---|---|---|
| VCC | 3.3V | Power supply |
| OUT | GPIO 34 | Analog temperature output |
| GND | GND | Ground |
LM35 Temperature Calculation
The ESP32 reads the analog value from the LM35 using:
analogRead(LM35_PIN);
The ESP32 ADC value is converted into voltage using:
float voltage = adcValue * (3.3 / 4095.0);
The LM35 produces approximately 10 mV per °C.
Therefore, the temperature is calculated using:
float temperatureC = voltage * 100.0;
The calculated temperature is returned by the readTemperature() function.
ADC Value → Voltage → Temperature in °C
ESP32 ADC
The program uses GPIO 34 as the analog input for the LM35.
The calculation in the program uses a 12-bit ADC range:
0 to 4095
The reference voltage used in the calculation is 3.3 V.
The relationship used by the program is:
Voltage = ADC Value × (3.3 / 4095)
The LM35 temperature is then calculated as:
Temperature (°C) = Voltage × 100
LED Interface
The built-in LED of the ESP32 is connected to:
GPIO 2
The LED is controlled through Bluetooth commands.
The following commands are used:
| 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:
GPIO 25
The buzzer can be controlled using Bluetooth commands.
| 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.
Temperature > 35 °C → Buzzer ON → High Temperature Warning
Bluetooth Communication
The ESP32 uses the BluetoothSerial library for Bluetooth communication.
The following library is included:
#include "BluetoothSerial.h"
A Bluetooth Serial object is created using:
BluetoothSerial SerialBT;
Bluetooth is initialized using:
SerialBT.begin("ESP32_Demo");
Therefore, the ESP32 Bluetooth device appears with the name:
ESP32_Demo
A Bluetooth-enabled device can pair with the ESP32 and send commands.
Bluetooth Commands
The program supports the following commands:
| 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:
incomingData.toUpperCase();
This allows the program to compare the received command with the predefined commands.
Connections
| Device | Pin | ESP32 | Function |
|---|---|---|---|
| LM35 | VCC | 3.3V | Power supply |
| LM35 | OUT | GPIO 34 | Analog temperature input |
| LM35 | GND | GND | Ground |
| Buzzer | + | GPIO 25 | Buzzer control |
| Buzzer | − | GND | Ground |
| Built-in LED | LED | GPIO 2 | LED control |
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.
- Include the BluetoothSerial library.
- Declare GPIO 2 for the LED.
- Declare GPIO 34 for the LM35 analog input.
- Declare GPIO 25 for the buzzer.
- Create a Bluetooth Serial object.
- In the void setup() function, configure the LED and buzzer pins as OUTPUT.
- Set the initial state of the LED and buzzer to LOW.
- Start Serial communication at 115200 baud.
- Start Bluetooth using the device name ESP32_Demo.
- 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.
The overall operation is:
Bluetooth Command → ESP32 → Required Operation
Working
The ESP32 starts Bluetooth communication using the device name ESP32_Demo.
A Bluetooth-enabled device can send commands to the ESP32.
The ESP32 receives the command and checks which operation has been requested.
For LED control:
ON → LED ON
OFF → LED OFF
For temperature measurement:
TEMP → LM35 Reading → Temperature → Bluetooth Output
For buzzer control:
BUZZON → Buzzer ON
BUZZOFF → Buzzer OFF
The BLINK command makes the LED blink five times.
The STATUS command sends the message:
ESP32 Running OK
Temperature Alert
When the TEMP command is received, the ESP32 reads the LM35 sensor.
The temperature is calculated and transmitted through Bluetooth.
If the temperature is greater than 35 °C, the buzzer is automatically switched ON.
The Bluetooth device receives:
WARNING: HIGH TEMPERATURE!
If the temperature is not greater than 35 °C, the buzzer is switched OFF.
The operation can be represented as:
TEMP → Read LM35 → Calculate Temperature → Temperature > 35 °C? → Buzzer Alert
LED Blinking
When the BLINK command is received, the ESP32 blinks the built-in LED five times.
Each blink consists of:
LED HIGH → 300 ms → LED LOW → 300 ms
This sequence is repeated 5 times.
After completing the sequence, the ESP32 transmits:
Blink Complete
Serial Monitor
The Serial Monitor operates at 115200 baud.
The ESP32 displays received Bluetooth commands on the Serial Monitor.
For example:
Received: ON Received: TEMP Received: STATUS
When the temperature is requested, the measured temperature is also displayed on the Serial Monitor.
Bluetooth Output
When the ESP32 starts, it sends:
ESP32 Bluetooth Ready Commands: ON, OFF, TEMP, STATUS BLINK, BUZZON, BUZZOFF
For a temperature request, the output is in the format:
Temperature: 28.45 °C
If the temperature exceeds 35 °C:
WARNING: HIGH TEMPERATURE!
Program
/*
========================================
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"
BluetoothSerial SerialBT;
#define LED_PIN 2
#define LM35_PIN 34
#define BUZZER_PIN 25
String incomingData = "";
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
Serial.begin(115200);
// Start Bluetooth
SerialBT.begin("ESP32_Demo");
Serial.println("Bluetooth Started");
SerialBT.println("ESP32 Bluetooth Ready");
SerialBT.println("Commands:");
SerialBT.println("ON, OFF, TEMP, STATUS");
SerialBT.println("BLINK, BUZZON, BUZZOFF");
}
void loop() {
while (SerialBT.available()) {
char c = SerialBT.read();
if (c == '\n') {
incomingData.trim();
incomingData.toUpperCase();
Serial.print("Received: ");
Serial.println(incomingData);
// ======================
// LED ON
// ======================
if (incomingData == "ON") {
digitalWrite(LED_PIN, HIGH);
SerialBT.println("LED ON");
}
// ======================
// LED OFF
// ======================
else if (incomingData == "OFF") {
digitalWrite(LED_PIN, LOW);
SerialBT.println("LED OFF");
}
// ======================
// TEMP READ
// ======================
else if (incomingData == "TEMP") {
float temperatureC = readTemperature();
SerialBT.print("Temperature: ");
SerialBT.print(temperatureC);
SerialBT.println(" °C");
Serial.print("Temperature: ");
Serial.println(temperatureC);
// Auto buzzer alert
if (temperatureC > 35) {
digitalWrite(BUZZER_PIN, HIGH);
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;
}
}
}
// =====================================
// FUNCTION: Read LM35 Temperature
// =====================================
float readTemperature() {
int adcValue = analogRead(LM35_PIN);
// Convert ADC to Voltage
float voltage = adcValue * (3.3 / 4095.0);
// LM35 = 10mV per °C
float temperatureC = voltage * 100.0;
return temperatureC;
}
Observation
Observe the Bluetooth terminal and Serial Monitor while sending different commands.
The following operations can be observed:
| 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.
Result
The ESP32 successfully interfaces with the LM35 temperature sensor, built-in LED and buzzer.
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.
The system also provides an automatic buzzer alert when the measured temperature is greater than 35 °C.