Color-mixing lamp on an Arduino Uno
Three phototransistors on A0, A1 and A2, one for each color, set the red, green and blue of an RGB LED with analogWrite. The whole build, an Arduino Uno and 10 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.
Build it as the Starter Projects Kit: the parts in numbered bags and a booklet that checks every step. Retails for about $48, free on Mokxi.
What you'll learn
- Phototransistors as light sensors
- Turning a current into a voltage with a resistor
- Mapping a 10-bit reading to an 8-bit PWM value
- Mixing red, green and blue
- Difficulty: Beginner
- Time: About 20 minutes
- Board: Arduino Uno R3
- Parts: 12
How it works
Three phototransistors each pass more current as more light hits them. A 10k resistor from each emitter to ground turns that current into a voltage that the Uno reads on A0, A1 and A2. In the original build, a colored gel sits in front of each sensor, so each one measures one color of the light around it.
The sketch reads the three values and copies them onto an RGB LED: red on pin 10, green on pin 9 and blue on pin 11, each through a 220 ohm resistor.
analogRead gives 0 to 1023 but analogWrite takes 0 to 255, so each reading is divided by 4. The raw values are also printed every quarter second.
Parts list
12 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
- 3 × PhototransistorIn Starter Projects Kit.
- 3 × Resistor, 10k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × RGB LEDIn Starter Projects Kit, Sound and Lights Kit.
- 3 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
Kits that can build it
This starter kit has every part in the circuit. Each kit's page lists what else it can build.
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, 10k Ω (1) pin 1 | Arduino Uno R3 pin GND |
| Resistor, 10k Ω (2) pin 1 | Arduino Uno R3 pin GND |
| Resistor, 10k Ω (3) pin 1 | Arduino Uno R3 pin GND |
| RGB LED pin C | Arduino Uno R3 pin GND |
| Resistor, 220 Ω (1) pin 1 | Arduino Uno R3 pin 11 |
| Resistor, 220 Ω (3) pin 1 | Arduino Uno R3 pin 10 |
| Resistor, 220 Ω (2) pin 1 | Arduino Uno R3 pin 9 |
| Phototransistor (1) pin C | Arduino Uno R3 pin 5V |
| Phototransistor (2) pin C | Arduino Uno R3 pin 5V |
| Phototransistor (3) pin C | Arduino Uno R3 pin 5V |
| Phototransistor (1) pin E | Arduino Uno R3 pin A0 |
| Resistor, 10k Ω (1) pin 2 | Arduino Uno R3 pin A0 |
| Phototransistor (2) pin E | Arduino Uno R3 pin A1 |
| Resistor, 10k Ω (2) pin 2 | Arduino Uno R3 pin A1 |
| Phototransistor (3) pin E | Arduino Uno R3 pin A2 |
| Resistor, 10k Ω (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.
// Color-mixing lamp: three light sensors, each behind a colored gel, set the
// red, green and blue of one RGB LED.
//
// phototransistors collector -> 5V, emitter -> A0 / A1 / A2, and a 10k
// resistor from each emitter to GND
// A0 behind the red gel, A1 green, A2 blue
// RGB LED red -> pin 10, green -> pin 9, blue -> pin 11, each
// through 220 ohm; the long leg (common cathode) to GND
//
// Each phototransistor passes more current the more light falls on it, and
// its 10k resistor turns that current into a voltage on the analog pin. The
// gel in front lets through mostly one color, so each sensor measures one
// part of the light around it, and the lamp copies that mix.
//
// analogRead gives 0 to 1023 and analogWrite takes 0 to 255, so each reading
// is divided by 4 on the way across.
const int RED_LED = 10;
const int GREEN_LED = 9;
const int BLUE_LED = 11;
void setup() {
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(BLUE_LED, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: color-mixing lamp");
}
void loop() {
int red = analogRead(A0);
delay(5);
int green = analogRead(A1);
delay(5);
int blue = analogRead(A2);
analogWrite(RED_LED, red / 4);
analogWrite(GREEN_LED, green / 4);
analogWrite(BLUE_LED, blue / 4);
Serial.print("raw red ");
Serial.print(red);
Serial.print(", green ");
Serial.print(green);
Serial.print(", blue ");
Serial.println(blue);
delay(250);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Sets the three LED pins as outputs and starts the serial port at 9600 baud. The sensor pins need no setup, because analog inputs work by default.
void setup() {
pinMode(RED_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(BLUE_LED, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: color-mixing lamp");
}loop sketch.ino
Reads A0, A1 and A2 with a short 5 ms pause between each, so the analog input settles. It divides each by 4 and passes it to analogWrite on the matching LED pin. Then it prints the raw values and waits 250 ms.
void loop() {
int red = analogRead(A0);
delay(5);
int green = analogRead(A1);
delay(5);
int blue = analogRead(A2);
analogWrite(RED_LED, red / 4);
analogWrite(GREEN_LED, green / 4);
analogWrite(BLUE_LED, blue / 4);
Serial.print("raw red ");
Serial.print(red);
Serial.print(", green ");
Serial.print(green);
Serial.print(", blue ");
Serial.println(blue);
delay(250);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LED shows the wrong colors This project
Red is pin 10, green is pin 9 and blue is pin 11. Check that each leg of the RGB LED goes to the right pin. - A reading is stuck at 0 This project
Each phototransistor needs its collector on 5V and its emitter on the analog pin, with the 10k resistor to ground. - The reading does not change with light. Phototransistor
A phototransistor has polarity. Put a resistor (about 10 k ohms) in series, and read the voltage at the joint. If it still does nothing, flip the part around. - The reading is stuck at the top or bottom. Phototransistor
A bigger resistor makes the sensor more sensitive but saturates quickly in bright light. A smaller one needs more light. Try 1 k to 100 k ohms. - 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 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.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.