Built-in project · Arduino Uno R3

Random colors on an RGB LED (pins 11, 10 and 9), on an Arduino Uno

An RGB LED on pins 11, 10 and 9 changes to a random color twice a second, three random numbers from 0 to 255 at a time. The whole build, an Arduino Uno and 4 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $32. On Mokxi they're free.

Random colors on an RGB LED (pins 11, 10 and 9) · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Mixing colors from red, green and blue
  • Random numbers with random
  • Seeding the generator with randomSeed
  • Setting brightness with analogWrite
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 6

How it works

An RGB LED has red, green and blue inside, with one shared leg to ground. Each color has a 220 ohm resistor and a PWM pin: red on 11, green on 10 and blue on 9.

Every half second the sketch picks three random numbers from 0 to 255, one per color, and writes them to the pins. Mixing three random levels gives a new color each time. The numbers are printed so you can see which mix you got.

Parts list

6 parts, plus the jumper wires. They retail for about $32; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Resistor, 220 Ω (3) pin 1Arduino Uno R3 pin 11
Resistor, 220 Ω (2) pin 1Arduino Uno R3 pin 10
Resistor, 220 Ω (1) pin 1Arduino Uno R3 pin 9
RGB LED pin CArduino Uno R3 pin GND
Resistor, 220 Ω (3) pin 2RGB LED pin R
Resistor, 220 Ω (2) pin 2RGB LED pin G
Resistor, 220 Ω (1) pin 2RGB LED pin B

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Random colors: an RGB LED on pins 11, 10 and 9.
//
//   RGB LED legs -> 220 ohm each -> pins 11 (red), 10 (green), 9 (blue)
//   the common leg -> GND
//
// random(256) gives a whole number from 0 to 255, so three of them make a
// color. randomSeed() starts the sequence from the noise on an unconnected
// analog pin, so it is different after each reset.

const int RED = 11;
const int GREEN = 10;
const int BLUE = 9;

void setup() {
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  randomSeed(analogRead(A5));
  Serial.begin(9600);
  Serial.println("Mokxi Uno: random colors on pins 11, 10 and 9");
}

void loop() {
  int r = random(256);
  int g = random(256);
  int b = random(256);
  analogWrite(RED, r);
  analogWrite(GREEN, g);
  analogWrite(BLUE, b);
  Serial.print(r);
  Serial.print(", ");
  Serial.print(g);
  Serial.print(", ");
  Serial.println(b);
  delay(500);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Sets the three color pins as outputs. randomSeed(analogRead(A5)) starts the random sequence from the noise on an unconnected pin, so the colors differ after each reset. Then it starts serial at 9600.

void setup() {
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  randomSeed(analogRead(A5));
  Serial.begin(9600);
  Serial.println("Mokxi Uno: random colors on pins 11, 10 and 9");
}

loop sketch.ino

Picks r, g and b with random(256), writes them to pins 11, 10 and 9 with analogWrite, prints the three numbers, and waits 500 ms.

void loop() {
  int r = random(256);
  int g = random(256);
  int b = random(256);
  analogWrite(RED, r);
  analogWrite(GREEN, g);
  analogWrite(BLUE, b);
  Serial.print(r);
  Serial.print(", ");
  Serial.print(g);
  Serial.print(", ");
  Serial.println(b);
  delay(500);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The same colors repeat after every reset This project
    The seed comes from analogRead(A5). Leave A5 unconnected so it picks up noise.
  • A color is missing This project
    Check that each color leg goes through its own 220 ohm resistor to the right pin.
  • The colors are wrong or one color never lights. RGB LED
    Check whether your LED is common cathode (shared leg to ground) or common anode (shared leg to 5 V). Common anode works backwards: LOW turns a color on.
  • One color is much brighter than the others, or the LED runs too hot. RGB LED
    Use one resistor per color leg, never one for the shared leg. Typical values are 220 ohm for red and 100 to 150 ohm for green and blue on 5 V.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.