Jump to content

ESP32 Bluetooth: Difference between revisions

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


'''Interfacing LDR with ESP32 and Transmitting Sensor Data using Bluetooth''' demonstrates how an LDR sensor can be interfaced with an ESP32 board to measure light intensity and transmit the sensor value wirelessly 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 reads the analog value from the LDR and sends the value through Bluetooth to a paired device.
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>
'''LDR → ESP32 → Bluetooth → Paired Device'''
'''Bluetooth Device → ESP32 → LED / LM35 / Buzzer'''
</center>
</center>


== Objectives ==
== Objectives ==


* To study the working of the LDR sensor.
* To study the working of the LM35 temperature sensor.
* To interface the LDR sensor with the ESP32 board.
* To interface the LM35 temperature sensor with the ESP32.
* To read the analog value from the LDR.
* To interface an LED and buzzer with the ESP32.
* To display the LDR value on the Serial Monitor.
* To control the LED and buzzer using Bluetooth commands.
* To transmit the LDR sensor value using Bluetooth.
* 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
* LDR Sensor Module
* LM35 Temperature Sensor
* Buzzer
* LED
* PC / Laptop
* PC / Laptop
* Bluetooth-enabled device
* Bluetooth-enabled device
Line 33: Line 36:
== Theory ==
== Theory ==


=== Introduction to LDR ===
=== Introduction to LM35 ===


'''LDR''' stands for '''Light Dependent Resistor'''.
The '''LM35''' is an analog temperature sensor used to measure temperature.


An LDR is a light-sensitive resistor whose resistance changes according to the intensity of light falling on its surface.
The output voltage of the LM35 changes according to the temperature.


The LDR works on the principle of '''Photo-Conductivity'''.
The LM35 provides an output of approximately '''10 mV per °C'''.


As the intensity of light increases, the resistance of the LDR decreases.
The temperature can therefore be calculated from the sensor output voltage.
 
Similarly, as the intensity of light decreases, the resistance of the LDR increases.
 
<center>
'''More Light → Lower LDR Resistance'''
</center>


<center>
<center>
'''Less Light → Higher LDR Resistance'''
'''Temperature → LM35 → Analog Voltage → ESP32 ADC'''
</center>
</center>


The change in resistance can be converted into a corresponding voltage using an LDR sensor circuit or module.
The ESP32 reads the analog voltage from the LM35 using an analog input pin.
 
== LDR as an Analog Sensor ==
 
The LDR is an '''analog sensor''' because its output changes continuously according to the intensity of light.
 
The ESP32 reads the analog voltage using an analog input pin.


In this practical, the LDR is connected to:
In this practical, the LM35 output is connected to:


<center>
<center>
'''ESP32 GPIO 35'''
'''GPIO 34'''
</center>
</center>


The analog value is read using the '''analogRead()''' function.
== LM35 Connections ==
 
<pre>
ldrVal = analogRead(ldrPin);
</pre>
 
The sensor reading is stored in the variable '''ldrVal'''.
 
== Connections ==


{| class="wikitable"
{| class="wikitable"
! Device
! LM35 Pin
! Pin
! ESP32
! ESP32
! Function
! Function
|-
|-
| LDR Module
| Data / Output
| GPIO 35
| Analog sensor input
|-
| LDR Module
| VCC
| VCC
| 3.3V
| 3.3V
| Power supply
| Power supply
|-
|-
| LDR Module
| OUT
| GPIO 34
| Analog temperature output
|-
| GND
| GND
| GND
| GND
Line 99: Line 78:
|}
|}


== Bluetooth Communication ==
== LM35 Temperature Calculation ==


The ESP32 has built-in Bluetooth functionality.
The ESP32 reads the analog value from the LM35 using:


The program uses the '''BluetoothSerial''' library to establish Bluetooth Serial communication.
<pre>
analogRead(LM35_PIN);
</pre>


The following library is included:
The ESP32 ADC value is converted into voltage using:


<pre>
<pre>
#include "BluetoothSerial.h"
float voltage = adcValue * (3.3 / 4095.0);
</pre>
</pre>


A Bluetooth Serial object is created using:
The LM35 produces approximately 10 mV per °C.
 
Therefore, the temperature is calculated using:


<pre>
<pre>
BluetoothSerial BT;
float temperatureC = voltage * 100.0;
</pre>
</pre>


The Bluetooth device is initialized with the name:
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:


<pre>
{| class="wikitable"
BT.begin("COGESP");
! Bluetooth Command
</pre>
! 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 ==


Therefore, the ESP32 Bluetooth device appears as:
The buzzer is connected to:


<center>
<center>
'''COGESP'''
'''GPIO 25'''
</center>
</center>


on a device used for Bluetooth pairing.
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 Data Transmission ==
== Bluetooth Communication ==


After reading the LDR sensor value, the program converts the value into a character array before transmitting it.
The ESP32 uses the '''BluetoothSerial''' library for Bluetooth communication.


The sensor value is first converted into a '''String''':
The following library is included:


<pre>
<pre>
s = String (ldrVal);
#include "BluetoothSerial.h"
</pre>
</pre>


The String is then converted into a character array:
A Bluetooth Serial object is created using:


<pre>
<pre>
s.toCharArray(msg, 30);
BluetoothSerial SerialBT;
</pre>
</pre>


The value is transmitted through Bluetooth using:
Bluetooth is initialized using:


<pre>
<pre>
BT.print("LDR Val: ");
SerialBT.begin("ESP32_Demo");
BT.println(msg);
</pre>
</pre>


The transmitted data appears in the following format:
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:


<pre>
{| class="wikitable"
LDR Val: 1234
! Command
</pre>
! 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
|}


== Serial Monitor Output ==
Commands are received as text through Bluetooth.


The LDR value is also displayed on the Serial Monitor using:
The received command is converted to uppercase using:


<pre>
<pre>
Serial.println(msg);
incomingData.toUpperCase();
</pre>
</pre>


The Serial Monitor is configured at:
This allows the program to compare the received command with the predefined commands.


<pre>
== Connections ==
Serial.begin(9600);
</pre>


Therefore, the Serial Monitor baud rate should be set to '''9600'''.
{| 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 to interface the LDR with the ESP32 and transmit its value using Bluetooth:
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.
# Include the required sensor library definitions.
# Declare GPIO 2 for the LED.
# Declare GPIO 35 as the LDR input pin.
# Declare GPIO 34 for the LM35 analog input.
# Declare variables to store the LDR value and transmitted data.
# Declare GPIO 25 for the buzzer.
# Create a Bluetooth Serial object.
# Create a Bluetooth Serial object.
# In the '''void setup()''' function, start Serial communication at '''9600 baud'''.
# In the '''void setup()''' function, configure the LED and buzzer pins as '''OUTPUT'''.
# Start Bluetooth with the device name '''COGESP'''.
# Set the initial state of the LED and buzzer to LOW.
# Configure GPIO 35 as an '''INPUT'''.
# Start Serial communication at '''115200 baud'''.
# In the '''void loop()''' function, read the LDR value using '''analogRead()'''.
# Start Bluetooth using the device name '''ESP32_Demo'''.
# Convert the LDR value into a String.
# Display the available commands through Bluetooth.
# Convert the String into a character array.
# In the '''void loop()''' function, check whether Bluetooth data is available.
# Transmit the LDR value through Bluetooth.
# Read the received characters.
# Display the LDR value on the Serial Monitor.
# When a newline character is received, process the complete command.
# Wait for '''300 ms'''.
# 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>
'''LDR → Analog Reading → ESP32 → Bluetooth → LDR Value'''
'''Bluetooth Command → ESP32 → Required Operation'''
</center>
</center>


== Working ==
== Working ==


The ESP32 reads the analog output of the LDR through '''GPIO 35'''.
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>


The analog value is stored in the variable '''ldrVal'''.
<center>
'''OFF → LED OFF'''
</center>
 
For temperature measurement:


<center>
<center>
'''LDR → GPIO 35 → ldrVal'''
'''TEMP → LM35 Reading → Temperature → Bluetooth Output'''
</center>
</center>


The program converts the sensor value into a character array and transmits it through Bluetooth.
For buzzer control:


The Bluetooth device is initialized with the name:
<center>
'''BUZZON → Buzzer ON'''
</center>


<center>
<center>
'''COGESP'''
'''BUZZOFF → Buzzer OFF'''
</center>
</center>


A paired Bluetooth device receives the transmitted sensor data.
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:


At the same time, the sensor value is displayed on the Serial Monitor.
<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>
'''LDR → ESP32 → Bluetooth + Serial Monitor'''
'''TEMP → Read LM35 → Calculate Temperature → Temperature > 35 °C? → Buzzer Alert'''
</center>
</center>


The sensor value is transmitted approximately every '''300 ms'''.
== 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>


== Bluetooth Data Format ==
This sequence is repeated '''5 times'''.


The Bluetooth output is transmitted in the following format:
After completing the sequence, the ESP32 transmits:


<pre>
<pre>
LDR Val: <sensor value>
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>
LDR Val: 856
Received: ON
LDR Val: 872
Received: TEMP
LDR Val: 901
Received: STATUS
</pre>
</pre>


The actual value depends on the light intensity detected by the LDR.
When the temperature is requested, the measured temperature is also displayed on the Serial Monitor.
 
== Bluetooth Output ==
 
When the ESP32 starts, it sends:


== Timing ==
<pre>
ESP32 Bluetooth Ready
Commands:
ON, OFF, TEMP, STATUS
BLINK, BUZZON, BUZZOFF
</pre>


The program uses a delay of '''300 ms''' between successive sensor readings.
For a temperature request, the output is in the format:
 
<pre>
Temperature: 28.45 °C
</pre>


The sequence is:
If the temperature exceeds 35 °C:


<center>
<pre>
'''Read LDR → Convert Value → Transmit Bluetooth → Display Serial → 300 ms Delay → Repeat'''
WARNING: HIGH TEMPERATURE!
</center>
</pre>


== Program ==
== Program ==


<pre>
<pre>
// © 2026, Cognifront
/*
========================================
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"


#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
BluetoothSerial SerialBT;
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif


BluetoothSerial BT;
#define LED_PIN    2
#define LM35_PIN    34
#define BUZZER_PIN  25


#include<DHT.h>
String incomingData = "";


int ldrPin = 35;
void setup() {
int ldrVal;
char msg[30];
String s;


void setup()
  pinMode(LED_PIN, OUTPUT);
{
   pinMode(BUZZER_PIN, OUTPUT);
   Serial.begin(9600);


   BT.begin("COGESP"); //Bluetooth device name
   digitalWrite(LED_PIN, LOW);
  digitalWrite(BUZZER_PIN, LOW);


   Serial.println("The device started, now you can pair it with bluetooth!");
   Serial.begin(115200);


   pinMode(ldrPin, INPUT);
   // 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() {
{
  ldrVal = analogRead(ldrPin);


   s = String (ldrVal);
   while (SerialBT.available()) {


  s.toCharArray(msg, 30);
    char c = SerialBT.read();


  BT.print("LDR Val: ");
    if (c == '\n') {
  BT.println(msg);


  Serial.println(msg);
      incomingData.trim();
      incomingData.toUpperCase();


  delay(300);
      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 LDR sensor value on the Serial Monitor.
Observe the Bluetooth terminal and Serial Monitor while sending different commands.


The value changes according to the intensity of light falling on the LDR.
The following operations can be observed:


The same LDR value is transmitted through Bluetooth to the paired device.
{| 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
|}


The Bluetooth device appears with the name:
When the temperature exceeds '''35 °C''' during a '''TEMP''' command, the buzzer turns ON and a high-temperature warning is transmitted.


<center>
== Result ==
'''COGESP'''
</center>
 
The transmitted output follows the format:


<pre>
The ESP32 successfully interfaces with the '''LM35 temperature sensor, built-in LED and buzzer'''.
LDR Val: xxxx
</pre>


== Result ==
The ESP32 reads the LM35 temperature through GPIO 34 and communicates with a Bluetooth-enabled device using the Bluetooth device name '''ESP32_Demo'''.


The ESP32 successfully interfaces with the '''LDR sensor''' through GPIO 35.
The LED and buzzer can be controlled using Bluetooth commands, while the temperature can be read and transmitted wirelessly.


The ESP32 reads the analog LDR value and displays it on the Serial Monitor.
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:

  1. Start the Arduino IDE.
  2. Include the BluetoothSerial library.
  3. Declare GPIO 2 for the LED.
  4. Declare GPIO 34 for the LM35 analog input.
  5. Declare GPIO 25 for the buzzer.
  6. Create a Bluetooth Serial object.
  7. In the void setup() function, configure the LED and buzzer pins as OUTPUT.
  8. Set the initial state of the LED and buzzer to LOW.
  9. Start Serial communication at 115200 baud.
  10. Start Bluetooth using the device name ESP32_Demo.
  11. Display the available commands through Bluetooth.
  12. In the void loop() function, check whether Bluetooth data is available.
  13. Read the received characters.
  14. When a newline character is received, process the complete command.
  15. Remove unnecessary spaces from the command.
  16. Convert the command to uppercase.
  17. If the command is ON, turn the LED ON.
  18. If the command is OFF, turn the LED OFF.
  19. If the command is TEMP, read the LM35 temperature and transmit it through Bluetooth.
  20. If the temperature is greater than 35 °C, turn the buzzer ON and transmit a warning.
  21. If the command is STATUS, transmit the ESP32 status.
  22. If the command is BLINK, blink the LED five times.
  23. If the command is BUZZON, turn the buzzer ON.
  24. If the command is BUZZOFF, turn the buzzer OFF.
  25. If an unknown command is received, transmit Unknown Command.
  26. 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.