Arduino Automatic Night Light with LDR Light Sensor (Parts List and Code)

Arduino ile akıllı gece lambası projesi kapak görseli

🇹🇷 Bu yazıyı Türkçe okuyun

What does this project do?

We build a night light that turns on by itself as the room gets darker and turns off as it gets brighter. A light-dependent resistor (LDR) measures the light in the room, and the Arduino sets the LED brightness to match. When the room is completely dark, the LED shines at full brightness.

  • Age group: 9 and up
  • Level: Beginner
  • Time: About 30 minutes
  • What you will learn: reading an analog sensor, using map() and constrain(), dimming an LED with PWM

Parts list

PartQty
Arduino Uno (or compatible)1
LDR (light-dependent resistor)1
10 kΩ resistor1
White or yellow LED1
220 Ω resistor1
Breadboard1
Male-to-male jumper wires~8
USB cable1

Wiring

Arduino automatic night light with LDR light sensor wiring diagram
ComponentConnectionArduino pin
LDROne leg5V
LDROther legA0
10 kΩ resistorBetween A0 and GNDA0 / GND
LEDLong leg (through 220 Ω)D9
LEDShort legGND

The LDR and the 10 kΩ resistor together form a voltage divider. The more light there is, the higher the value on A0.

Code

// Arduino Automatic Night Light - ozandamci.com

const int ldrPin = A0;
const int ledPin = 9;   // PWM pin (marked with ~)

// Adjust these for your own room (check the values in the Serial Monitor)
const int darkValue = 200;    // below this is "completely dark"
const int brightValue = 700;  // above this is "bright enough"

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

void loop() {
  int light = analogRead(ldrPin);   // 0 - 1023
  Serial.print("Light: ");
  Serial.println(light);

  // Keep the value within the limits, then invert it into a brightness level
  int limited = constrain(light, darkValue, brightValue);
  int brightness = map(limited, darkValue, brightValue, 255, 0);

  analogWrite(ledPin, brightness);
  delay(100);
}

Step-by-step instructions

  1. Place the LDR. Put the LDR on the breadboard. Connect one leg to the Arduino’s 5V pin.
  2. Build the voltage divider. Connect the other leg of the LDR to A0. Put the 10 kΩ resistor at the same point and connect its other end to GND.
  3. Place the LED. Connect a 220 Ω resistor to the long leg of the LED and wire it to D9; connect the short leg to GND.
  4. Upload the code. In the Arduino IDE, choose your board and port, then click Upload.
  5. Calibrate. Open Tools → Serial Monitor. Write down the value in normal room light and the value when you cover the LDR with your hand. Put these two numbers into brightValue and darkValue in the code and upload it again.
  6. Test it. Slowly cover the LDR with your hand. The LED should get brighter and brighter.

Troubleshooting

  • The LED is always fully on or always dim: The calibration values may not suit your room; repeat step 5.
  • It works backwards (lights up in bright light): The LDR and the 10 kΩ resistor have been swapped; the LDR must be on the 5V side.

Try it yourself

  • Use an RGB LED instead and make a lamp that changes colour in the dark.
  • Put the lamp inside a cardboard or 3D-printed box.

Video

Video narration is in Turkish.

Birisi “Arduino Automatic Night Light with LDR Light Sensor (Parts List and Code)” üzerinde düşündü

Bir Cevap Yazın