================================================================================
  Question 2: ESP32 LDR-Based Light Intensity Monitoring [30 Marks]
================================================================================

FULL QUESTION:
CAE 2 IoT Practical Exam - Section A (Answer either Q1 or Q2):
Q2. LDR-Based Light Intensity Monitoring [30 Marks]
Develop an ESP32-based light intensity monitoring system using an LDR sensor.
The system should:
- Interface the LDR sensor with ESP32.
- Read the light intensity from the sensor.
- Display the sensor readings through the Serial Monitor.
- Demonstrate the working of the system with suitable sensor readings.

SHORT QUESTION:
ESP32 LDR Light Intensity Monitoring with ADC Reading on Serial Monitor [30 Marks].

================================================================================
README & SETUP INSTRUCTIONS:
================================================================================
# Question 2: ESP32 LDR-Based Light Intensity Monitoring

## Problem Statement [30 Marks]
Develop an ESP32-based light intensity monitoring system using an LDR sensor.
The system should:
1. Interface the LDR sensor with ESP32.
2. Read the light intensity from the sensor.
3. Display the sensor readings through the Serial Monitor.
4. Demonstrate the working of the system with suitable sensor readings.

---

## Hardware Wiring
| LDR Module Pin | ESP32 Pin | Description |
| :--- | :--- | :--- |
| **VCC** | **3V3** | 3.3V Power Supply |
| **GND** | **GND** | Ground |
| **AO** | **GPIO 34** | Analog Voltage (Light Intensity ADC input) |
| **DO** | **GPIO 35** | Digital Output (Comparator threshold indicator) |

---

## How to Run in Wokwi Simulator
1. Open [wokwi.com](https://wokwi.com) and create an **ESP32** project.
2. Paste `sketch.ino` into the code tab.
3. Paste `diagram.json` into the `diagram.json` tab.
4. Click **Start Simulation (Play)**.
5. Open the **Serial Monitor** at **115200 baud**.
6. Click the LDR sensor in Wokwi and drag the Lux slider up and down to observe raw ADC changes, light intensity percentages, and status changes.

---

## Expected Output (Serial Monitor)
```text
==================================================
  ESP32 LDR Light Intensity Monitoring System
==================================================
LDR Sensor Initialized successfully.
Streaming light intensity readings every 1 second...

Raw ADC: 3420  |  Light Intensity: 83.5 %  |  Status: BRIGHT (High Illumination)
Raw ADC: 1850  |  Light Intensity: 45.2 %  |  Status: MODERATE (Ambient Light)
Raw ADC: 410   |  Light Intensity: 10.0 %  |  Status: DARK (Low / No Light)
```

================================================================================
SOURCE CODE FILES:
================================================================================

1. sketch.ino
--------------------------------------------------------------------------------
#define LDR_ANALOG_PIN  34
#define LDR_DIGITAL_PIN 35

void setup() {
  Serial.begin(115200);
  delay(1000);
  pinMode(LDR_ANALOG_PIN, INPUT);
  pinMode(LDR_DIGITAL_PIN, INPUT);

Serial.println("LDR Sensor Initialized successfully.");
  Serial.println("Streaming light intensity readings every 1 second...\n");
}

void loop() {

  int rawADC = analogRead(LDR_ANALOG_PIN);

  int digitalVal = digitalRead(LDR_DIGITAL_PIN);

  float lightPercent = (rawADC / 4095.0) * 100.0;

  String statusStr;
  if (lightPercent > 65.0 || digitalVal == LOW) {
    statusStr = "BRIGHT (High Illumination)";
  } else if (lightPercent > 25.0) {
    statusStr = "MODERATE (Ambient Light)";
  } else {
    statusStr = "DARK (Low / No Light)";
  }

  Serial.print("Raw ADC: ");
  Serial.print(rawADC);
  Serial.print("  |  Light Intensity: ");
  Serial.print(lightPercent, 1);
  Serial.print(" %  |  Status: ");
  Serial.println(statusStr);

  delay(1000);
}
--------------------------------------------------------------------------------

2. diagram.json
--------------------------------------------------------------------------------
{
  "version": 1,
  "author": "IoT Practical Exam",
  "editor": "wokwi",
  "parts": [
    { "type": "board-esp32-devkit-c-v4", "id": "esp", "top": 0, "left": 0, "attrs": {} },
    { "type": "wokwi-photoresistor-sensor", "id": "ldr1", "top": 0.3, "left": -159, "attrs": {} }
  ],
  "connections": [
    [ "esp:TX", "$serialMonitor:RX", "", [] ],
    [ "esp:RX", "$serialMonitor:TX", "", [] ],
    [ "ldr1:GND", "esp:GND.2", "black", [ "v0" ] ],
    [ "ldr1:VCC", "esp:3V3", "red", [ "v0" ] ],
    [ "ldr1:AO", "esp:34", "green", [ "v0" ] ],
    [ "ldr1:DO", "esp:35", "blue", [ "v0" ] ]
  ],
  "dependencies": {}
}
--------------------------------------------------------------------------------
