Automatic mood light

An RGB light that switches itself on at dusk and slowly shifts from warm orange to calm blue as the room gets darker.
  • Level:Beginner
  • Duration: 45 min
  • Ages 10+
  • Platform: Arduino
Community project EEMENTOR team

Components used

Automatic mood light – EEMENTOR STEM project

A night light with a personality. When the room is bright, the light stays off. As it gets darker it glows warm orange, and in full darkness it fades to a calm blue. The colours change smoothly, so there are no sudden jumps.

What you need

  • UNO R3 board and USB cable
  • Photoresistor module (45-in-1 Sensor Kit)
  • RGB LED module (45-in-1 Sensor Kit)
  • 3 resistors, 220 Ω
  • Breadboard and jumper wires

Wiring

Breadboard wiring diagram: Automatic mood light
Breadboard view. Check each connection against the wiring table below.
From Through To
UNO pin 9 220 Ω resistor RGB module R
UNO pin 10 220 Ω resistor RGB module G
UNO pin 11 220 Ω resistor RGB module B
RGB module − GND (breadboard − rail)
Photoresistor S UNO A0
Photoresistor + (middle) 5V (breadboard + rail)
Photoresistor − GND (breadboard − rail)

Build it step by step

  1. Place both modules and the three resistors on the breadboard as in the diagram.
  2. Connect 5V and GND to the breadboard rails.
  3. Upload the code and open the Serial Monitor at 9600 baud.
  4. Note the light value in a bright room and when you cover the sensor. Set BRIGHT and DARK to those numbers.
  5. Upload again and slowly cover the sensor to watch the colours change.

The code

const int SENSOR_PIN = A0;
const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;

// Set these from your own Serial Monitor readings
int BRIGHT = 200;   // reading in a bright room
int DARK = 800;     // reading with the sensor covered

float level = 0;    // smoothed darkness, 0 = bright, 1 = dark

void setColor(int r, int g, int b) {
  analogWrite(RED_PIN, r);
  analogWrite(GREEN_PIN, g);
  analogWrite(BLUE_PIN, b);
}

void setup() {
  Serial.begin(9600);
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
}

void loop() {
  int light = analogRead(SENSOR_PIN);
  Serial.println(light);

  // 0 when bright, 1 when dark (works whichever way your module counts)
  float target = (float)(light - BRIGHT) / (DARK - BRIGHT);
  target = constrain(target, 0.0, 1.0);

  // Move slowly towards the target so the colour changes smoothly
  level = level + (target - level) * 0.05;

  if (level < 0.2) {
    setColor(0, 0, 0);                         // bright room: off
  } else if (level < 0.6) {
    float f = (level - 0.2) / 0.4;             // dusk: warm orange, getting brighter
    setColor(255 * f, 80 * f, 0);
  } else {
    float f = (level - 0.6) / 0.4;             // dark: orange fades to blue
    setColor(255 * (1 - f), 80 * (1 - f), 200 * f);
  }
  delay(30);
}

How it works

The sketch turns the light reading into a "darkness level" between 0 and 1, using your own bright and dark readings so it works with any module. Instead of jumping straight to the new level, it moves 5% of the way each time, which makes the colours fade smoothly. Three ranges then decide the colour: off, warm orange at dusk, and a blend from orange to blue in the dark.

Make it your own

  • Pick your own colours for dusk and darkness.
  • Make the light "breathe" slowly in the dark by changing the brightness over time.
  • Put the light in a jar or behind paper to soften the glow.

Troubleshooting

  • The light never turns on: check your BRIGHT and DARK numbers against the Serial Monitor.
  • A colour is missing: check that colour's resistor and wire.
  • Colours are inverted: your RGB LED may be common anode. Connect the common pin to 5V and use 255 - value in setColor().

Learn the basics first

New to these parts? These lessons explain them step by step: Build an automatic night light and Mix colours with the RGB LED module.

0 comments

Leave a comment

Please note, comments need to be approved before they are published.

Build it yourself

Get the parts used in this project, tested by our engineers.

45-in-1 Sensor Kit | Compatible with Arduino IDE and ESP32 $29.00

Shop the parts

More projects

  • Pulse visualiser

    • Level:Intermediate
    • Duration: 45 min
    • Platform: Arduino

    by EEMENTOR team

  • Flame alarm

    • Level:Beginner
    • Duration: 40 min
    • Platform: Arduino

    by EEMENTOR team

Community projects

Built something awesome? Share it.

Send us your project and we'll feature it in the hub, credited to you, so other makers can learn from it.

  1. Sign in and tell us about your build
  2. Our team reviews your submission
  3. We publish it in the hub, credited to you