DIY ELECTRONICSTR

← All projects

Night Light (LDR)

Beginner20 min

A night light that turns itself on as the room gets dark. An LDR (light sensor) and a 100 kΩ resistor form a voltage divider; the Arduino reads the light level on pin A0 and, in the dark, dims the LED on pin 9 with PWM. It teaches analog reading, thresholds and PWM. No extra library needed.

Get it on Google Play

What you need

Wiring steps

  1. 1

    Power the breadboard

    Power the breadboard

    Run a red jumper wire from the 5V pin in the Arduino's POWER section to the red-striped (+) row at the bottom of the breadboard, and a black wire from the GND pin next to it to the blue-striped (−) row. The rest of the circuit takes its power from these two rows.

    • Arduino 5V → bottom red-striped (+) row
    • Arduino GND → bottom blue-striped (−) row
  2. 2

    Plug in the LDR and connect it to A0

    Plug in the LDR and connect it to A0

    Plug the LDR into the bottom half of the breadboard, in columns 3 and 6; an LDR has no polarity, so its legs can go either way. Connect the + row to column 3 with a short red wire. Connect the Arduino's analog pin A0 to column 6 with a yellow wire: this is where the Arduino reads the light level.

    • One LDR leg → column 3
    • Other LDR leg → column 6
    • + row → column 3
    • Arduino A0 → column 6
  3. 3

    Add the 100 kΩ resistor

    Add the 100 kΩ resistor

    Plug the 100 kΩ resistor (brown-black-yellow) from column 6 into the − row. The LDR and this resistor form a voltage divider: in the dark the LDR's resistance rises, the voltage on A0 falls and the reading goes down; in the light it is the other way round. Without the resistor, A0 floats and reads random values. Which way round the resistor goes does not matter.

    • One leg of the 100 kΩ resistor → column 6
    • Other leg of the 100 kΩ resistor → − row
  4. 4

    Connect the LED and its resistor

    Connect the LED and its resistor

    Plug the red LED into columns 10 and 11: the short leg (−) in column 10, the long (bent) leg (+) in column 11. An LED plugged in the wrong way round does not light. Plug the 100 Ω resistor (brown-black-brown) from column 11 into column 15. Connect the Arduino's pin 9 to column 15 with a blue wire, and column 10 to the − row with a short black wire. Pin 9 supports PWM (it has a ~ next to it); that is how the code dims the LED. If you light the LED at full brightness by changing 100 to 255 in the code, use 220 Ω instead of 100 Ω: with 100 Ω about 30 mA flows, more than an Arduino pin can comfortably give.

    • LED short leg (−) → column 10
    • LED long leg (+) → column 11
    • 100 Ω resistor → columns 11 and 15
    • Arduino pin 9 → column 15
    • Column 10 → − row

Arduino sketch

Download the sketch (.ino)

#define LDR_PIN A0
#define LED_PIN 9

// You can't know this threshold
// by heart. Calibrate it for your room's
// light from the values you
// read in the Serial Monitor.
const int DARK_THRESHOLD = 400;

void setup() {
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int ldrValue = analogRead(LDR_PIN);

  // Watch the sensor
  // reading while debugging
  Serial.print("LDR value: ");
  Serial.println(ldrValue);

  // In the dark the LDR's resistance
  // goes up: the voltage on A0 drops,
  // so the analog reading gets smaller.
  if (ldrValue < DARK_THRESHOLD) {
    // A night light: instead of full
    // on/off with digitalWrite, PWM
    // (0-255) limits the
    // brightness to a dim glow.
    analogWrite(LED_PIN, 100);
  } else {
    // the room is bright: off
    analogWrite(LED_PIN, 0);
  }

  // a pause so the ADC isn't hammered
  // and the Serial Monitor stays readable
  delay(200);
}

What the Serial Monitor shows

SERIAL MONITOR · 9600 BAUD
LDR value: 366

What you will see

When the light fades, the LED comes on dimly; when the room is bright again, it goes off. To try it, cover the LDR with your hand. If the LED never lights or always stays on, check the values you read in the Serial Monitor in the dark and in the light, and set DARK_THRESHOLD in the code (400) between the two.

Similar projects

In the app, tick off every wire, match the parts to your inventory and pick up where you left off.

Get it on Google Play