Arduino RGB LED Color Mixer with 3 Potentiometers (Code)

Arduino ile RGB LED Renk Karıştırıcı Yapımı projesi kapak görseli

🇹🇷 Bu yazıyı Türkçe okuyun

What does this project do?

We mix our own colours with a single RGB LED and three potentiometers. Each potentiometer controls one colour: one for red, one for green and one for blue. As you turn the knobs, the LED changes colour. Mix red and green to get yellow, or red and blue to get purple. With all three turned up to the maximum, the LED shines almost white. It’s a great experiment for understanding how screens make colours.

  • Age group: 10 and up
  • Level: Beginner
  • Time: About 40 minutes
  • What you will learn: reading an analog value from a potentiometer, converting values with map(), setting LED brightness with analogWrite() (PWM), the RGB colour model

New to Arduino? Have a look at the What is Arduino and how does it work? guide first. All projects, sorted from easy to hard, are on the Getting Started with Arduino page.

Parts list

PartQty
Arduino Uno (or compatible)1
RGB LED (common cathode, 4 legs)1
10 kΩ potentiometer3
220 Ω resistor3
Breadboard1
Male-to-male jumper wires~16
USB cable1

Wiring

Arduino RGB LED color mixer with potentiometers wiring diagram
ComponentComponent pinArduino pin
RGB LEDRed leg (through 220 Ω)D9
RGB LEDGreen leg (through 220 Ω)D10
RGB LEDBlue leg (through 220 Ω)D11
RGB LEDLongest leg (common cathode)GND
Potentiometer 1 (red)Middle legA0
Potentiometer 2 (green)Middle legA1
Potentiometer 3 (blue)Middle legA2
PotentiometersOne outer leg5V
PotentiometersOther outer legGND

The longest leg of the RGB LED is the common leg. On a common cathode LED, this leg goes to GND. When you hold the LED with its flat edge on the left, the leg order is usually: red, common (longest), green, blue.

Code

// Arduino RGB LED Color Mixer - ozandamci.com

const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;

const int redPot = A0;
const int greenPot = A1;
const int bluePot = A2;

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // The potentiometers give 0–1023, the LED needs 0–255
  int red = map(analogRead(redPot), 0, 1023, 0, 255);
  int green = map(analogRead(greenPot), 0, 1023, 0, 255);
  int blue = map(analogRead(bluePot), 0, 1023, 0, 255);

  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);

  Serial.print("R: ");
  Serial.print(red);
  Serial.print("  G: ");
  Serial.print(green);
  Serial.print("  B: ");
  Serial.println(blue);

  delay(50);
}

No Arduino? Try this circuit in your browser with the Wokwi simulation → (free, nothing to install).

Step-by-step instructions

  1. Prepare the power rails. Connect the Arduino’s 5V pin to the + rail of the breadboard and the GND pin to the – rail.
  2. Place the RGB LED. Put the LED on the breadboard. Connect the longest leg to the – rail.
  3. Add the resistors. Connect a 220 Ω resistor to each of the red, green and blue legs and wire them to D9, D10 and D11.
  4. Place the potentiometers. Put the three potentiometers side by side. Connect one outer leg of each to the + rail and the other outer leg to the – rail.
  5. Connect the middle legs. Connect the middle leg of the first potentiometer to A0, the second to A1 and the third to A2.
  6. Upload the code. In the Arduino IDE, choose Arduino Uno and the right port from Tools → Board, then click Upload.
  7. Test it. Turn the knobs one at a time, then turn two of them up together to find new colours. You can see the value of each colour in Tools → Serial Monitor.

Troubleshooting

  • The LED doesn’t light up at all: Your LED may be a common anode type. In that case, connect the long leg to 5V instead of GND and invert the value in the three analogWrite lines, like 255 - red.
  • The colour changes the wrong way when you turn the knob: Swap the outer legs (5V and GND) of the potentiometer.
  • One colour never lights up: Check the resistor and wire for that colour. D9, D10 and D11 are PWM pins; if you use a different pin, the brightness won’t change.

Try it yourself

  • Type the R, G, B values from the Serial Monitor into an online colour picker and find the same colour on the screen.
  • Make a colour fade with code instead of potentiometers: let the LED show the colours of the rainbow one after another.
  • Put a ping-pong ball or a frosted cap over the LED; the colours mix much better.

For teachers: a two-page activity sheet for using this project in class: lesson plan, discussion questions, answer key and a student worksheet.

Video

The narration is in Turkish. English subtitles are available: click CC on the video.

“Arduino RGB LED Color Mixer with 3 Potentiometers (Code)” için 2 yorum

Bir Cevap Yazın