Projects / RGB mood lamp with three knobs

RGB mood lamp with three knobs

Beginner1.5 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. UNO14.0 mA

    Identify the LED legs

    The longest leg is the shared cathode.

  2. UNO14.0 mA

    Wire the LED

    Each colour leg through 220 ohm to D9, D10, D11.

  3. UNO

    Wire the three pots

    Outer legs to 5V and GND, middles to A0, A1, A2.

  4. UNO14.0 mA

    Upload the sketch

    LED lights with the mix of the three knobs.

  5. UNO

    Turn one knob at a time

    See red, green and blue separately.

  6. UNO

    Mix pairs

    Red and green gives yellow.

  7. UNO

    Press the button

    The colours cycle on their own.

  8. UNO

    Turn the third knob in auto mode

    The speed changes.

In short

How do you build an RGB mood lamp with three knobs?

Three knobs mix red, green and blue in a frosted jar lamp. You need Arduino Uno R3, RGB LED 5 mm common cathode, Potentiometer 10 kΩ × 3, Resistor 220 Ω × 3, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 90 minutes.

Level
Beginner
Time
About 90 min
Parts
7

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Three potentiometers, outer legsUno 5V and GNDEach makes a 0 to 5 V divider
Pot middle legsUno A0, A1, A2Red, green and blue levels
RGB LED red leg through 220 ohmUno D9Red PWM
RGB LED green leg through 220 ohmUno D10Green PWM
RGB LED blue leg through 220 ohmUno D11Blue PWM
RGB LED common cathode (longest leg)Uno GNDCommon-cathode type
Mode buttonUno D2 to GNDSwitches knobs and auto cycle

Code

The Uno cannot output a steady in-between voltage, so analogWrite switches each LED on and off about 490 or 980 times a second and changes how long it stays on in each cycle (the duty). Your eye averages that into a brightness. analogRead gives 0 to 1023, so dividing by four fits the 0 to 255 range that analogWrite expects. The three channels mix like paint made of light: red and green make yellow, red and blue make magenta, green and blue make cyan, and all three at full make white. The button switches to an automatic mode that walks round the colour wheel using a simple hue-to-RGB function, with the third knob setting the speed. Pins 9, 10 and 11 are PWM pins on the Uno. A common-cathode LED has a shared ground leg, the longest one; for a common-anode LED, wire the long leg to 5V and invert the values. Put the LED in a frosted jar or a ping pong ball to blend the colours.

Runs on a virtual Arduino Uno in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

const uint8_t POT[3] = {A0, A1, A2};
const uint8_t LEDP[3] = {9, 10, 11};      // R, G, B PWM pins
const uint8_t BTN = 2;

bool autoMode = false;
bool lastBtn = HIGH;
float hue = 0;                            // 0 to 360 degrees

void setRGB(uint8_t r, uint8_t g, uint8_t b) {
  analogWrite(LEDP[0], r);
  analogWrite(LEDP[1], g);
  analogWrite(LEDP[2], b);
}

// Convert a hue (0 to 360) at full colour to red, green and blue values.
void hueToRGB(float h, uint8_t &r, uint8_t &g, uint8_t &b) {
  float x = 1 - fabs(fmod(h / 60.0, 2) - 1);
  float rr = 0, gg = 0, bb = 0;
  if (h < 60)       { rr = 1; gg = x; }
  else if (h < 120) { rr = x; gg = 1; }
  else if (h < 180) { gg = 1; bb = x; }
  else if (h < 240) { gg = x; bb = 1; }
  else if (h < 300) { rr = x; bb = 1; }
  else              { rr = 1; bb = x; }
  r = rr * 255; g = gg * 255; b = bb * 255;
}

void setup() {
  for (uint8_t i = 0; i < 3; i++) pinMode(LEDP[i], OUTPUT);
  pinMode(BTN, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  bool b = digitalRead(BTN);
  if (lastBtn == HIGH && b == LOW) { autoMode = !autoMode; delay(30); }
  lastBtn = b;

  if (autoMode) {
    // The third knob sets the cycle speed and brightness stays at full.
    float speed = 0.05 + analogRead(POT[2]) / 1023.0 * 0.8;
    hue += speed;
    if (hue >= 360) hue -= 360;
    uint8_t r, g, bl;
    hueToRGB(hue, r, g, bl);
    setRGB(r, g, bl);
  } else {
    setRGB(analogRead(POT[0]) / 4, analogRead(POT[1]) / 4, analogRead(POT[2]) / 4);
  }
  delay(15);
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
All knobs at zeroLED is off
Red knob onlyRed LED
Red and green knobs highYellow
All three highWhite-ish
Press the buttonSlow colour cycle starts

If it does not work

One colour never lights

Check that leg and its resistor.

Colours are inverted

You have a common-anode LED, wire the long leg to 5V and use 255 minus each value.

Light looks harsh

Diffuse it in a jar or lower the maximum value.

Colours flicker

Loose pot wiring, press the jumper wires in firmly.

Button toggles twice

Hold the delay(30) debounce, or check for a loose wire.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the RGB mood lamp with three knobs?

Arduino Uno R3, RGB LED 5 mm common cathode, Potentiometer 10 kΩ × 3, Resistor 220 Ω × 3, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the RGB mood lamp with three knobs take to build?

About 90 minutes for a beginner maker, in 8 steps.

How do I know the RGB mood lamp with three knobs works?

All knobs at zero: LED is off. Red knob only: Red LED. Red and green knobs high: Yellow.

One colour never lights: what should I check?

Check that leg and its resistor.

Colours are inverted: what should I check?

You have a common-anode LED, wire the long leg to 5V and use 255 minus each value.

Light looks harsh: what should I check?

Diffuse it in a jar or lower the maximum value.

Colours flicker: what should I check?

Loose pot wiring, press the jumper wires in firmly.