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.

Build it step by step in the app

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

#define LDR_PIN A0
#define LED_PIN 9

// Bu eşik değerini ezbere yazamazsın. Odandaki ışık durumuna göre
// Serial Monitor'den okuduğun değerlere bakarak kalibre etmelisin.
const int KARANLIK_ESIGI = 400;

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

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

  // Hata ayıklama için sensör verisini izle
  Serial.print("LDR Degeri: ");
  Serial.println(ldrDegeri);

  // LDR karanlıkta yüksek direnç gösterir. A0'a giden voltaj düşeceği için
  // okunan analog değer azalır.
  if (ldrDegeri < KARANLIK_ESIGI) {
    // Gece lambası olduğu için digitalWrite ile 1/0 yapmak yerine
    // PWM ile parlaklığı sınırlandırarak (0-255 arası) daha loş yakıyoruz.
    analogWrite(LED_PIN, 100);
  } else {
    analogWrite(LED_PIN, 0); // Ortam aydınlıksa kapat
  }

  delay(200); // ADC'yi sürekli yormamak ve Serial'i okunabilir tutmak için gecikme
}

What the Serial Monitor shows

SERIAL MONITOR · 9600 BAUD
LDR Degeri: 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 KARANLIK_ESIGI in the code (400) between the two.

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

Build it step by step in the app