Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk3 min

Smart Color Control Using Arduino and an RGB LED

Learn how to wire an RGB LED to an Arduino, identify its common lead type, and set red, green, and blue channels with PWM.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To control an RGB LED with an Arduino, connect its red, green, and blue channels to three PWM-capable pins, add a separate current-limiting resistor to each channel, and set the channel values with analogWrite(). First identify whether the LED is common-anode or common-cathode: that determines how its shared lead is wired and whether your code must invert the values.

What you need to know before wiring

An RGB LED contains red, green, and blue elements. Adjusting each channel independently mixes their light into a displayed color. Arduino’s analogWrite() uses pulse-width modulation (PWM) on supported pins; it does not produce a true analog voltage on every pin. PWM pins vary by board. Arduino’s PWM output guide lists pins 3, 5, 6, 9, 10, and 11 for Uno R3 and earlier, Uno R4, Nano, and Mini. Check that guide for other boards and confirm the exact board variant.

As an Amazon Associate I earn from qualifying purchases.

At the default 8-bit resolution, analogWrite() takes values from 0 to 255: 0 is off and 255 is the maximum PWM setting. The Arduino guide notes that resolution can be changed on supported cores, so use this range only when your configuration uses the default 8-bit resolution.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Identify the LED type

Look up the part number or check the component documentation to determine whether the LED is common-anode or common-cathode. The shared lead connects to a different supply point for each type, and the channel values in the sketch behave differently.

#1 Best Overall
DIYables RGB LED Module for Arduino, ESP32, ESP8266, Raspberry Pi, 10 Pieces
  • RGB LED module with built-in resistors
  • Common cathode
  • Generating PWM to R, G, B pins to create any color.
  • RGB LED for Arduino, ESP32, ESP8266, Raspberry Pi, or any 5V or 3.3V microcontroller.
  • Tutorials for Arduino, ESP32, ESP8266, , Raspberry Pi,, Raspberry Pi, Pico, and MicroPython are provided => Search for: DIYables RGB LED module
LED type Shared lead connection Channel code
Common-cathode Connect the shared cathode to GND. Write the channel value directly; higher values request more output.
Common-anode Connect the shared anode to the appropriate positive supply for the component and board. Invert each channel value; at default 8-bit resolution, write 255 - value.

These are the two common RGB LED arrangements shown in the cited tutorials. Follow the LED and board specifications for the correct supply connection; do not infer the pinout from the LED’s appearance alone.

Wire the LED with current limiting

Use one current-limiting resistor in series with each color channel. Check the LED’s forward voltage and current limits along with the Arduino board’s output constraints before selecting resistor values. Arduino Project Hub’s common-anode example lists a 220-ohm resistor, but that is a parts-list example for that project, not a universal value for every LED and board.

Rank #2
ELEGOO 3mm & 5mm Clear & Diffused LED Kit, RGB, UV, Fast Flashing, 350pcs
  • 350 PIECES ACROSS TWO SIZES AND SPECIAL EFFECTS - Stock 30 each of 3mm and 5mm white, red, blue, yellow and green LEDs, plus 10 UV, 10 RGB common-anode, 10 RGB common-cathode, 10 fast-flashing RGB and 10 slow-flashing RGB LEDs in a divided organizer
  • CLEAR AND DIFFUSED OPTIONS FOR DIFFERENT LOOKS - Use narrow, bright indicators for control panels or softer light for models, cosplay, school experiments, 3D-printed props and breadboard projects, with both 3mm and 5mm sizes ready for compact or more visible designs
  • EXPLORE UV, RGB AND SELF-FLASHING CIRCUITS - Compare common-anode and common-cathode RGB wiring, create color-mixing lessons or add automatic fast and slow effects without coding every blink; these are conventional through-hole LEDs, not addressable WS2812 or RGB strip pixels
  • PROTECT EVERY LED WITH THE CORRECT RESISTOR - Check polarity, forward voltage and desired current before power is applied, calculate a series resistor for the supply voltage and never connect a bare LED directly to a battery, power rail or controller output without current limiting
  • BARE COMPONENTS FOR LEARNING, PROTOTYPING AND REPAIR - Test each diode before permanent soldering, keep the compartments labeled and verify RGB pin order when leads have been trimmed; resistors, wires, breadboards, controller boards, soldering tools and power supplies are not included
  1. Connect the LED’s red, green, and blue leads, each through its own resistor, to three PWM-capable Arduino pins.
  2. Connect the shared lead to GND for a common-cathode LED, or to the appropriate positive supply for a common-anode LED.
  3. Check the LED’s pinout and the board’s pin capabilities before powering the circuit.

Set colors in Arduino code

Declare the three channel pins as outputs, then use a function to set all three values together. This example uses PWM pins 9, 10, and 11. Change them if needed to match PWM-capable pins on your board.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;

// Set to true for common-anode; false for common-cathode.
const bool commonAnode = false;

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void setColor(int red, int green, int blue) {
  if (commonAnode) {
    red = 255 - red;
    green = 255 - green;
    blue = 255 - blue;
  }

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

void loop() {
  setColor(255, 0, 0);  // red
  delay(1000);
  setColor(0, 255, 0);  // green
  delay(1000);
  setColor(0, 0, 255);  // blue
  delay(1000);
  setColor(255, 255, 255);  // all channels on
  delay(1000);
}

For a common-anode LED, set commonAnode to true. Adafruit’s Arduino Lesson 3 RGB LEDs sketch gives the same instruction: “If you are using a Common Anode RGB LED, then you need to change the analog write values so that the color is subtracted from 255, Uncomment the line #define COMMON_ANODE in the sketch!”

Rank #3
EDGELEC 100pcs 5mm RGB Tri-Color (Red Green Blue Multicolor) 4Pin LED Diodes Common Cathode Diffused Round Lens 29mm Long Lead +300pcs Resistors (for DC 6-12V) Included,Light Emitting Diodes
  • Product Overview: 100 pcs LED Diodes + 300 pcs Resistors
  • Product Features: Tri-Color (Red/Green/Blue) Lights / Common Cathode / 5mm Diffused Round Lens
  • Product Parameters: 29mm Long Lead / Red: DC 2.0-2.2V; Blue&Green: DC 3.0-3.2V (IF=20mA) / 0.06 Watts / 2pin / DIP LEDs
  • Product Parameters: Luminance Brighter Up To +20% / 120° Viewing Angle / Single LED Diodes Wight: 0.38g
  • Accessories: 200pcs 430ohm + 100pcs 470ohm (For DC 6-12V) 1/4 Watt Metal Film Resistors Included / ±1% Tolerance
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose channel values and refine the mix

The three numbers are relative channel controls, not guaranteed color-calibrated measurements. For instance, setColor(255, 0, 0) requests the red channel at its maximum setting and the other two off at default 8-bit resolution. Mixed values can be adjusted by eye, but the cited tutorials do not establish calibrated color matching across different LEDs.

If you want physical controls, three potentiometers can provide one adjustable input per color channel. Read each potentiometer and map its reading to the range supported by your analogWrite() configuration, then pass those three values to setColor(). Keep the LED channel resistors in place; potentiometers do not replace current limiting.

Quick Recap

Bestseller No. 1
DIYables RGB LED Module for Arduino, ESP32, ESP8266, Raspberry Pi, 10 Pieces
DIYables RGB LED Module for Arduino, ESP32, ESP8266, Raspberry Pi, 10 Pieces
RGB LED module with built-in resistors; Common cathode; Generating PWM to R, G, B pins to create any color.
$9.99
Bestseller No. 4
10pcs 4Pin RGB Module KY-016 Three Colors 3 Color RGB LED Sensor Module for Arduino
10pcs 4Pin RGB Module KY-016 Three Colors 3 Color RGB LED Sensor Module for Arduino
10pcs 4Pin RGB Module KY-016 Three Colors 3 Color RGB LED Sensor Module For Arduino; Three primary colors can be mixed to get different colors by adjusting the PWM
$9.88
Best Value
CHANZON 100 pcs 5mm RGB Multicolor LED Diode Lights Common Cathode
  • Overview: Tricolor Red(R) Green(G) Blue(B) Common Cathode(CC), R:620nm-625nm / G:515nm-520nm / B:450nm-455nm,Luminous Intensity(High Brightness): R:2000-3000mcd / G:15000-18000mcd / B:7000-8000mcd High Brightness 5mmled
  • Viewing Angle: 30 Degrees,Parameters : DC 2V-2.2V(R) 3V-3.2V (G/B) Volt 20 mA, Polarity: Anode "+" (Shorter Leg) | Cathode "-" (Longer Leg), Clear Transparent Round Small Lens
  • Shipping Weight: 1.81oz / 0.05kg, Package: Pack of 100 Pieces Leddiode(Through Hole DIP 4pins leads mini LEDs Set) Three Colour
  • Compatible with: DIY PCB Board Circuit, Arduino, Raspberry Pi, Hobby, Science Experiments, Throwies Project, Breadboard, T1 T-1 Bulb, Bulk Parts Replacement
  • Micro F5 5 mm Diameter, 4pin 4 pin, Tiny Bright Light,Multi Color with Low Voltage & Low Power Consumption
Rank #4
10pcs 4Pin RGB Module KY-016 Three Colors 3 Color RGB LED Sensor Module for Arduino
  • 10pcs 4Pin RGB Module KY-016 Three Colors 3 Color RGB LED Sensor Module For Arduino
  • RGB LED module LED is made of a plug-in full-color, by R, G, B three-pin PWM input voltage can be adjusted in three primary colors (red / blue / green) strength in order to achieve full color mixing effect.
  • Three primary colors can be mixed to get different colors by adjusting the PWM
  • Can interface with a variety of microcontrollers
  • Operating voltage: 5V LED drive mode: common cathode drive

Common problems to check

  • No light: Verify the shared lead connection, LED pinout, resistor path, and that the three selected pins support PWM on your board.
  • Colors appear reversed or stay on: Check the LED type and set the common-anode option correctly. Common-anode channels need inverted output values.
  • Only some colors respond: Check each channel’s wiring and resistor separately, and confirm the pin numbers in the sketch match the circuit.
  • Unexpected brightness or color: Confirm the values and resolution used by the board, and remember that the numeric channel settings are not a universal calibrated color standard.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.