Electronic – Is it possible to get the full spectrum of a RGB diode (3 prong input) through 1 GPIO pin

gpiopcbpcb-design

I apologize if this question is not well formed. I am an amateur in this field.

I have the requirement to independently control a plurality of RGB diodes from a single microcontroller. Each diode has 3 input pins (each corresponding to one of R, G, & B). This is the led.

I want to hook each of these diodes up to a Adafruit Trinket; however the Adafruit Trinket only has 5 GPIO pins, which, I am guessing would only allow me to control the RGB spectrum of one diode and 2 colors on another diode. (3 + 2 = 5).

As per my requirements, I need to independently control 6 diodes, which, as I understand, would require 18 GPIO pins; far more than are available on the Trinket.

I am wondering if there's some other smart way to somehow control the full RGB spectrum on one diode using just one GPIO pin; or if there's some standard way to control these diodes that I am not familiar with; for example, extending the number of GPIO pins on a board, etc.

If not, and each diode does require 3 GPIO pins, I wonder how someone would go about prototyping a device that requires many GPIO pins yet also needs to be physically very small (similar in size to the Trinket). Would I just go about using a larger board that accommodates more GPIO pins and plan on downsizing considerably when the custom PCB is made?

Thanks in advance.

Best Answer

There are several ways to use fewer microcontroller pins than you have diodes. Which route you choose depends on details like required brightness, whether you want to buy or build stuff, how much you want to spend, form factor, size etc etc.

An RGB LED contains 3 independently driven diodes. I'll refer to this as '3 diodes' or 'an RGB', so we don't over or under count.

Simplest solutions first

a) One diode per pin, as in your question. This is the simplest. 5 pins will drive 5 diodes, or nearly 2 RGB LEDs. Each pin can be PWM'd to control individual diode brightness, so you can get the full available colour gamut from one of the RGB's.

b) Buy WS2811 or 2812 driven RGBs. These are usually available in a long strip. The entire strip can be driven from one or two GPIO pins (depending on whether it's a '1 or a '2) and strips can be cascaded and still need the same number of pins. The strips often have break lines with pads either side between the diodes so you can separate the RGBs, wiring across the break with some lengths of wire.

The timing required on the drive pins is a bit tricky, but most popular MCUs have driver libraries available for these ICs.

Or you could buy the WS281x ICs themselves and build with them, but it's far cheaper and easier to get the driven RGBs. WS2811 WS2812.

As jcaron comments below, APA102 is another option (9822 being a common clone of that), with more conventional SPI programming, and a much higher PWM frequency (which improves on the horrible 281x flicker (though some later 281x's have apparently higher PWM frequency)), but they appear to be much more expensive than 281x's at the moment (early 2019).

c) Multiplexing. Each diode spends some time off, so you get lower maximum brightness. Each GPIO pin drives the anode or cathode of several diodes. With 5 pins, you would typically split them into 2+3, so could drive 6 diodes, or 2 RGBs. You will typically need a current booster to drive the common lines, unless you want to suffer a further brightness hit. If you decode the device selection lines rather than using them directly, so use the group of 2 lines to select one of 4 devices, you could drive 4 RGBs.

d) Charlieplexing. Like multiplexing, but a more subtle arrangement, that dynamically reuses the undriven common pins to drive the other diodes. You can drive up to n(n-1) individual diodes, which is 20 in your 5 pin case. When the diodes are physically grouped into 3s as in RGBs, there will be a restriction due to this, but you can still drive 5 RGBs with 5 pins (and add another 5 single diodes to the matrix as well).

e) GPIO expansion. You can use ICs like HC595 to increase the effective number of output pins on an MCU, this has 8 outputs and can be cascaded indefinitely for more outputs. Most popular MCUs have libraries which simplify the driving of such expanders. HC595 has lower current output than typical MCU pins and will not light LEDs as brightly without drivers, though 6mA will still get a reasonable brightness. There are other ICs available with more pins, and higher current outputs.