This project presents the design and implementation of an intelligent lighting control system utilizing the ESP32 microcontroller platform. The system integrates multiple sensor modalities and communication protocols to provide autonomous and user-controlled lighting management. Three independent bulb control channels are implemented, each responding to distinct input mechanisms: infrared proximity detection, passive infrared motion sensing, and Bluetooth serial communication. A web-based graphical user interface enables centralized control via Wi-Fi connectivity, demonstrating practical integration of IoT principles in residential automation applications.
- Introduction
- System Architecture
- Hardware Components
- Software Architecture
- Implementation Details
- User Interfaces
- Testing and Validation
- Setup and Installation
- Results and Discussion
- Conclusion
- License
The primary objectives are: (1) Design multi-channel lighting control using ESP32 microcontroller, (2) Integrate heterogeneous sensor inputs for autonomous control, (3) Implement multiple communication protocols (Wi-Fi, Bluetooth Classic), (4) Develop responsive web-based user interface, and (5) Validate functionality through modular component testing.
The system controls three independent lighting channels (Bulb B1, B2, and B3), each with distinct control mechanisms, demonstrating a comprehensive IoT lighting solution suitable for residential or small-scale commercial applications.
The system implements a four-layer hierarchy with ESP32 as the central controller: (1) User Interface Layer (web GUI, Bluetooth terminal, physical sensors), (2) Communication Layer (Wi-Fi server, Bluetooth Serial, digital I/O), (3) Processing Layer (ESP32 controller, state management), and (4) Actuator Layer (relay modules, LCD display).
| Channel | Control Method | Sensor Type | GPIO Pins | Timeout |
|---|---|---|---|---|
| Bulb B1 | IR Proximity + Manual | IR Obstacle Detection | GPIO 15 (Input), GPIO 4 (Output) | 60 seconds |
| Bulb B2 | PIR Motion + Manual | Passive Infrared | GPIO 27 (Input), GPIO 26 (Output) | 60 seconds |
| Bulb B3 | Manual Only | N/A | GPIO 25 (Output) | No timeout |
Control Priority: Sensor activation triggers automatic 60-second timeout. Manual control (Wi-Fi/Bluetooth) overrides sensor state and resets timers.
| Component | Specification | Quantity | Purpose |
|---|---|---|---|
| ESP32 Dev Board | Dual-core Xtensa LX6, 240 MHz, Wi-Fi + BT | 1 | Main microcontroller |
| IR Obstacle Sensor | Detection range: 2-30 cm, Digital output | 1 | Proximity detection for B1 |
| PIR Motion Sensor | HC-SR501, Range: 7m, Angle: 120Β° | 1 | Motion detection for B2 |
| 1-Ch Relay Module | 5V trigger, 10A/250VAC switching | 3 | AC bulb switching |
| 16Γ2 I2C LCD | PCF8574 backpack, Address: 0x27 | 1 | System status display |
| AC Bulbs | 230V/110V, E27 socket | 3 | Lighting outputs |
| Power Supply | 5V/2A for ESP32 + Sensors | 1 | System power |
ESP32 GPIO Mapping:
βββ Sensor Inputs
β βββ GPIO 15 β IR Sensor (Digital Input)
β βββ GPIO 27 β PIR Sensor (Digital Input)
β
βββ Relay Outputs (Active-LOW)
β βββ GPIO 4 β Bulb B1 Relay
β βββ GPIO 26 β Bulb B2 Relay
β βββ GPIO 25 β Bulb B3 Relay
β
βββ I2C Interface (LCD)
βββ GPIO 21 β SDA
βββ GPIO 22 β SCL
Relay Configuration: All relays operate with active-LOW logic. GPIO pins initialized HIGH during setup() to prevent spurious activation during boot.
Sensor Integration: IR sensor provides active digital output (no external pull resistor required). PIR sensor configured with INPUT_PULLUP for noise immunity.
Power Distribution: ESP32 and sensors powered from 5V rail. Relays switch independent AC circuit. Galvanic isolation maintained between control (5V DC) and switched (230V AC) circuits.
- Platform: Arduino IDE 2.x with ESP32 board support
- Core Library: ESP32 Arduino Core v2.x
- Language: C++ (Arduino framework)
| Library | Source | Purpose |
|---|---|---|
WiFi.h |
ESP32 Built-in | Wi-Fi connectivity and HTTP server |
BluetoothSerial.h |
ESP32 Built-in | Bluetooth Classic SPP communication |
Wire.h |
ESP32 Built-in | I2C protocol for LCD |
LiquidCrystal_I2C.h |
Frank de Brabander | 16Γ2 LCD control via I2C |
Final_Code/
βββ Final_Code.ino # Main program (setup and loop)
βββ functions.h # Sensor and timer handling functions
βββ wifi_handling.h # HTTP server and web GUI generation
Design Pattern: Modular header inclusion enables logical separation while maintaining shared state access.
The loop() function implements non-blocking event-driven architecture:
void loop() {
handleIR(); // Poll IR sensor, activate B1 if detected
handlePIR(); // Poll PIR sensor, activate B2 if detected
handleBluetooth(); // Process incoming Bluetooth commands
handleWiFi(); // Service HTTP client requests
handleTimers(); // Manage automatic timeouts for B1/B2
updateLCD(); // Refresh LCD display (1 Hz rate)
}IR Proximity Detection (Bulb B1):
- Active-LOW detection when object within 2-3 cm
- Activates B1, records timestamp; auto-deactivates after 60-second timeout
PIR Motion Detection (Bulb B2):
- Warm-up period: 30 seconds after power-on
- Activates B2, records timestamp; timer resets on continued motion detection
- Auto-deactivates after 60-second timeout with no motion
Bluetooth Serial (SPP):
- Device name:
ESP32_SMART_HOME - Pairing PIN:
1234(default) - Commands:
B3ON/B3OFF(newline-terminated, case-sensitive)
Wi-Fi Web Server:
- HTTP server on port 80
- Connects to configured Wi-Fi network
- Serves responsive HTML/CSS control interface
HTTP Endpoints:
| Endpoint | Action |
|---|---|
/B1ON / /B1OFF |
Control Bulb B1 |
/B2ON / /B2OFF |
Control Bulb B2 |
/B3ON / /B3OFF |
Control Bulb B3 |
/ALLON / /ALLOFF |
Control all bulbs |
/ |
Serve control GUI |
LCD updates at 1 Hz showing real-time bulb status:
Display Format:
Line 1: IR PIR BT
Line 2: ON OFF ON
The system serves a responsive HTML5 interface with embedded CSS, providing individual bulb control and master ON/OFF switches.
Features:
- Real-time color-coded status indicators
- Individual bulb ON/OFF buttons and master controls
- Mobile-responsive design; self-contained (no external dependencies)
- Dark theme with green (#22c55e) ON indicators, glow effects, Google Fonts (Rajdhani)
Control via Serial Bluetooth Terminal (Kai Morich, available on Google Play Store).
No Wi-Fi network infrastructure is required. See Operational Usage for the full connection procedure.
The Components_Testing/ directory contains isolated test sketches for incremental validation:
Test Modules:
- ESP32 Initialization - GPIO configuration, Serial communication
- I2C LCD Communication - LCD address detection (0x27), display control
- IR Sensor + Bulb B1 - Proximity detection, relay switching, timeout behavior
- PIR Sensor + Bulb B2 - Motion detection, countdown timer, warm-up validation
- Bluetooth Serial - Pairing, command reception, string parsing (
B3ON/B3OFF) - Wi-Fi Connectivity - Network connection, DHCP, HTTP server functionality
Stage 1 - Hardware Verification: ESP32 programming, Serial output validation, I2C bus scan, LCD rendering and backlight control.
Stage 2 - Sensor Characterization: IR range calibration (potentiometer adjustment), PIR warm-up verification (30s stabilization), relay active-LOW switching validation.
Stage 3 - Communication Protocols: Bluetooth pairing and bidirectional echo test, Wi-Fi connection establishment, HTTP GET request handling.
Stage 4 - Integration Testing: Multi-channel simultaneous control, timer functionality under concurrent sensor activation, LCD refresh rate optimization.
- Hardware Assembly: Connect components according to Pin Configuration
- Software Requirements:
- Arduino IDE 2.x or later
- ESP32 board support:
https://dl.espressif.com/dl/package_esp32_index.json - LiquidCrystal_I2C library (install via Library Manager)
Tools β Board β ESP32 Arduino β ESP32 Dev Module
Tools β Partition Scheme β Huge APP (3MB No OTA/1MB SPIFFS)
Tools β Upload Speed β 115200
Edit Final_Code.ino lines 17-18 with your Wi-Fi credentials:
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";Security Note: Remove credentials before committing to version control.
- Connect ESP32 via USB cable
- Select correct COM port:
Tools β Port β COMX - Click Upload (Ctrl+U) and monitor Serial output at 115200 baud
Expected Serial Output:
Bluetooth Ready
WiFi Connected
IP address: 192.168.x.x
LCD Display Sequence:
- "Smart Bulb System" β "Connecting WiFi" (during connection)
- "WiFi Connected" β IP address display (2 seconds)
- "IR PIR BT" β "OFF OFF OFF" (operational status)
Automatic Sensor Control:
- Approach IR sensor within 3 cm β B1 activates for 60 seconds
- Move within PIR detection range (7m, 120Β°) β B2 activates for 60 seconds
- Timer resets on continued detection
Manual Web Control:
- Connect device to same Wi-Fi network as ESP32
- Navigate to IP address shown on LCD:
http://192.168.x.x - Use individual bulb buttons or master controls (overrides sensor state)
Manual Bluetooth Control:
- Pair Android device with
ESP32_SMART_HOME(PIN: 1234) - Open Serial Bluetooth Terminal β connect to paired device
- Send
B3ONorB3OFFand observe confirmation messages
| Symptom | Probable Cause | Resolution |
|---|---|---|
| "text section exceeds space" error | Wrong partition scheme | Set Tools β Huge APP partition |
| LCD shows no text | Incorrect I2C address | Run Identifying_I2C_address.ino |
| Wi-Fi connection fails | Wrong credentials/band | Verify SSID/password, use 2.4 GHz |
| Bluetooth not discoverable | Sketch not running | Confirm "Bluetooth Ready" in Serial |
| Relay clicks but bulb stays off | Wiring issue | Verify relay COM/NO connections |
| PIR false triggers immediately | Warm-up incomplete | Wait full 30 seconds after power-on |
All primary objectives were successfully achieved. Three independent control mechanisms (IR, PIR, Bluetooth, Wi-Fi) operate without interference. The automatic 60-second timeout balances energy efficiency with user convenience.
- Hallway/Corridor (B1 - IR): Automatic activation when approaching doorway
- Bathroom/Garage (B2 - PIR): Motion-based lighting with automatic shutoff
- Bedroom/Living Room (B3 - Manual): Full manual control via web/Bluetooth
Current Implementation: Blocking web server (acceptable for single-user scenarios). Global boolean state management provides simple and effective tracking. Security considerations β plaintext Wi-Fi credentials and default Bluetooth PIN β are suitable for educational/personal use.
Advantages Over Commercial Solutions:
- Open-source, fully customizable, no cloud dependency or subscription fees
- Multi-protocol flexibility (Wi-Fi, Bluetooth, sensors)
- High educational value for IoT learning
This project demonstrates a functional ESP32-based smart lighting system that effectively balances autonomous sensor-driven operation with manual control flexibility. The modular software architecture and comprehensive component testing methodology provide a robust foundation for residential automation applications, while the open-source codebase enables further customization for specific deployment requirements.
This project is licensed under the MIT License. See LICENSE file for complete terms.
Project Repository: https://github.com/Shass27/A_Smart_Bulb_System
Author: Shaswath S
Last Updated: March 2026

