Connect a RGB LED to make any colour you wish!
An RGB LED is essentially a red, green and blue LED combined into one package.
An RGB LED has four legs: an anode to drive each of the three component colours and a common cathode. The longest pin is the common cathode (-) and is used by all other LEDs in the package.
Each of the colours—red, green and blue—can be made my supplying current to the shorter pins. Usually the order of the pins is the same as the diagram above; but occassionally they can change, and sometimes it’s even a common anode.
Any coloured light can be made by varying how bright the red, blue and green LEDs are lit. This can be done with
pin0.write_analog(n) where n is a value between 0 (off) and 1023 (on full).
220Ω resistors are placed before the each of the anode pins. 220Ω is a safe choice and will cover all RGB LEDs. You can consult the datasheet of the LED if you have it; you may not even need a resistor but it’s better safe than sorry!
pin0.analog_write(512)is half as bright as
random.randrange(start, stop, step)to generate a random colour every few seconds
Published: December 23, 2016 Updated: December 23, 2016
Acknowledgements: RGB LED teaser image by Oomlout (CC-BY-2.0). RGB spectrum image public domain.