Light Activated Switch
Turn on an LED when light level drops below a threshold.
- Beginner
- Sensors
- 5 parts
Opens in your browser with the parts, wiring and code already in place. Press Run to watch it work, then change anything you like.
How it works
A light sensor outputs a voltage that changes with brightness, wired to analog pin A0 along with 5V and ground. In this circuit a brighter room gives a bigger number. An LED with a resistor is on pin 13.
The sketch keeps a threshold of 500. Every half second loop() reads the light level (0 to 1023), prints it, and compares it with the threshold. Below 500 means it is dark, so the LED turns on and “Dark - LED ON” is printed; 500 or above means it is bright, so the LED goes off. That is how a night light or an automatic street light works.
Parts
- Arduino Uno
- Light Sensor
- Resistor
- LED
- GND
Wiring
- Arduino Uno 5V → Light Sensor VCC
- Light Sensor OUT → Arduino Uno A0
- Arduino Uno GND → Light Sensor GND
- Arduino Uno D13 → Resistor A
- Resistor B → LED Anode (+)
- LED Cathode (-) → GND
Arduino code
// Light Activated Switch
int threshold = 500;
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop() {
int lightLevel = analogRead(A0);
Serial.print("Light: ");
Serial.println(lightLevel);
if (lightLevel < threshold) {
digitalWrite(13, HIGH);
Serial.println("Dark - LED ON");
} else {
digitalWrite(13, LOW);
Serial.println("Bright - LED OFF");
}
delay(500);
}
Try changing it
- Make it come on only in a much darker room by lowering the threshold: int threshold = 200;
- React faster by changing delay(500) to delay(100).