⭐⭐ Advanced beginner ⏱️ 15 minutes

Night Light

You make a smart night light. When it gets dark, the LED ring glows softly. When it gets light, it turns off again.

First you log in for a moment. Then your project is ready right away.

🧰 What do you need?

1× Arduino UNO
1× Light sensor (LDR)
1× RGB LED ring (8 LEDs)
🔌
USB cable + Jumper wires

🗺️ Wiring diagram

Follow the wires from the Arduino to each part. You can choose the wire colors yourself.

From To
Arduino UNO · A0 Light sensor (LDR) · S
Arduino UNO · 5V Light sensor (LDR) · +
Arduino UNO · GND Light sensor (LDR) · -
Arduino UNO · D6 RGB LED ring (8 LEDs) · DI
Arduino UNO · 5V RGB LED ring (8 LEDs) · 5V
Arduino UNO · GND RGB LED ring (8 LEDs) · GND

🛠️ This is how you build it

  1. Connect the light sensor: S to A0, + to 5V and − to GND.
  2. Connect the LED ring: DI to pin 6, 5V to 5V and GND to GND.
  3. Open the serial monitor and look at the number you see in the light and in the dark. Change DARK_LIMIT to match.

💻 The code

// Night light
// Is it getting dark? Then the LED ring glows softly. Is it getting light? Then it turns off.

#include <Adafruit_NeoPixel.h>

const int LIGHT_PIN = A0;   // S of the light sensor on A0
const int RING_PIN = 6;     // DI of the LED ring on pin 6
const int LED_COUNT = 8;

// At this value, we call it "dark". Look in the serial monitor which number you see
// in light and in the dark, and put a number in between here.
const int DARK_LIMIT = 500;

Adafruit_NeoPixel ring(LED_COUNT, RING_PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  Serial.begin(9600);
  ring.begin();
  ring.setBrightness(60);
  ring.show();
}

void loop() {
  int light = analogRead(LIGHT_PIN);   // A number from 0 to 1023
  Serial.print(F("Light sensor: "));
  Serial.println(light);

  // With this sensor, the number gets higher when it gets darker.
  // Does it work the other way round for you? Then change > to <.
  bool dark = light > DARK_LIMIT;

  for (int i = 0; i < LED_COUNT; i++) {
    if (dark) {
      ring.setPixelColor(i, ring.Color(255, 140, 40));  // Warm yellow light
    } else {
      ring.setPixelColor(i, 0);                         // Off
    }
  }
  ring.show();
  delay(200);
}

🧠 What does the code do?

  • The light sensor is on A0. analogRead() gives a number from 0 to 1023.
  • Is the number higher than DARK_LIMIT? Then we call it dark.
  • In the dark, we give all the lights a warm yellow color. Otherwise they turn off.

🎯 Try this…

  • Let the light slowly get brighter as it gets darker
  • Let the color slowly change, like a relaxing mood light
  • Let the light turn off by itself after 1 minute

🚀 Build this project with Codey: open codey.online/starter-kits/uno/projects/night-light and click MAKE THIS. Then the code and the wiring diagram are ready right away.

First you log in for a moment. Then your project is ready right away.
⬇️ Download this page as a PDF 📚 Download the complete tutorial (PDF)

Codey Online is a product of OTRONIC

Build your Arduino or ESP32 project with AI

Describe what you want to build. Codey Online writes the code, draws the wiring diagram, compiles it and flashes your board straight from the browser.

Start for free