


We will control the RGB LED color by outputting HIGH (5V) or LOW (0V) using digitalWrite to these pins.įor example, to make the RGB LED turn red, we would write: digitalWrite ( RGB_RED_LED_PIN, HIGH ) digitalWrite ( RGB_GREEN_LED_PIN, LOW ) digitalWrite ( RGB_BLUE_LED_PIN, LOW ) Recall that the embedded red LED is hooked up to Pin 6, the blue LED to Pin 5, and the green LED to Pin 3.
#LED ANODE CATHODE ARDUINO CODE#
We are going to write code that flashes through a sequence of colors. Notice how the schematic highlights how the current flows from the I/O pins, through the resistors, into the RGB LED, and then down to ground. Here’s the wiring without a breadboard (not recommended but perhaps easier to see the circuit itself):Īnd here’s the wiring with a breadboard (the schematic on the right is the same either way). We could have used any digital I/O pins however, we chose Pins 6, 5, and 3 for red, blue, and green respectively (these pins support PWM and thus, we’ll be able to use the same circuit for our next tutorial on cross-fading colors with RGB LEDs). With a Common Cathode design, the 2nd leg needs to be hooked up to the GND pin and the 1st (red), 3rd (blue), and 4th (green) legs should connect to digital I/O pins ( don’t forget a current limiting resistor for each). We’re going to start with a Common Cathode circuit + code because it’s more intuitive (and similar to our red LED tutorials) before covering the Common Anode version. Notice how the RGB LED type is clearly labeled. Here are two screenshots of supplier websites ( Sparkfun and Adafruit). It’s called diffused because the epoxy casing is scratchy and not perfectly transparentĪ clear Common Cathode RGB LED from Sparkfun. Instead, consult the supplier website, the datasheet, or experiment with the LED yourself (remember, diodes only work in one direction so as long as you include your current limiting resistors, you should be fine!).Ī diffused Common Anode RGB LED from Adafruit. You cannot tell whether you have a Common Anode or Common Cathode RGB LED by visual inspection. How can I tell if I have a Common Anode or Cathode RGB LED? So, you drive each individual color leg with a higher voltage source. Here, all three embedded LEDs share the cathode leg. In contrast, the Common Cathode works much more like a typical LED ( e.g., like the red LED from our previous lessons).For example, with a 5V voltage source connected to the common anode leg, setting the three other legs (red, green, blue) to 5V would, somewhat counterintuively, turn off the LED. To control the RGB LED’s color, you must hook up the anode to the higher voltage source and connect the red, green, and blue legs to lower voltage levels ( e.g., ground). With the Common Anode, the three embedded LEDs share the anode leg.Importantly but somewhat confusingly, there are two RGB LED designs-the Common Anode and the Common Cathode-which differ in the leg shared between the three embedded LEDs. While capable of displaying thousands of color combinations, RGB LEDs are actually quite simple they contain three separate LEDs in one package: red, green, and blue. RGB LED (Either Common Cathode or Common Anode) L4: Feature Selection and Hyperparameter Tuning.
