Go back

RGB LED Color Cycling with Arduino on Velxio

Introduction

Have you ever wanted to create colorful light effects with an RGB LED but didn’t have the components handy? With Velxio, you can simulate the entire circuit and code right in your browser. In this tutorial, we’ll build a simple RGB LED color cycler that displays red, green, blue, yellow, cyan, magenta, and white in sequence. You’ll learn how to use PWM (Pulse Width Modulation) to mix colors and how to wire a common-cathode RGB LED to an Arduino Uno.

Circuit Walkthrough

Let’s start by placing the components on the Velxio canvas. You’ll need an Arduino Uno and an RGB LED. The RGB LED has four pins: one common cathode (COM) and three anodes for red (R), green (G), and blue (B).

Circuit diagram of rgb-led on the Velxio simulator canvas

Here’s the wiring:

In Velxio, you can drag wires from one pin to another. The colors of the wires in the simulation match the pin functions: red for red, green for green, blue for blue, and black for ground. This makes it easy to follow the connections.

Code Walkthrough

Now let’s write the Arduino sketch. Open the code editor in Velxio and enter the following:

Source code for rgb-led in the Velxio Monaco editor

// RGB LED Color Cycling
// Display different colors

const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;

void setup() {
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
}

void setColor(int red, int green, int blue) {
  analogWrite(RED_PIN, red);
  analogWrite(GREEN_PIN, green);
  analogWrite(BLUE_PIN, blue);
}

void loop() {
  // Red
  setColor(255, 0, 0);
  delay(1000);

  // Green
  setColor(0, 255, 0);
  delay(1000);

  // Blue
  setColor(0, 0, 255);
  delay(1000);

  // Yellow
  setColor(255, 255, 0);
  delay(1000);

  // Cyan
  setColor(0, 255, 255);
  delay(1000);

  // Magenta
  setColor(255, 0, 255);
  delay(1000);

  // White
  setColor(255, 255, 255);
  delay(1000);
}

How It Works

Key Concepts

PWM (Pulse Width Modulation)

PWM is a technique where a digital pin rapidly switches between HIGH and LOW to simulate an analog voltage. The duty cycle (percentage of time HIGH) determines the perceived brightness. Arduino’s analogWrite() outputs a PWM signal with a frequency of about 490 Hz on pins 9, 10, and 11.

RGB Color Mixing

An RGB LED contains three separate LEDs (red, green, blue) in one package. By varying the brightness of each, you can create millions of colors. For example:

Common Cathode vs Common Anode

This tutorial uses a common-cathode RGB LED, where the COM pin is connected to ground. To turn on a color, you apply a HIGH signal to its anode. If you have a common-anode LED, you would connect COM to VCC and use inverted logic (LOW to turn on).

Common Pitfalls and Debugging Tips

Velxio editor toolbar with compile and run controls

Velxio canvas toolbar above the simulation viewport

Suggested Extensions

Once you have the basic color cycler working, try these enhancements:

  1. Fade Between Colors: Instead of jumping, use a loop to gradually change PWM values for smooth transitions.
  2. Button Control: Add a push button to cycle colors manually.
  3. Potentiometer Input: Use a potentiometer to select a color from a palette.
  4. Serial Control: Send color values over the serial monitor to set custom colors.
  5. Multiple LEDs: Chain several RGB LEDs for a light show.

To add new components, use the component picker:

Velxio component picker modal browsing parts by category

If you need additional libraries, the library manager makes it easy:

Velxio Library Manager modal listing installed Arduino libraries

Call to Action

Ready to see it in action? Open the live example on Velxio and start experimenting: RGB LED Color Cycling on Velxio. No downloads, no hardware – just pure learning. Happy tinkering!


Share this post on:
David Montero

Written by

David Montero

Creator of Velxio, the open-source circuit and Arduino simulator.

GitHub velxio.dev

Related posts


Previous Post
Build a Simon Says Memory Game with Arduino on Velxio
Next Post
Serial Hello World: Your First Arduino Communication Project