If you just wanted to test it out without using a microcontroller or any other hardware, then just connect the common anode straight to your power supply (most commonly 5volts if you are working with microcontroller circuits)
Then you will need three resistors. One for each cathode.
Connect each cathode to ground and you will light up that corresponding color. you can also mix the colors by lighting up more than one at a time.
If you are connecting it to a microcontroller then again just connect the common anode to your power rail (VCC) and then the other three connections (through your resistors) to three port pins of the microcontroller. The easiest way to work with it now is to just pretend they are common cathodes but before you send the data out to the LED pins, you just need to invert the data.
so if you wanted to cycle through the colors and you connect the LED to PORTA pins 0, 1 and 2 and you're programming in basic, you would do this:
Code: Select all
while true
inc(leds)
leds = leds xor 255 ; this inverts the data in leds and stores it back in leds
porta = leds
leds = leds xor 255 ; invert back to original
delayms(500)
wend
If you ever are dealing with common anode (rather than common cathode) then you just need to invert the data before sending it to your port. Remember we are only dealing with the first three port pins of porta so if we started at 000 and sent that data to a common anode RGB LED, it would actually light them all up, so before we send the data out, we invert it using the xor instruction.
we xor our 8-bit variable with 255 (which is binary 11111111)
if we had the number 00000101 (which means that we wanted the first and third color to be lit) then putting it through the xor instruction we get:
Hopefully that helps!