================================================================================
  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
--------------------------------------------------------------------------------
/*
================================================================================
CAE 2 – IOT PRACTICAL EXAMINATION
QUESTION 2: LDR-Based Light Intensity Monitoring [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.
================================================================================
*/

// Short Description: ESP32 monitors ambient light intensity using an LDR sensor (ADC analog reading and digital threshold) and streams formatted light readings to the Serial Monitor at 115200 baud.

/*
README / BEFORE RUNNING:
- Prerequisites: Wokwi Simulator or physical ESP32 board with Arduino IDE.
- Hardware Wiring (ESP32 DevKit):
    * LDR Module VCC -> ESP32 3V3 (Power)
    * LDR Module GND -> ESP32 GND (Ground)
    * LDR Module AO  -> ESP32 GPIO 34 (Analog Light Intensity input, ADC1)
    * LDR Module DO  -> ESP32 GPIO 35 (Digital Threshold indicator)
- How to Run:
    1. In Wokwi: Load sketch.ino and diagram.json, then click 'Start Simulation'.
    2. Physical Hardware: Select 'DOIT ESP32 DEVKIT V1' in Arduino IDE, select COM port, and upload.
    3. Open Serial Monitor set to 115200 baud.
- Expected Behavior:
    * Prints "LDR Light Intensity System Initialized..." on startup.
    * Every 1 second, displays: "Raw ADC: XXXX | Light Intensity: YY.Y % | Status: BRIGHT/DARK".
    * In Wokwi, click the LDR sensor and move the Lux slider to simulate varying light conditions.
*/

#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("==================================================");
  Serial.println("  ESP32 LDR Light Intensity Monitoring System");
  Serial.println("==================================================");
  Serial.println("LDR Sensor Initialized successfully.");
  Serial.println("Streaming light intensity readings every 1 second...\n");
}

void loop() {
  // Read analog light value (0 - 4095 on 12-bit ESP32 ADC)
  int rawADC = analogRead(LDR_ANALOG_PIN);
  
  // Read digital threshold pin (LOW = Bright, HIGH = Dark on typical modules)
  int digitalVal = digitalRead(LDR_DIGITAL_PIN);

  // Convert raw ADC reading to an intensity percentage (0.0% to 100.0%)
  float lightPercent = (rawADC / 4095.0) * 100.0;

  // Determine light level category
  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)";
  }

  // Print formatted output to Serial Monitor
  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": {}
}
--------------------------------------------------------------------------------
