Overview
Learn how to use the KY-018 LDR light sensor with an ESP32 to measure brightness. This project is useful for automatic lights, light-activated alarms, and day/night detection.
Instructions
Step by step
1. Connect the LDR VCC pin to 3.3V.
2. Connect the LDR GND pin to GND.
3. Connect the LDR S pin to D34.
4. Run the code and open the Serial Monitor.
5. Change the light level in the simulator.
6. A higher value means brighter light.
7. A lower value means darker light.
Code
Source code
const int ldrPin = 34;
void setup() {
Serial.begin(115200);
Serial.println("KY-018 LDR Light Sensor Test");
}
void loop() {
int lightValue = analogRead(ldrPin);
Serial.print("Light level: ");
Serial.println(lightValue);
delay(1000);
}
Related