In this chapter, we will be using an LED driver chip to drive a few Red/Green/Blue (RGB) LEDs to generate any color in the rainbow. We will read through the specifications for an LED driver chip and learn how to create a serial data stream that is tailored to that particular chip. Then, to improve performance and further offload workload from our software, we will use Direct Memory Access (DMA) to execute the serial data transfers.
As we have learned in chapter five, the EFM32 has three timers that drive up to twelve GPIO pins that are each able to perform Pulse Width Modulation (PWM) to a channel of LEDs. But what if your design requires more than twelve channels or you have other plans for those timers? The answer is to turn to a purpose-built LED driver chip such as the TI TLC5940.
The TLC5940 LED driver has 16 channels of LED driver circuits and is capable of driving up to 120 mA per channel. In addition, multiple TLC5940 chips can be chained together without consuming any additional MCU pins. Although the specification is geared toward LEDs, there is no reason why it can’t be used to drive other components that need a PWM signal, like motors or servos. The TLC5940 also has a pin that sets the current limit for all pins on the device, which means that you can omit the LED protection resistor in your circuit, if desired.
We can use any of the available serial data peripherals on the MCU to communicate with the TLC5940, like UART, LEUART, or USART.
The source code for all examples in this book can be found on Github here.
Materials Needed for This Chapter:
The TLC5940 LED driver is a device that receives a serial data stream and uses that data to connect its 16 output pins to ground, sinking current at specific slices of time. When it is not sinking current, the output pins float. This is a typical part for which there are many variants on the market that do similar things with differences in the number of output pins, current capacity, and additional features. It has 4096 settings of PWM brightness that are all controllable per output pin based on the values contained within the serial data stream. The current capacity of all output pins is limited by the external sense resistor on the board, but the current limit of each individual output pin can be further adjusted by the values within the serial data stream.
The specification has a few terminology nuances that you should note. Each output pin is called a channel, the PWM brightness value is called greyscale (GS), and the individual limit on current per channel is called dot correction (DC).
Since each channel can drive up to 120 mA, that means that each channel can drive more than a single LED. The forward current of a typical LED is 20 mA, and you can therefore drive six LEDs in series if you have the voltage on board to drive them all. Depending on the color, the forward voltage is in the 2 to 3.3 V range, so you would need to power six LEDs connected in series with 12 to 19.8 V. If your design is limited to low voltage such as 3.3 V, you can connect the six LEDs per channel in parallel, but we must then utilize current-limiting resistors with each LED to ensure that the current through each LED is consistent. There are manufacturing variances in all LEDs, and if we were to connect them in parallel to the output pin of the TLC5940 without a current-limiting resistor, one of the LEDs could draw more current than the other LEDs, causing it to be brighter than all of the other LEDs, and perhaps damaging one or more of the LEDs in parallel on a single TLC5940 channel. We don't have that problem if only one LED is connected per channel, or if multiple LEDs per channel are connected in a serial fashion. See the following figure for clarity.
It may be possible to drive many more than six LEDs per channel if the current of each LED should be be smaller than 20 mA. The key to keeping color consistency is to make sure that each LED gets its correct forward voltage. Dimming can be achieved by either applying a PWM waveform to the voltage pin, reducing the current through the LED, or both techniques at once.
This part can drive single-color LEDs, multicolor RGB LEDs with three discrete colors, or RGBW components that have three colors plus a dedicated white LED. However, when driving a multicolor LED, we must find common anode parts. Common anode devices have a single “common” anode pin, which should be the higher-voltage pin for the LED to turn on, and three or four cathode legs that are to be grounded to illuminate each of the discrete LEDs within the multicolor component.
Sparkfun TLC5940 Breakout Board Overview
The Sparkfun breakout board for the TLC5940 has some nice layout features that will enable us to experiment with RGB LEDs right on the breakout board without the use of a breadboard. You will need to solder header pins to the control signals on the left and right side of the board, and you can also solder header pins on the output channels and Vcc/Ground connections at the bottom of the board. I chose to solder my four RGB LEDs right into the output channels and Vcc pins right on the breakout board. If you look closely, you can see how the red, blue, and green legs are on the top row, while the anode pins are on the Vcc row.
NOTE: If you choose to solder your LEDs right into the breakout board and later want to wire up a different circuit to the breakout board, it is possible. You will need to use a solder sucker (or desoldering tool) or solder wick to remove the solder around the pin. First, add a bit of new solder to the joint. This adds more flux, which is in the solder and helps solids flow like a liquid when heated. Then, you heat up the joint and move your solder sucker quickly over the joint, and push the button to create suction. Alternatively, using solder wick, place the wick over the solder joint and heat the solder. The wicking material will absorb the solder. If you are lucky, the pin will be cleaned of enough solder to break free from the through-hole joint. If you are not so lucky, you will have to keep trying and use a fine-tipped soldering iron to push the pin back through the hole.
The Sparkfun breakout board has pull-ups on each of the output pins of 2.2 kΩ to Vcc. This keeps the output pins from floating and returns the voltage to a high level quickly. That is helpful if you are driving a circuit that has a sensitive input without a lot of loading, such as a transistor used to turn on a common cathode bank of LEDs.
The maximum current that the part can sink is determined by the IREF pin. A resistor placed between the IREF pin and ground will set the maximum current, called I(max) in the spec. The resistor chosen for IREF calibration on the Sparkfun breakout board is 2.2 kΩ. This is located at the top of the board and is a 0603-sized package, but the board offers a through-hole connection for your own resistor to replace the 0603 resistor, if you choose. Note that the two resistors will then be in parallel, which will create a reduced overall resistance. The maximum current sunk by any output is given in the device data sheet as:
where V(IREF) is set to 1.24 V by the chip. Therefore, 2.2 kΩ gives an I(max) of 17.7 mA. The spec states that I(max) should be set between 5 mA and 120 mA, and that means that this breakout board is configured by Sparkfun to drive at the lower end of the possible current sink range per channel. This is safe for a single LED, and we don’t need to use a current-limiting resistor. The TLC5940 takes care of doing that for us.
The maximum current is further adjusted down by the dot correction value per channel (DCn):
Note that you must always populate a resistor for R(IREF). Do not assume that just because you know you will never drive more than 120 mA, for example, that the part will function if you just throw any random value resistor on there, or worse, leave the resistor off completely. It probably won’t work at all.
Remember that you are still able to build your own breakout board for an LED driver chip that doesn’t have a readily-available breakout board like this one from Sparkfun. Refer to chapter 9 for more details on how to create your own breakout board.
In the next section, we will connect the TLC5940 to the Starter Kit and begin to write the driver to control it.