- Joined
- Jan 21, 2010
- Messages
- 25,510
The specs say that each output can sink or source 20mA, but the max combined current is only 90mA. Why is the heck do they put so output pins on them if they can only do 90mA?
This is typically a package dissipation limit. If you draw more current in total then the chip will get too hot (this is somewhat simplistic, there may also be other reasons).
It is not typical to use the outputs of a microcontroller to drive a load directly. LEDs are an exception because they draw such a low current. Normally, you'll see loads driven by external transistors mosfets, etc. If you imagine that these devices may require only 1mA (or less) then you can see how the package limit is not a huge issue in most cases.
...so I would rather use common cathode...
I have some BC327 PNP transistors. Will that do the trick for my amplifier? If not, could you or Steve recommend something?Yes, PNP transistors allow you to switch the common cathode LED directly, but you need to invert your signal. This can be a bit of a pain. It may be less of a pain to use another transistor to invert the signal so that both common anode and common cathode LEDs can be driven without changing the code.
Let's draw some circuits...
Here is the simplest way of driving common cathode LEDs:
Clearly you run into package dissipation issues, and the LED cannot require more voltage (or current) than the uC can provide at its output. R1 is used to set the current.
When the output id high, the LED is on. So the PWM ramping from 0 to 400 will go from off to a medium brightness.
And here is how you can simply drive a common anode LED [thanks for the correction]
Same comments about voltage and current.
Now, however, the LED is ON when the output is low, so using PWM to ramp from 0 to 400 will result in the LED going from fully ON to something a bit less bright.
In this case, you need to ramp from 1024 to 624. And you have to remember to tie the output high when you want the LED to be off.
So, you can do this
Here we drive a common anode [thanks again] LED via a transistor (NPN). R1 again provides the current limit, and R2 sets the base current into the transistor. R2 is often going to be about 100 times larger than R1 for low current LEDs (but that's a very rough calculation which may not apply to high current LEDs.)
This circuit has a number of advantages. Firstly, when the output is on, the LED is ON, so your programming is easy. Secondly, the LED can be operated from a voltage higher than the uC.
A similar circuit can be made for common cathode LEDs
This is pretty much the exact opposite. And it suffers few of the advantages of the circuit above.
The LED voltage can only exceed the uC voltage if the "ground" voltage is below the ground of the uC, and the LED operates in the opposite sense to the uC output.
But we can fix that...
Adding another transistor to invert the output, the fourth circuit gains all of the advantages of the third. R2 can be even higher in this case (up to 1000 times R1), with R3 and R4 being approximately equal and 10 to 100 times R1 (which sets the output current)
Attachments
Last edited: