Three knobs mix an RGB LED (pins 11, 10 and 9), on an Arduino Uno
Three potentiometers on A0, A1 and A2 set the red, green and blue of an RGB LED on pins 11, 10 and 9: mix any color by hand. The whole build, an Arduino Uno and 7 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 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $34. On Mokxi they're free.
What you'll learn
- Reading three analog inputs
mapconverts one range to another- PWM brightness for each color
- Mixing light to make any color
- Difficulty: Beginner
- Time: About 20 minutes
- Board: Arduino Uno R3
- Parts: 9
How it works
Three potentiometers on A0, A1 and A2 mix the color of one RGB LED on pins 11, 10 and 9. The first knob sets red, the second green and the third blue.
Each knob reads from 0 to 1023 with analogRead. The map function turns it into 0 to 255, which is what analogWrite needs to set the brightness of that color.
The loop repeats every 20 ms, so the color follows your hand smoothly.
Parts list
9 parts, plus the jumper wires. They retail for about $34; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 1 × RGB LEDIn Starter Projects Kit.
- 3 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 3 × PotentiometerIn Halloween Prop Kit, Starter Projects Kit.
Kits that can build it
These 10 starter kits have every part in the circuit. Each kit's page lists what else it can build.
- ELEGOO UNO R3 Super Starter Kit
- ELEGOO UNO R3 Most Complete Starter Kit
- Arduino Starter Kit
- Freenove Ultimate Starter Kit
- SunFounder Starter Kit for Arduino Uno R3
- SunFounder Inventor Lab Kit
- REXQualis UNO R3 Super Starter Kit
- keyestudio Super Starter Kit
- Vilros Arduino Uno 3 Ultimate Starter Kit
- Adafruit MetroX Classic Kit
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 pin | Connects to |
|---|---|
| Resistor, 220 Ω (3) pin 1 | Arduino Uno R3 pin 11 |
| Resistor, 220 Ω (2) pin 1 | Arduino Uno R3 pin 10 |
| Resistor, 220 Ω (1) pin 1 | Arduino Uno R3 pin 9 |
| Potentiometer (1) pin 1 | Arduino Uno R3 pin 5V |
| Potentiometer (2) pin 1 | Arduino Uno R3 pin 5V |
| Potentiometer (3) pin 1 | Arduino Uno R3 pin 5V |
| RGB LED pin C | Arduino Uno R3 pin GND |
| Potentiometer (1) pin 3 | Arduino Uno R3 pin GND |
| Potentiometer (2) pin 3 | Arduino Uno R3 pin GND |
| Potentiometer (3) pin 3 | Arduino Uno R3 pin GND |
| Potentiometer (1) pin 2 | Arduino Uno R3 pin A0 |
| Potentiometer (2) pin 2 | Arduino Uno R3 pin A1 |
| Potentiometer (3) pin 2 | Arduino Uno R3 pin A2 |
| Resistor, 220 Ω (3) pin 2 | RGB LED pin R |
| Resistor, 220 Ω (2) pin 2 | RGB LED pin G |
| Resistor, 220 Ω (1) pin 2 | RGB 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.
// Mix a color by hand: three knobs, one for each color of an RGB LED.
//
// knob 1 -> A0 sets red on pin 11 knob 2 -> A1 sets green on pin 10
// knob 3 -> A2 sets blue on pin 9 each knob's ends -> 5V and GND
// RGB LED legs -> 220 ohm each -> pins 11, 10 and 9; the common leg -> GND
//
// Each reading, 0 to 1023, is mapped to a duty cycle, 0 to 255.
const int RED = 11;
const int GREEN = 10;
const int BLUE = 9;
void setup() {
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: turn the three knobs to mix a color");
}
void loop() {
analogWrite(RED, map(analogRead(A0), 0, 1023, 0, 255));
analogWrite(GREEN, map(analogRead(A1), 0, 1023, 0, 255));
analogWrite(BLUE, map(analogRead(A2), 0, 1023, 0, 255));
delay(20);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Makes pins 11, 10 and 9 outputs and starts serial with a short instruction.
void setup() {
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: turn the three knobs to mix a color");
}loop sketch.ino
Reads each knob, maps the result to 0 to 255, and writes it to the matching color pin, all in a single line per color. It then waits 20 ms.
void loop() {
analogWrite(RED, map(analogRead(A0), 0, 1023, 0, 255));
analogWrite(GREEN, map(analogRead(A1), 0, 1023, 0, 255));
analogWrite(BLUE, map(analogRead(A2), 0, 1023, 0, 255));
delay(20);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- A knob controls the wrong color This project
Match the knobs: A0 to pin 11 red, A1 to pin 10 green, A2 to pin 9 blue. - The LED does nothing This project
Check the LED common leg is on ground and each knob has 5V and GND on its ends. - 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. - The reading jumps around or only covers part of the range. Potentiometer
Wire the two outer legs to 5 V and ground and the middle leg (the wiper) to an analog pin. Using the wrong two legs gives a dead or partial range. - Turning the knob does nothing, or the value is stuck at 0 or 1023. Potentiometer
Check that the wiper goes to an analog input (A0 to A5 on an Uno), not a digital pin. Make sure both ends are connected, since a floating end gives noise.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.