BeginnerCreative Technology

Mood Lamp

Mix colours with a knob using an RGB LED and PWM.

Time
1 h
Estimated cost
~£19
Hardware
ESP32-DevKitC V4 (38-pin)

Example code — not yet validated

Try it in the browser

What you will build

A small desk lamp whose colour you choose by turning a knob. A button switches between three modes: Colour (knob picks the hue), Brightness (knob dims the current colour) and Rainbow (colours drift on their own). A frosted jar or ping-pong ball softens the light.

How it works

An RGB LED is three LEDs in one package. The ESP32 drives each colour with PWM: it switches the pin on and off thousands of times per second, and the share of "on" time sets the apparent brightness (0–255). The potentiometer is a voltage divider; the ESP32's analog-to-digital converter turns its position into a number from 0 to 4095. The sketch maps that number to a colour on the colour wheel.

Parts

Part Qty Purpose Required
ESP32-DevKitC V4 + breadboard + USB cable 1 Controller Yes
RGB LED, 5 mm, common cathode 1 Light source Yes
220 Ω resistor 3 One per colour Yes
10 kΩ rotary potentiometer 1 Colour / brightness knob Yes
Tactile push button 1 Mode switch Yes
Frosted jar, lampshade or ping-pong ball 1 Diffuser Optional

Build steps

  1. Identify the RGB LED legs: the longest leg is the common cathode → connect it to GND.
  2. Connect GPIO 25 → 220 Ω → red leg, GPIO 26 → 220 Ω → green leg, GPIO 33 → 220 Ω → blue leg (check your LED's datasheet for leg order).
  3. Potentiometer: outer legs to 3V3 and GND, middle leg (wiper) to GPIO 34.
  4. Button between GPIO 27 and GND.
  5. Upload the code and open the Serial Monitor at 115200 baud.
  6. Checkpoint: turning the knob sweeps red → yellow → green → cyan → blue → magenta → red.
  7. Press the button to cycle modes; the Serial Monitor prints the mode name.
  8. Place the diffuser over the LED.

Try it in the browser

Playground Simulation. Drag the virtual potentiometer and click the virtual button. The virtual RGB LED shows the mixed colour and a small panel shows the three PWM values (0–255).

Open Simulator Playground

Code

Download mood-lamp.ino

// Mood Lamp — XBOTNOW
// Board: ESP32-DevKitC V4 · Core: esp32 v3.x (ledcAttach API)
const int PIN_R = 25, PIN_G = 26, PIN_B = 33;
const int POT_PIN = 34;      // ADC1
const int BUTTON_PIN = 27;

enum Mode { COLOUR, BRIGHTNESS, RAINBOW };
Mode mode = COLOUR;
float hue = 0;               // 0..360
float level = 1.0;           // 0..1
int lastBtn = HIGH;
unsigned long lastPress = 0;

void writeRGB(float h, float v) {
  // HSV (s = 1) to RGB
  float c = v, x = c * (1 - fabs(fmod(h / 60.0, 2) - 1));
  float r = 0, g = 0, b = 0;
  if (h < 60)       { r = c; g = x; }
  else if (h < 120) { r = x; g = c; }
  else if (h < 180) { g = c; b = x; }
  else if (h < 240) { g = x; b = c; }
  else if (h < 300) { r = x; b = c; }
  else              { r = c; b = x; }
  ledcWrite(PIN_R, (int)(r * 255));
  ledcWrite(PIN_G, (int)(g * 255));
  ledcWrite(PIN_B, (int)(b * 255));
}

void setup() {
  Serial.begin(115200);
  ledcAttach(PIN_R, 5000, 8);   // 5 kHz, 8-bit
  ledcAttach(PIN_G, 5000, 8);
  ledcAttach(PIN_B, 5000, 8);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
}

void loop() {
  int btn = digitalRead(BUTTON_PIN);
  if (btn == LOW && lastBtn == HIGH && millis() - lastPress > 200) {
    mode = (Mode)((mode + 1) % 3);
    lastPress = millis();
    const char* names[] = {"Colour", "Brightness", "Rainbow"};
    Serial.printf("Mode: %s\n", names[mode]);
  }
  lastBtn = btn;

  float knob = analogRead(POT_PIN) / 4095.0;   // 0..1
  if (mode == COLOUR)          hue = knob * 359;
  else if (mode == BRIGHTNESS) level = max(knob, 0.02f);
  else                         hue = fmod(hue + 0.3, 360);

  writeRGB(hue, level);
  delay(10);
}

Troubleshooting

Symptom Likely cause Check
Colours inverted (knob at red shows cyan) Common-anode LED Connect common leg to 3V3 and use 255 - value in ledcWrite
Knob only works over half its turn Wiper on wrong leg Middle leg must go to GPIO 34
Compile error on ledcAttach Old board package Update esp32 by Espressif to v3.x
One colour missing Leg order differs Swap the pin numbers in code

Safety and limitations

A 5 mm RGB LED is a desk accent, not a room light. Do not drive LED strips directly from GPIO: that needs a MOSFET driver and a separate supply (see Automatic Night Light).

Checklist

Next project

Automatic Night Light: let sensors decide when the light comes on.

Next project: Automatic Night Light