Jump to content

ESP32 Bluetooth

From cognifront
Revision as of 05:17, 4 September 2026 by CogWikiEditor (talk | contribs)

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.