RGB LED Color Mixing
![lesson-thumbnail.jpg](/proxy/?url=https://content.instructables.com/F46/V7VQ/JD2FC65I/F46V7VQJD2FC65I.jpg&filename=lesson-thumbnail.jpg)
Let's learn how to control multi color LEDs using Arduino’s analog outputs. We'll connect an RGB LED to the Arduino Uno and compose a simple program to change its color. Here in Tinkercad Circuits, you can explore the sample circuit (click Start Simulation to watch the LED change color) and build your own right next to it. Optionally grab your electronics supplies and build along with a physical Arduino Uno, USB cable, breadboard, RGB LED, resistors (any value from 100-1K ohms will do), and some breadboard wires.
Additive, or light-based color has three primary colors: red, green, and blue. Mixing these three colors in different levels of intensity can create almost any color of light. Color changing LEDs work the same way, but the LEDs are all together in a small package we call an RGB LED. They have four legs, one for each color and one for either ground or power, depending on the configuration. The types are called "common cathode" and "common anode," respectively.
- Continue to the next step.
Build the Circuit
![RGB-freewired.jpg](/proxy/?url=https://content.instructables.com/FZR/CAR5/JDCBOIZR/FZRCAR5JDCBOIZR.jpg&filename=RGB-freewired.jpg)
Since you’re just getting the hang of the breadboard, here’s the equivalent free-wired version of this circuit for comparison. You can build either way in the Circuits editor, but if you are also building a circuit with physical components, the breadboard will help your virtual circuit look the same.
- Try it for yourself! Add a new Arduino and breadboard along side the sample, and prep your breadboard by connecting Arduino 5V to the power rail and Arduino GND to the ground rail.
- Add an RGB LED from the components panel and place it across four different rows of the breadboard.
- The RGB LED in the simulator has a common cathode (negative, ground) at its second leg, so wire this row/pin to ground.
- Add three resistors (drag all three or create one and then copy/paste) and move them to the breadboard rows for the remaining three LED pins, bridging across the breadboard's center gap to three separate rows on the other side.
- Connect wires from the free resistor ends and to three of your PWM-capable Arduino pins, which are marked with a
tilde (little squiggle).
HINT:
Although you may be tempted to consolidate and use a single resistor on the common pin, don't! Each LED needs its own resistor since they don't draw exactly the same amount of current as each other.
- Tidy up your wires by adjusting their colors (dropdown or number keys) and creating bends (double click).
- Continue to the next step.
Color Mixing Code With Blocks
![testing-code-blocks_optimized.gif](/proxy/?url=https://content.instructables.com/F0F/OQBY/JDUKHV98/F0FOQBYJDUKHV98.gif&filename=testing-code-blocks_optimized.gif)
We'll use the code editor to test the wiring and adjust the LED's color. Click the "Code" button to open the code editor. You can toggle between the sample code and your own program by selecting the respective Arduino board in the workplane (or dropdown menu above the code editor).
- Drag an RGB LED output block into a blank program and adjust the dropdowns to match the pins you connected earlier (11, 10, and 9).
- Choose a color and click "Start Simulation" to watch your RGB LED light up. If the color doesn’t seem right, you probably need to swap two of your color pins, either in the wiring or the code.
- Create a colorful light show by duplicating your RGB output block (right click->duplicate) and changing the color, then adding some wait blocks in between. You could simulate a racetrack countdown, or color changes to go with your favorite song. Also check out the repeat block-- anything you put inside will happen on repeat for the specified number of times.
- Continue to the next step.
Arduino Code Explained
When the code editor is open, you can click the dropdown menu on the left and select "Blocks + Text" to reveal the Arduino code generated by the code blocks.
-
void setup() { pinMode(11, OUTPUT); pinMode(10, OUTPUT); pinMode(9, OUTPUT); } void loop() { analogWrite(11, 255); analogWrite(10, 0); analogWrite(9, 0); delay(1000); // Wait for 1000 millisecond(s) analogWrite(11, 255); analogWrite(10, 255); analogWrite(9, 102); delay(1000); // Wait for 1000 millisecond(s) }
After setting up the pins as outputs in the setup, you can see the code uses
analogWrite()
like in the last lesson on fading an LED. It writes each of the three pins with a different brightness value, resulting in a blended color. - Continue to the next step.
Build the Physical Circuit (Optional)
![physical-circuit.jpg](/proxy/?url=https://content.instructables.com/F7P/RVSG/JDUKKSDD/F7PRVSGJDUKKSDD.jpg&filename=physical-circuit.jpg)
![physical-circuit_optimized.gif](/proxy/?url=https://content.instructables.com/F94/Y0V7/JDUKKSWE/F94Y0V7JDUKKSWE.gif&filename=physical-circuit_optimized.gif)
To program your physical Arduino Uno, you'll need to install the free software (or plugin for the web editor), then open it up.
- Wire up the Arduino Uno circuit by plugging in components and wires to match the connections shown here in Tinkercad Circuits. If your physical RGB LED happens to be common anode, the second pin should be wired to power instead of ground, and the brightness values 0-255 are reversed. For a more in-depth walk-through on working with your physical Arduino Uno board, check out the free Instructables Arduino class (a similar circuit is described in the second lesson).
- Copy the code from the Tinkercad Circuits code window and paste it into an empty sketch in your Arduino software, or click the download button (downward facing arrow) and open the resulting file using Arduino.
- Plug in your USB cable and select your board and port in the software’s Tools menu.
- Upload the code and watch your LED change color!
- Continue to the next step.
Next, Try...
![rgb-simple.gif](/proxy/?url=https://content.instructables.com/F5R/BFSP/JDCBOJ67/F5RBFSPJDCBOJ67.gif&filename=rgb-simple.gif)
Now that you know how to control RGB LEDs, it's time to celebrate your digital and analog output achievements! Using the skills you've picked up previous lessons on controlling multiple LEDs and using analogWrite() to fade, you've created a single pixel just like the (much smaller) ones inside the screens of your mobile device, TV, and computer.
- Experiment with different combinations of colors and repeat patterns by adding more code blocks.
- Try covering up your LED with different diffusing materials to change the light quality. You could try making LED diffusers from anything that lets light through, such as ping pong balls, polyester fiber fill, or 3D printing.
- Next up in your Arduino journey, try learning to detect input with pushbuttons and
digitalRead()
.