Illustration: at night on a shelf, a frosted dome glows pink and cyan from an RGB LED.Lights and displays
Illustration (made with AI): roughly what the finished project looks like.

A Colour-Changing Lamp with an RGB LED

An RGB LED holds three small LEDs: red, green and blue. Mix their brightness and you get any colour, while the lamp slowly changes from one colour to the next.

Difficulty
Beginner
Time
30 min
Age
8+
Parts
5, 5 on the site

Written by the RoboHub team. A question? Write to us.

Variations

Choose a variation and the parts list updates.

Instructions

  1. Find the common leg

    An RGB LED has four legs. The longest is the common one: on a common-cathode LED it goes to ground. The other three are red, green and blue.

    On a common-anode LED the common leg goes to 5V, not to ground (see step 4). D9 is the board's digital pin 9, GND is ground. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • RGB LED − → Arduino GND
  2. Wire it up

    Connect each colour leg through its own 330 Ω resistor: red to pin 9, green to pin 10, blue to pin 11. These pins are marked with a tilde (~): they can set the brightness.

    On a common-anode LED the common leg goes to 5V, not to ground (see step 4). D9 is the board's digital pin 9, GND is ground. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • RGB LED R → 330 ΩArduino D9
    • RGB LED G → 330 ΩArduino D10
    • RGB LED B → 330 ΩArduino D11
  3. Upload the code

    analogWrite() sets the brightness from 0 to 255. The code moves slowly from red to green, from green to blue and back to red.

    Arduino C++
    const int R = 9, G = 10, B = 11;       // PWM pins (marked ~)
    
    void setup() {
      pinMode(R, OUTPUT);
      pinMode(G, OUTPUT);
      pinMode(B, OUTPUT);
    }
    
    void color(int r, int g, int b) {      // 0 = off, 255 = full brightness
      analogWrite(R, r);
      analogWrite(G, g);
      analogWrite(B, b);
    }
    
    void loop() {
      // red -> green -> blue -> red, one small step at a time
      for (int i = 0; i < 255; i++) { color(255 - i, i, 0); delay(10); }
      for (int i = 0; i < 255; i++) { color(0, 255 - i, i); delay(10); }
      for (int i = 0; i < 255; i++) { color(i, 0, 255 - i); delay(10); }
    }
  4. If the colours are inverted

    If your LED is common-anode, connect the common leg to 5V and, in the color() function, subtract every value from 255.

    Illustration: an RGB LED glowing violet under a frosted dome.

    Illustration, made with AI

  • Lights and displays
    Beginner45 min10+

    Pedestrian Crossing with a Button

    Cars have green until a pedestrian presses the button. Then the cars get red, the pedestrians get green and a buzzer ticks, just like at a real crossing.

    6/6 parts on the site
  • Sensors
    Beginner25 min8+

    A Light That Turns On by Itself in the Dark

    A photoresistor measures the light. When the room gets dark the LED turns on by itself and goes off again in daylight, just like street lighting.

    6/6 parts on the site
  • Lights and displays
    Beginner20 min8+

    Running Lights with Six LEDs

    Six LEDs light up one after another, so a dot of light runs left and right. A neat way to learn the for loop.

    4/4 parts on the site