Jump to content

ESP32 Bluetooth

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

Interfacing LDR with ESP32 and Transmitting Sensor Data 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.

The ESP32 reads the analog value from the LDR and sends the value through Bluetooth to a paired device.

LDR → ESP32 → Bluetooth → Paired Device

Objectives

  • To study the working of the LDR sensor.
  • To interface the LDR sensor with the ESP32 board.
  • To read the analog value from the LDR.
  • To display the LDR value on the Serial Monitor.
  • To transmit the LDR sensor value using Bluetooth.

Software

  • Arduino IDE
  • ESP32 Board Package
  • BluetoothSerial Library

Hardware Modules

  • ESP32 Board
  • LDR Sensor Module
  • PC / Laptop
  • Bluetooth-enabled device
  • Connecting wires

Theory

Introduction to LDR

LDR stands for Light Dependent Resistor.

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

The LDR works on the principle of Photo-Conductivity.

As the intensity of light increases, the resistance of the LDR decreases.

Similarly, as the intensity of light decreases, the resistance of the LDR increases.

More Light → Lower LDR Resistance

Less Light → Higher LDR Resistance

The change in resistance can be converted into a corresponding voltage using an LDR sensor circuit or module.

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:

ESP32 GPIO 35

The analog value is read using the analogRead() function.

ldrVal = analogRead(ldrPin);

The sensor reading is stored in the variable ldrVal.

Connections

Device Pin ESP32 Function
LDR Module Data / Output GPIO 35 Analog sensor input
LDR Module VCC 3.3V Power supply
LDR Module GND GND Ground

Bluetooth Communication

The ESP32 has built-in Bluetooth functionality.

The program uses the BluetoothSerial library to establish Bluetooth Serial communication.

The following library is included:

#include "BluetoothSerial.h"

A Bluetooth Serial object is created using:

BluetoothSerial BT;

The Bluetooth device is initialized with the name:

BT.begin("Sush");

Therefore, the ESP32 Bluetooth device appears as:

Sush

on a device used for Bluetooth pairing.

Bluetooth Data Transmission

After reading the LDR sensor value, the program converts the value into a character array before transmitting it.

The sensor value is first converted into a String:

s = String (ldrVal);

The String is then converted into a character array:

s.toCharArray(msg, 30);

The value is transmitted through Bluetooth using:

BT.print("LDR Val: ");
BT.println(msg);

The transmitted data appears in the following format:

LDR Val: 1234

Serial Monitor Output

The LDR value is also displayed on the Serial Monitor using:

Serial.println(msg);

The Serial Monitor is configured at:

Serial.begin(9600);

Therefore, the Serial Monitor baud rate should be set to 9600.

Algorithm

The following algorithm is used to interface the LDR with the ESP32 and transmit its value using Bluetooth:

  1. Start the Arduino IDE.
  2. Include the BluetoothSerial library.
  3. Include the required sensor library definitions.
  4. Declare GPIO 35 as the LDR input pin.
  5. Declare variables to store the LDR value and transmitted data.
  6. Create a Bluetooth Serial object.
  7. In the void setup() function, start Serial communication at 9600 baud.
  8. Start Bluetooth with the device name Sush.
  9. Configure GPIO 35 as an INPUT.
  10. In the void loop() function, read the LDR value using analogRead().
  11. Convert the LDR value into a String.
  12. Convert the String into a character array.
  13. Transmit the LDR value through Bluetooth.
  14. Display the LDR value on the Serial Monitor.
  15. Wait for 300 ms.
  16. Repeat the process continuously.

The overall operation is:

LDR → Analog Reading → ESP32 → Bluetooth → LDR Value

Working

The ESP32 reads the analog output of the LDR through GPIO 35.

The analog value is stored in the variable ldrVal.

LDR → GPIO 35 → ldrVal

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

The Bluetooth device is initialized with the name:

Sush

A paired Bluetooth device receives the transmitted sensor data.

At the same time, the sensor value is displayed on the Serial Monitor.

LDR → ESP32 → Bluetooth + Serial Monitor

The sensor value is transmitted approximately every 300 ms.

Bluetooth Data Format

The Bluetooth output is transmitted in the following format:

LDR Val: <sensor value>

For example:

LDR Val: 856
LDR Val: 872
LDR Val: 901

The actual value depends on the light intensity detected by the LDR.

Timing

The program uses a delay of 300 ms between successive sensor readings.

The sequence is:

Read LDR → Convert Value → Transmit Bluetooth → Display Serial → 300 ms Delay → Repeat

Program

// © 2026, Cognifront

#include "BluetoothSerial.h"

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

BluetoothSerial BT;

#include<DHT.h>

int ldrPin = 35;
int ldrVal;
char msg[30];
String s;

void setup()
{
  Serial.begin(9600);

  BT.begin("COGESP"); //Bluetooth device name

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

  pinMode(ldrPin, INPUT);
}

void loop()
{
  ldrVal = analogRead(ldrPin);

  s = String (ldrVal);

  s.toCharArray(msg, 30);

  BT.print("LDR Val: ");
  BT.println(msg);

  Serial.println(msg);

  delay(300);
}

Observation

Observe the LDR sensor value on the Serial Monitor.

The value changes according to the intensity of light falling on the LDR.

The same LDR value is transmitted through Bluetooth to the paired device.

The Bluetooth device appears with the name:

Sush

The transmitted output follows the format:

LDR Val: xxxx

Result

The ESP32 successfully interfaces with the LDR sensor through GPIO 35.

The ESP32 reads the analog LDR value and displays it on the Serial Monitor.

The sensor value is also successfully transmitted wirelessly through Bluetooth using the Bluetooth device name Sush.