Night light that turns on when someone is present (Arduino)
FreeINO

📡 IoT – Embedded programming · 🛠️ DIY · for use with Professional plan · IoT – Embedded programming pack

Night light that turns on when someone is present (Arduino)

DIY 5V LED night light that turns on when someone walks by in the dark: Arduino Nano, PIR sensor, photoresistor. Free source code included.

Downloadable files

  • den_ngu.ino
    Arduino source code · 1 KB
    Download

License CC BY 4.0 — free to use, credit “OctaX.one”. Demo the production version requires a free account to download.

Want to customize it?

The OctaX assistant adjusts dimensions, content and branding on request — estimated ≈ 3 credits.

✦ Customize with AI

Use a 5V supply only. Do not connect to the 220V mains yourself unless you are qualified.

Materials

  • Arduino Nano
  • PIR motion sensor HC-SR501
  • Photoresistor + 10 kΩ resistor
  • MOSFET IRLZ44N + resistors 220 Ω, 100 kΩ
  • 5V LED strip (≤ 1 m)
  • 5V 2A power supply

Steps

  1. Connect the PIR sensor HC-SR501: VCC → 5V, GND → GND, OUT → pin D2.
  2. Connect the photoresistor with a 10 kΩ resistor as a voltage divider into pin A0.
  3. Connect the 5V LED strip through a logic-level N-channel MOSFET (e.g. IRLZ44N): gate G → D9 via a 220 Ω resistor, plus a 100 kΩ resistor from G to GND.
  4. Upload the code, read the A0 value with the room lights off, then adjust NGUONG_TOI accordingly.
  5. Put it in a box and aim the sensor at the walkway, about 30–50 cm above the floor.

Source code

// Đèn ngủ tự bật khi có người trong bóng tối — OctaX.one (CC BY 4.0)
// Bo: Arduino Nano. PIR → D2, quang trở (cầu chia áp với 10 kΩ) → A0, MOSFET điều khiển dải LED → D9 (PWM).
const int PIN_PIR = 2;
const int PIN_LDR = A0;
const int PIN_LED = 9;
const int NGUONG_TOI = 600;              // A0 lớn hơn ngưỡng này coi là tối (đo thực tế rồi chỉnh)
const unsigned long GIU_SANG = 60000UL;  // giữ đèn sáng 60 giây sau lần cuối thấy người
const int DO_SANG = 140;                 // 0–255: đèn ngủ không cần sáng tối đa

unsigned long lanCuoiCoNguoi = 0;
int doSangHienTai = 0;

void setup() {
  pinMode(PIN_PIR, INPUT);
  pinMode(PIN_LED, OUTPUT);
}

void loop() {
  bool coNguoi = digitalRead(PIN_PIR) == HIGH;
  bool troiToi = analogRead(PIN_LDR) > NGUONG_TOI;

  // Chỉ "ghi nhận có người" khi trời tối, để ban ngày đèn không bật
  if (coNguoi && troiToi) lanCuoiCoNguoi = millis();

  int mucTieu = (lanCuoiCoNguoi && millis() - lanCuoiCoNguoi < GIU_SANG) ? DO_SANG : 0;

  // Sáng/tắt dần cho dịu mắt: mỗi vòng thay đổi 1 nấc
  if (doSangHienTai < mucTieu) doSangHienTai++;
  else if (doSangHienTai > mucTieu) doSangHienTai--;
  analogWrite(PIN_LED, doSangHienTai);
  delay(8);
}

Templates in the same field