
Video
Circuit Diagram

Source Code
1int value;
2
3void setup() {
4 pinMode(13, OUTPUT);
5}
6
7void loop() {
8 value = analogRead(A0);
9
10 if (value < 50) {
11 digitalWrite(13, HIGH);
12 } else {
13 digitalWrite(13, LOW);
14 }
15}FAQ
How do I determine the threshold value?▾
First, monitor the LDR values in the Serial Monitor: note the values when the environment is bright and when it is dark. Choose a number between these two values and turn on the LED using the condition `if (value < threshold)`.
The LED is flickering around the threshold—how can I prevent this?▾
When the light level is right around the threshold value, small fluctuations cause the LED to turn on and off. If you use two separate thresholds (one for turning on and one for turning off, e.g., 400 and 450)—that is, if you add hysteresis—the flickering disappears.
The LED lights up the LDR and then turns itself off—what should I do?▾
If the LED's light reaches the LDR, the sensor will detect that the environment is bright. Position the LDR away from the LED or so that it is not facing it; if necessary, build a small shield around the LDR.
Can I light a real candle using this method?▾
Yes, you can control a 220 V light by connecting a relay module instead of an LED. Mains voltage is dangerous; make the connections while the device is unplugged, and ask an adult or an electrician for help.
