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
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)
FREE

Build this circuit in EEMENTOR Lab

Wire it yourself in your browser before you touch a real wire. No sign-up, nothing to install.

The UNO and the parts for this lesson laid out in EEMENTOR Lab, ready to wireBuild it yourself
  1. The UNO and this lesson’s parts are already placed. Wire them using the wiring table above. Pins with no wire show red.
  2. Missing a part? Add it from the parts list on the left.
  3. Press Run, then try the inputs: click, hold or slide them under Selected.
  4. Then try this lesson’s sketch: paste it into the Code tab and press Compile & Run.

EEMENTOR Lab is free and in beta. Results are a guide; a real build can differ slightly. About the Lab

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.

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