Current Per Pin - Part 2
Last time we took a look at the basics of electrical current and tried to get a feeling for how much current was drawn by incandescent light bulbs. This time we will be looking at current, LEDs, and microcontrollers.
Little Red Currents
Moving closer to our microcontrollers, let’s look at LEDs. LEDs work at a much lower voltage, typically 2 V for red, yellow, and green and 3 V for blue and white. But they are also designed to be at their maximum brightness at 20 milliamps (0.020 amps) of current, and newer low current LEDs are closer to 2 mA.
But, LEDs have a nasty trait. Unlike an incandescent light bulb, LEDs cannot adequately limit the current flowing through them. They have a very low resistance and will flow a lot of current, then heat up to the point that they boil their epoxy encapsulation and melt their bonding wires.
To stop this, we attach a resistor right before the LED to limit the amount of current that can flow through the LED. You need to do a simple calculation that takes into consideration your system voltage, the LED voltage, and how much current your LED draws at peak brightness, but there are online calculators to do it for you.
If you look at schematics, you will see that LEDs are usually paired with a resistor. If we look at the typical first Arduino blinky project sketch we can see the LED and resistor:
On later versions, like the Due Milanove, there is an LED built on with a current limiting resistor.
Backward Current
Just to screw everything up, back in the dawn of time, a British inventor and rebel leader named Benjamin Franklin got it all wrong. He figured that flowing electricity had a positive charge. We now know that negatively charged electrons flow and positively charged protons don’t. Instead of fixing this misunderstanding, the electronics industry embraced it and our circuits appear to flow protons.
This is known as Conventional Current. You will see that current appears to flow from the positive pole of a battery to the negative. The voltages in your PC are +3.3, +5, +12, and ground, like a bunch of positrons without the MC^2 annihilation event.
The best I can suggest is don’t worry about it. You can either just accept that protons or magic positrons are free to flow, or assume the charge on an electron to be positive. Everything still works, don’t worry about it (or think about it for that matter).
Next, it’s important to understand that current is pulled, not pushed. A light bulb draws ½ amp. An Intel core I7 processor can draw 83 amps. A PC power supply does not push 800 watts of power, the system pulls what is required, usually about 40 watts, the rest is just reserve capacity and marketing.
Keep the Current Smoke In Your Processor
Okay, enough background information, what does this have to do with microcontrollers? Each processor has a parameter that you need to know about; the maximum allowable current per pin. This is the amount of current that the processor can supply on the I/O pins.
When you look in the data sheet for your processor there is a section called the Electrical Characteristics. The two parameters that you should look for are the current sourced per pin and the total current sourced for the processor.
When you hook an LED to your processor it will consume current that has to be provided by the processor I/O pin. When you hook up a bunch of LEDs to a bunch of pins, the processor chip has to supply all of those pins and there is a limit. But how much can the processor supply?
Processor Model | Current per pin | Total current on all pins |
---|---|---|
ATMEL ATMEGA328 as used on an Arduino | 40 mA | 200 mA |
Microchip PIC18F4550 | 25 mA | 200 mA |
Microchip PIC18F25J50 | PORTA pins other than RA6, all of PORTD and PORTE, 4 mA per pin.Others 25 mA | 200 mA |
ST STM32F407 | 25 mA | 240 mA |
As you can see, the ability to provide current varies depending on the processor and manufacturer. The current capacity can be asymmetric within the processor. And the sum of all of your current is limited too, far less that what would be needed to light an incandescent light bulb.
If you were to connect an LED directly to an I/O pin without a resistor, and turn it on, the LED, being very low resistance, will draw a large amount of current. The result will be the release of Magic Smoke and the smell of burning epoxy from the LED and your processor.
Alternative Current
Current limiting resistors are great if you need to restrict the current below what the processor can provide, but what do you do if you need more current than the processor can provide?
Elecia wrote a post on the lights in a Sphero BB-8 robot toy and there, they use a transistor that uses a minute amount of power to turn on a blue LED that consumes a large amount of power. These FETs can also be used to turn on relays, motors, and other large current consumers.
If you need to light up a whole lot of LEDs but the total current draw would blow your processor, or you don’t have enough pins to light them all at the same time, you can use a form of time division multiplexing called Charlieplexing. With Charlieplexing, instead of turning on, say, 100 LEDs at the same time, you divide your time turning one LED on at a time then turning it off, and moving on the next one, going around and around. Better yet, do 10 at a time. The idea is that by rapidly turn on and off each LED in turn, they are blinked often enough that your eye perceives them all being on, but the processor only supplies current to one at a time.
You can also use peripheral chips to do the multiplexing for you, like the AMS AS1130 as used in the Boldport Club - Matrix project. This chip takes I2C commands to control the lighting of up to 132 LEDs in an 11x12 matrix.
Proper Termination
There was a bit of background information to go through to discuss the current limitations of our processors. What is an amp, how big is it, and how little it takes to blow up an LED. But this is still more than a computer chip can handle. Don't try and spin a motor directly from a processor I/O pin, just don't.
So be conscious of the current your circuits are drawing and try some of the techniques to limit the current consumption so your Magic Smoke stays in.
This post is part of a series. Please see the other posts here.
By Khaifuture (Own work) [CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0)], via Wikimedia Commons
An Arduino with an LED connected without a current limiting resistor. The photographer writes: “This LED blinks every 3 seconds, The LED is connected to the PIN13 on the Arduino, the ground is also connected with the LED on the board. I should have put a resistor on the board so the LED won't burn out, but it was for just testing that's why I didn't put it on this image.”
Maybe not today, maybe not tomorrow, but soon, when wearing in a big rubber suit out in a sewage lagoon, he'll think, "Why did that blow? I never had to put a resistor on my Arduino. It hasn't been a problem so far. Oh, wait..." And he will regret it.
Music to work by: Talking Timbuktu by Ali Farka Touré and Ry Cooder