Built-in project · Arduino Uno R3

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 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. Free.

Color-mixing lamp · 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.

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
// 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);
}

Parts list

12 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

11 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Ground: Arduino Uno R3 pin GND; Resistor, 10k Ω (1) pin 1; Resistor, 10k Ω (2) pin 1; Resistor, 10k Ω (3) pin 1; RGB LED pin C
  • Arduino Uno R3 pin 11; Resistor, 220 Ω (1) pin 1
  • Arduino Uno R3 pin 10; Resistor, 220 Ω (3) pin 1
  • Arduino Uno R3 pin 9; Resistor, 220 Ω (2) pin 1
  • Arduino Uno R3 pin 5V; Phototransistor (1) pin C; Phototransistor (2) pin C; Phototransistor (3) pin C
  • Arduino Uno R3 pin A0; Phototransistor (1) pin E; Resistor, 10k Ω (1) pin 2
  • Arduino Uno R3 pin A1; Phototransistor (2) pin E; Resistor, 10k Ω (2) pin 2
  • Arduino Uno R3 pin A2; Phototransistor (3) pin E; Resistor, 10k Ω (3) pin 2
  • RGB LED pin R; Resistor, 220 Ω (3) pin 2
  • RGB LED pin G; Resistor, 220 Ω (2) pin 2
  • RGB LED pin B; Resistor, 220 Ω (1) pin 2

Change it and keep it

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