ESP32 और LDR मॉड्यूल से रोशनी की तीव्रता पढ़ना
Serial Monitor में LDR मॉड्यूल की कच्ची ADC वैल्यू और अँधेरे व रोशनी की रेफ़रेंस वैल्यू से निकाला गया सापेक्ष रोशनी प्रतिशत लाइव देखें। यह प्रतिशत लक्स माप नहीं है। मॉड्यूल के कैलिब्रेशन का लॉजिक सिखाता है। इसे LDR नाइट लाइट प्रोजेक्ट से पहले करना सबसे अच्छा है, क्योंकि थ्रेशोल्ड वैल्यू इसी से तय होती है।

वीडियो
सर्किट डायग्राम

सोर्स कोड
1// ─────────────────────────────────────────
2// BlueGrays · bluegrays.com
3// Reading Light Intensity with the ESP32 LDR Module
4// ─────────────────────────────────────────
5// Reads the analog output (AO) of the LDR module with the 12-bit ADC and
6// converts it to a 0-100 light percentage using the calibration values.
7// The result is printed to the Serial Monitor (115200 baud) every 0.5 s.
8//
9// Pins:
10// GPIO 34 (classic ESP32) / GPIO 1 (ESP32-S2/S3) -> LDR module AO
11// LDR module VCC -> 3V3, GND -> GND
12
13#if defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32S3)
14 const int LDR_PIN = 1; // ESP32-S2/S3: ADC1_CH0
15#else
16 // Classic ESP32 DevKit V1: ADC1_CH6, input-only. GPIO 1 clashes with Serial TX here
17 const int LDR_PIN = 34;
18#endif
19
20// Calibration: measure in your own room and enter the values here. On the LDR module the AO voltage
21// (and raw) DROPS as the light gets brighter.
22const int RAW_DARK = 4000; // Dark raw
23const int RAW_LIGHT = 100; // Light raw
24
25void setup() {
26 Serial.begin(115200);
27 analogReadResolution(12); // Force 12-bit
28 Serial.println("LDR Reading");
29 Serial.println("-----------------------------");
30}
31
32void loop() {
33 int raw = analogRead(LDR_PIN);
34 // Clip to the calibration range, dark = 0%
35 int clipped = constrain(raw, RAW_LIGHT, RAW_DARK);
36 int percent = map(clipped, RAW_DARK, RAW_LIGHT, 0, 100);
37
38 Serial.print("Raw: ");
39 Serial.print(raw);
40 Serial.print(" | Light: %");
41 Serial.println(percent);
42
43 delay(500);
44}
45
FAQ
ESP32 का ADC कौन-सी वैल्यू देता है?
ESP32 का ADC 12-बिट है और 0 से 4095 तक की वैल्यू देता है। यह Arduino Uno की 0–1023 रेंज से अलग है, इसलिए अपने थ्रेशोल्ड और गणना उसी हिसाब से बदलें।
Wi-Fi चालू होने पर एनालॉग रीडिंग बिगड़ जाती है। क्यों?
ESP32 पर Wi-Fi चालू रहते ADC2 चैनल (GPIO 0, 2, 4, 12–15, 25–27) इस्तेमाल नहीं किया जा सकता। एनालॉग सेंसर ADC1 पिनों (GPIO 32–39) से जोड़ने पर यह दिक्कत नहीं आती।
क्या रीडिंग असली लक्स वैल्यू है?
नहीं, यह कोड लक्स नहीं निकालता; यह कच्ची ADC वैल्यू और चुनी गई अँधेरे व रोशनी की रेफ़रेंस वैल्यू पर आधारित सापेक्ष रोशनी प्रतिशत दिखाता है। LDR का रिस्पॉन्स लीनियर नहीं होता और मॉडल के हिसाब से बदलता है। सटीक लक्स माप के लिए BH1750 जैसा कैलिब्रेटेड सेंसर इस्तेमाल होता है।
ESP32 का ADC सिरों पर गलत रीडिंग क्यों देता है?
ESP32 का ADC, 0 V और 3.3 V के पास लीनियर नहीं होता। `analogReadMilliVolts()` फ़ंक्शन कैलिब्रेटेड वैल्यू देता है; ज़रूरी माप के लिए 0.1–3.1 V रेंज में काम करने से सबसे सटीक नतीजे मिलते हैं।
