RGB :: Common Cathode, Common Anode

As you are all aware I was surprised by the fact that, while most of the 2021 kits have common cathode RGB LEDs, a significant number have common anode RGB LEDs. We are going to make the most of this glitch and take this opportunity to think about different ways to control LEDs.

Common cathode (from class — ‘expected / normal’) and common Anode ( from class — wonky) LEDs are virtually identical in appearance and there is no difference in their spectrum of color output. So, from these perspectives, common anode and common cathode are fully interchangeable.

However, in terms of inner connections and electrical setup they are quite different; in fact, they are mirrors of each other. They require different wiring — and sending the same code code to one channel of each leads to opposite behaviour. These differences reveal some interesting patterns that, while a bit advanced, are useful to think through.

What’s Wrong with Wonky RGBs?

What’s Wrong with Wonky RGBs? Nothing. Literally nothing at all. I love a wonky LED.

The issue with this mix up has much more to do with us being remote than it does with any actual problem with the LEDs. (Had we been in a face-to-face class we would have just switched them). We (the makerSpace team and I) have never ordered common anode RGB LEDs — so being able to deliver them to you was a genuine surprise! (After school special kind of surprise).

Discovering and coming to understand what happened partway through a remote class — and having no access to one for me to test with and confirm — made for a challenging pivot, as you all witnessed.

The take away here is, if you received a common anode (wonky) instead of a common cathode (expected) RGB LED you can still do everything I have asked — it just takes a small bit of extra understanding. And some clarifying on my part.

There is a bright side. This shipping glitch has given us a chance to explore some really interesting aspects of electronics. To gain this understanding we need to resolve two issues: identification and signal switching (a strength of the Arduino). These issues are entangled — I am going to deal with the identification problem first, and the switching problem second.

Identifying Your RGB LED

Cathode vs Anode

Whats the difference between a common cathode and common anode LED? Recall from our basic LED introduction that for a single color LED we need to pay attention to which leg is facing ground.

Single color LEDs require that the shorter leg, which is below the flat side of the LED cap, faces ground. In the basic intro I mentioned in passing that the short / flat side leg is called the cathode, the other leg is the anode. I also mentioned and repeated that this means LEDs have polarity. At the time, I led (is that a pun?) you to believe that it would not matter if you could not remember these different labels.

Well — now, is the time to remember.

ALL LEDs have at least one anode (positive side) and one cathode (ground side). RGB LEDs are three-in-one-LEDs arranged with each other in parallel inside a single package. Consequently they have several legs. You might expect a 3-LED component to have 6 LEGs. This would be possible; but it would be a lot to keep track of AND it would be more expensive to produce. Manufactures realized an LED can be controlled by changing voltage on only one side of the circuit. So they make a decision and wire all of the anode (+ve) legs OR all of the cathode (-ve) legs of the RGB LED together internally. Thus reducing the number of legs on the component.

Our RGB LEDs are identified and grouped based on which side of the LED (+ve, -ve) is common. (BTW you will also see this pattern in 7-segment LEDs should you ever encounter one of those).

Image source.

The image above shows the pinout of common cathode (left) and common anode (right) RGB LEDs. In both cases, the LONG leg is common (wired together for all the internal LEDs). In the left component (common cathode), the long leg needs to be connected to ground. In the right (common anode) the long leg needs to be connected to +ve power (5V if using an Arduino Uno).

As you can see above, the big consequence of common cathode vs common anode is the kind of signal we need to send to the individual RGB legs to turn them on. Since the common side is inverted, it should not be surprising that the signals we have to send the individual R-G-B legs is also inverted (+ve signals to common cathode, GND signals to common anode).

Fortunately, the connection patterns between the LED legs and the resistors is the same — regardless of the polarity of the common leg. Once we know which side is common we can sort out the proper power and ground connections. And begin considering the code.

Which one do I have?

Since we are remote we have no choice but to press forward with the two kinds of RGB LEDs that ended up in circulation. And also, since we are remote, we do not have any diagnostic tools to help us out. We will have to sort this out experimentally.

As we did in class you can find out what kind of RGB LED you have by hooking up the common leg and one of the remaining RGB channels. Then power up and see if it glows.

To begin, disconnect power. Connect the common (longest) leg of your led to GROUND with a 470 ohm resistor. Connect ANY of the remaining legs to +ve (5V) with a 1k-ohm resistor. Power up.

Is it glowing?

If so, you have a common cathode (-ve) LED! You are done this part and can skip down to Switching HIGH, Switching LOW.

If it is not glowing — don’t despair. You almost certainly have a common anode LED. Let’s confirm. Power down your circuit. We just need to flip the LED. Recall that this is the thing we do whenever we build an LED circuit an it fails to glow (Troubleshooting in the alligator clip circuit Building Block).

Since we are dealing with four legs, not two (as we did with a single color LED), this is done most easily by leaving the LED in place and moving the resistor legs. We will start with the resistor connected to the common leg first. Take the leg of the 470 ohm resistor that is connected to the blue rail from our first test and MOVE it over to a red rail connected to 5V. Now connect your test RGB channel to ground (a blue rail) by similarly moving the leg of the 1k-ohm resistor between power rails. Power up your circuit.

Is it glowing?

If so, you have confirmed you have a common anode (+ve) LED.

If it is still not glowing, you may have some additional troubleshooting to work through. Check out ( Troubleshooting in the alligator clip circuit Building Block) but remember you are powering with 5V from Arduino not your battery pack.

You may want to mark your LEDs on the bottom with a ‘C’ or an ‘A’ if you have more than one kind.

Now, let’s discuss the electrical and code implications of these differences.

Switching High or Switching Low

Our goal is to control all three channels of our RGB LED separately. To do this we need to control the three parallel circuits individually. Electrically speaking, the circuits are copies of each other. So if we can understand the differences using single LEDs (which you all have), then we can extend that knowledge to the context of RGB LEGS — regardless of which polarity (common cathode (-ve) or common anode (+ve)) you have in your kit!

So far in this course we have turned things ON/OFF using code by connecting Arduino pins to the long leg (round side, anode) of our single color LEDs. In these cases, we have been connecting the flat side of our LEDs (short leg, cathode) to ground through a current limiting resistor. This gives the intuitive code pattern that sending HIGH signals through the Arduino turns the LED on.

Circuits with Static Anode Connections

With common anode (+ve) RGB LEDs in the mix we now have to look at an alternate connection pattern. Specifically, what happens when we connect the anode (long leg, round part of body) to +ve voltage through a current limiting resistor and we connect Arduino pins to the cathode (-ve) side of an LED?

In the remainder of this text I will refer to an LED leg that is connected via a resistor to ground or to 5V as being statically connected. I intend this to mean that the voltage at that pin does not change during different explorations. In contrast, Arduino pins are dynamic — their voltages can be changed on the fly with code. These are terms of convenience only — you are not likely to see them elsewhere.

Let’s consider a very basic pair of single LED circuits. One circuit follows the pattern we have already used and statically connects the cathode to ground through a current limiting resistor. The other circuit has an LED that is hooked up such that the anode has a static connection to the 5V (+ve) rail through a resistor. In both cases the remaining LED leg goes to a dynamic and changeable Arduino pin.

Finally, recall that voltage is defined as a potential difference between two points in a circuit. In other words, for our circuit to do stuff we need to have a top AND a bottom of a waterfall. As long as this is provided we will see the LEDs glow.

The video shows these relationships in action.

Video – Switching HIGH, Switching Low

Let’s build the circuits in the video.

REQUIRED PARTS

  • breadboard
  • Arduino
  • 2 leds (single color, both the same)
  • 470 ohm resistors
  • hook up wire (wire strippers)

What’s going On? :: digitalWrite()

Once you have the circuit assembled, upload the following digital output code once your circuits are built.

// digitalWrite -- with arduino pins connected to
// anode and cathode

int ledCC_1 = 3; // CC = common cathode (yellow)
int ledCA_1 = 9; // CA = common anode (white)

void setup() {
  pinMode( ledCC_1, OUTPUT);
  pinMode( ledCA_1, OUTPUT);
}

void loop() {
  // set both the CC and CA pins HIGH
  digitalWrite( ledCC_1, 1);
  digitalWrite( ledCA_1, 1);
  delay(1000);

  // set both the CC and CA pins LOW
  digitalWrite( ledCC_1, 0);
  digitalWrite( ledCA_1, 0);
  delay(1000);
}

Get the code on tangible github.

The illumination of each LED depends on the presence or absence of a waterfall. In the case of the left circuit (common cathode) with the resistor connected to ground the LED follows the intuitive pattern of setting a pin HIGH equating to turning ON the LED.

In the right example, with the anode connected statically to 5V, the LED only glows when the connected Arduino pin is set to LOW. At first this may seem counter-intuitive — the expectation is that setting the Arduino pin low should turn the LED off. However, due to the circuit pathway we constructed, setting pin 9 (in this case) LOW creates a waterfall (voltage difference) across the LED and it turns ON.

With the code above, the LEDs act as mirrors of one another and blink in an alternate pattern. Can you create code that blinks them in sync?

The following images illustrate these power and code relationships and how they relate to LED glow.

analogWrite

Let’s shift our attention to how these circuits behave when controlled with analog outputs.

// analogWrite -- with arduino pins connected to
// anode and cathode

int ledCC_1 = 3; // CC = common cathode (yellow)
int ledCA_1 = 9; // CA = common anode (white)

void setup() {
  pinMode( ledCC_1, OUTPUT);
  pinMode( ledCA_1, OUTPUT);
}

void loop() {

  for (int brightness = 0; brightness <=255; brightness ++) {
    // set both the CC and CA pins to brightness
    analogWrite( ledCC_1, brightness);
    analogWrite( ledCA_1, brightness);
    delay(10);
  }

}

Get the code on tangible github.

Once uploaded, we see that the LEDs fade in and out. But the fades also go in opposite directions! Again the circuits are actings as mirrors of one another.

In both cases the for..loop is being used to create a staircase of counts that range from 0 to 255. When the for..loop count is passed into the analogWrite command and written with PWM(~), a staircase voltage from 0V (GND) to 5V is generated at that ~pin.

Static Cathode

As a thought experiment, let’s again begin with the left circuit. It has a static cathode (flat side) connection and a dynamic connection to a PWM pin (analog output) on the anode (long leg). You should check the schematic and convince yourself that this technical description is accurate.

The Arduino pin is on the high side of the circuit and is generating the top of the water fall. As the for..loop begins (brightness = 0), the voltage at the Arduino pin is very close to ground (~0V). The LED is OFF or dim because the waterfall (blue bar in images below) is not yet very tall. As the for..loop gets to its end (255) the voltage approaches 5V — the waterfall is close to full height and the LED glows brightly.

Static Anode

Now let’s consider the static anode circuit. Again we need to think about the size of the waterfall — keeping in mind that the dynamic Arduino pin is now connected on the LOW side (cathode, flat side) of the LED. The waterfall height will be controlled by this connection.

As the for loop begins (brightness = 0), the voltage at the Arduino pin is again very close to ground (~0V). The LED in this case is very bright because the anode is pulled HIGH by the fixed resistor and a waterfall (voltage difference) exists across the LED.

It is important (i find this hard to work through sometimes) to really pause and see that, in this case, the waterfall is at its tallest at the beginning of the for..loop. As the for..loop gets to its end (255) the voltage on the flat side of the LED (cathode) approaches 5V. The waterfall height has been shrinking the whole time. At this point the right LED is very dim.

Note in the image below the LED is facing LEFT ! I did this to keep power info on the left. But it is easy to overlook.

Extending this concept to RGB

Once you understand the above concepts is it a short leap to extending this to RGB LEDs.

We know that to control the color of an RGB LED we need to send individual signals to each of the LED’s color channels. We have to pay attention to which side of the LED is wired up as common — anode or cathode? And which is wired to the Arduino.

If the cathode is common — then the RGB LED channels will individually act like the single LED with a static fixed resistor connected to GND (ground). Arduino pins will be connected through 1k resistors on the high (anode, long leg) side of the LED. Setting pin voltage to 5V with either digital or analog commands will cause the LED to glow. Colors can be created by varying the voltage on each connected pin.

If the anode is common — then the RGB LED channels will individually act like the single LED with a static fixed resistor connected to 5V. Arduino pins will be connected through 1k resistors on the low (cathode, flat side) side of the LED. Setting pin voltage to GND (ground) with either digital or analog commands will cause the LED to glow. Colors can be created by varying the voltage on each connected pin.

Summary

Any LED can be controlled by switching power on its high (anode) side or its low (cathode side). For a single color LEDs you can pick which leg to control. For RGB LEDs you are constrained by the manufacturers design. The common leg of these LEDs must be statically connected to either 5V or GND. The remaining legs can then be connected and controlled by dynamic Arduino pins. By mixing the voltages at these pins millions of colors can be produced.

Going Further

Another take on RGB and common connections.

Some info on seven segment LEDs mentioned above.

Leave a Reply

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