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

| 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
- Place both modules and the three resistors on the breadboard as in the diagram.
- Connect 5V and GND to the breadboard rails.
- Upload the code and open the Serial Monitor at 9600 baud.
- Note the light value in a bright room and when you cover the sensor. Set
BRIGHTandDARKto those numbers. - 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
BRIGHTandDARKnumbers 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 - valueinsetColor().
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