CYTHEIA AIOpen the editor

Light Activated Switch

Turn on an LED when light level drops below a threshold.

Open this circuit and run it

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

Wiring

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

Open this circuit and run it

Next challenge

More projects in Sensors