#include // LDR Sensor connected to Analog Pin A0 const int ldrPin = A0; // Reference voltage for XIAO nRF52840 const float vRef = 3.3; const int adcMax = 4095; // 12-bit ADC void setup() { // Initialize Serial communication at 115200 baud Serial.begin(115200); delay(1000); Serial.println("=== LDR (Light Dependent Resistor) Analysis ==="); Serial.println("Reading analog values from light sensor"); Serial.println("Connect LDR to A0 (via voltage divider with 10kΩ resistor)"); Serial.println(""); Serial.println("Time(ms)\tRaw Value\tVoltage(V)\tLight Level"); Serial.println("=".repeat(55)); } void loop() { // Read raw ADC value from LDR int rawValue = analogRead(ldrPin); // Convert ADC value to voltage // Formula: V = (ADC_reading / ADC_max) * Vref float voltage = (rawValue / (float)adcMax) * vRef; // Convert voltage back to estimated light level (0-100%) // Lower voltage = high light (low resistance) // Higher voltage = low light (high resistance) float lightPercent = (1.0 - (voltage / vRef)) * 100.0; // Constrain to 0-100 range if (lightPercent < 0) lightPercent = 0; if (lightPercent > 100) lightPercent = 100; // Print data with timestamp unsigned long currentTime = millis(); Serial.print(currentTime); Serial.print("\t"); Serial.print(rawValue); Serial.print("\t"); Serial.print(voltage, 3); Serial.print("\t"); Serial.print(lightPercent, 1); Serial.println("%"); // Delay 100ms between readings (10 readings per second) delay(100); } /* CIRCUIT CONFIGURATION: ====================== LDR Pin Configuration: - LDR_End1 → VCC (3.3V) - LDR_End2 → A0 (analog input) - 10kΩ Resistor → From A0 to GND - This forms a voltage divider This setup allows XIAO to read continuous analog voltage that varies with light intensity. Use oscilloscope on A0 to observe real-time waveform changes. EXPECTED BEHAVIOR: ================== - Bright light: Raw: 200-500, Voltage: 0.2-0.4V, Light: 75-95% - Ambient light: Raw: 1000-2000, Voltage: 0.8-1.6V, Light: 25-75% - Covered/Dark: Raw: 2500-4000, Voltage: 2.0-3.2V, Light: 0-25% The continuous voltage output is characteristic of ANALOG signals. No discrete steps—purely proportional to light intensity. */