Warm or cool light: a knob blends an RGB LED, on an Arduino Uno
A potentiometer on A0 slides an RGB LED (red 11, green 10, blue 9) from a warm orange to a cool blue, mixing the two colors with map() on the way. The whole build, an Arduino Uno and 5 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 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $33. On Mokxi they're free.
What you'll learn
- Blending two colors with map
- Mixing light with three PWM channels
- Reading a knob with analogRead
- Warm and cool white as color mixes
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Arduino Uno R3
- Parts: 7
How it works
A knob on A0 blends an RGB LED from a warm white to a cool white. The red, green and blue legs each have a 220 ohm resistor, and connect to pins 11, 10 and 9. The common leg goes to GND.
A warm white is mostly red with a little green and almost no blue. A cool white is mostly blue. The sketch stores both colors as arrays, WARM and COOL.
For each channel, map slides the value from its warm number to its cool number as the knob turns, so halfway around gives halfway between. The new values print every 200 ms.
Parts list
7 parts, plus the jumper wires. They retail for about $33; 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.
- 1 × 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 pin 1 | Arduino Uno R3 pin 5V |
| RGB LED pin C | Arduino Uno R3 pin GND |
| Potentiometer pin 3 | Arduino Uno R3 pin GND |
| Potentiometer pin 2 | Arduino Uno R3 pin A0 |
| 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.
// Cool or warm: a knob on A0 blends an RGB LED from warm to cool.
//
// RGB red -> 220 ohm -> pin 11 green -> 220 ohm -> pin 10
// RGB blue -> 220 ohm -> pin 9 the long common leg -> GND
// potentiometer: ends to 5V and GND, middle leg -> A0
//
// A warm white is mostly red with some green and little blue; a cool one is
// mostly blue. map() slides each channel from its warm value to its cool one
// as the knob turns, so halfway round you get halfway between.
const int RED = 11;
const int GREEN = 10;
const int BLUE = 9;
const int WARM[3] = {246, 52, 8};
const int COOL[3] = {100, 150, 255};
void setup() {
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: turn the knob from warm to cool");
}
void loop() {
int knob = analogRead(A0);
int r = map(knob, 0, 1023, WARM[0], COOL[0]);
int g = map(knob, 0, 1023, WARM[1], COOL[1]);
int b = map(knob, 0, 1023, WARM[2], COOL[2]);
analogWrite(RED, r);
analogWrite(GREEN, g);
analogWrite(BLUE, b);
Serial.print("R ");
Serial.print(r);
Serial.print(" G ");
Serial.print(g);
Serial.print(" B ");
Serial.println(b);
delay(200);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
It sets the three LED pins as outputs and starts the serial port at 9600. Then it prints a message asking you to turn the knob from warm to cool.
void setup() {
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: turn the knob from warm to cool");
}loop sketch.ino
It reads the knob, then uses map to turn that 0 to 1023 number into a red, a green and a blue level. Each runs from its WARM value to its COOL value. It writes the three levels with analogWrite, prints them and waits 200 ms.
void loop() {
int knob = analogRead(A0);
int r = map(knob, 0, 1023, WARM[0], COOL[0]);
int g = map(knob, 0, 1023, WARM[1], COOL[1]);
int b = map(knob, 0, 1023, WARM[2], COOL[2]);
analogWrite(RED, r);
analogWrite(GREEN, g);
analogWrite(BLUE, b);
Serial.print("R ");
Serial.print(r);
Serial.print(" G ");
Serial.print(g);
Serial.print(" B ");
Serial.println(b);
delay(200);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The colors look wrong or swapped. This project
Check red goes to pin 11, green to pin 10 and blue to pin 9, each through a 220 ohm resistor. - The LED stays dark. This project
The common leg must go to GND. Also check the knob's ends are on 5V and GND, and the middle goes to A0. - 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.