Lights and displaysPedestrian 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.
Lights and displaysAn 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.
Written by the RoboHub team. A question? Write to us.
Choose a variation and the parts list updates.
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.
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.
analogWrite() sets the brightness from 0 to 255. The code moves slowly from red to green, from green to blue and back to red.
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); }
}If your LED is common-anode, connect the common leg to 5V and, in the color() function, subtract every value from 255.

Illustration, made with AI
Illustration: at night on a shelf, a frosted dome glows pink and cyan from an RGB LED.
Step 4: If the colours are inverted
Lights and displaysCars 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.
SensorsA 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.
Lights and displaysSix LEDs light up one after another, so a dot of light runs left and right. A neat way to learn the for loop.