Illustration: on a windowsill at dusk, an LED controlled by a photoresistor is lit.Sensors
Illustration (made with AI): roughly what the finished project looks like.

A Light That Turns On by Itself in the Dark

A photoresistor measures the light. When the room gets dark the LED turns on by itself and goes off again in daylight, just like street lighting.

Difficulty
Beginner
Time
25 min
Age
8+
Parts
6, 6 on the site

Written by the RoboHub team. A question? Write to us.

Variations

Choose a variation and the parts list updates.

Instructions

  1. A voltage divider

    Connect one leg of the photoresistor to 5V and the other to pin A0. From that same A0 point, run a 10 kΩ resistor to ground. The brighter it is, the higher the voltage at A0.

    D9 is the board's digital pin 9, A0 is an analogue pin, GND is ground. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • Photoresistor 1 → Arduino 5V
    • Photoresistor 2 → Arduino A0
    • 10 kΩ resistor 1 → Arduino A0
    • 10 kΩ resistor 2 → Arduino GND
  2. LED

    Connect the LED through a 330 Ω resistor to pin 9, short leg to ground.

    D9 is the board's digital pin 9, A0 is an analogue pin, GND is ground. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • LED + → 330 ΩArduino D9
    • LED − → Arduino GND
  3. Upload the code

    The code prints the light level to the Serial Monitor, from 0 to 1023.

    Arduino C++
    const int SENSOR = A0, LED = 9;
    const int DARK = 300;                  // below this it counts as dark: set it from your own readings
    bool on = false;
    
    void setup() {
      pinMode(LED, OUTPUT);
      Serial.begin(9600);
    }
    
    void loop() {
      int light = analogRead(SENSOR);      // 0 = dark, 1023 = bright
      Serial.println(light);
      if (light < DARK) on = true;
      else if (light > DARK + 50) on = false;   // a margin, so it doesn't flicker at dusk
      digitalWrite(LED, on ? HIGH : LOW);
      delay(200);
    }
  4. Set the threshold

    Open the Serial Monitor (9600) and see what the sensor reads in daylight and when you cover it with your hand. Put a number between the two in DARK.

    Illustration: at dusk, an LED controlled by a photoresistor is lit.

    Illustration, made with AI

  • Lights and displays
    Beginner45 min10+

    Pedestrian Crossing with a Button

    Cars have green until a pedestrian presses the button. Then the cars get red, the pedestrians get green and a buzzer ticks, just like at a real crossing.

    6/6 parts on the site
  • Sensors
    Beginner1 h10+

    Parking Sensor with Sound and Lights

    An ultrasonic sensor measures the distance. Three LEDs show how close the obstacle is, and the buzzer beeps faster the closer you get.

    6/6 parts on the site
  • Lights and displays
    Beginner30 min8+

    A Colour-Changing Lamp with an RGB LED

    An RGB LED holds three small LEDs: red, green and blue. Mix their brightness and you get any colour, while the lamp slowly changes from one colour to the next.

    5/5 parts on the site