Pin 13 Considered Harmful
The scariest thing about teaching is the risk of being wrong.
Maybe you can relate? Written some blog posts, given a few talks, published an article, and then found out your spiel contained a piece of inaccurate—or even damaging—information? Hits right in the pit of your stomach, doesn’t it.
I’ve plugged an LED directly into pin 13 and GND. Don’t do this.
An LED can handle only so much current. The limit is printed on the packaging, usually something like 20 mA (milliAmps). If you exceed that, you’ll either gradually or immediately burn out the LED, and possibly damage the pin on the microcontroller chip. Therefore, LEDs must always be put in-series with a current-limiting resistor. Like this:
The formula for current is voltage divided by resistance. By convention, current is abbreviated as I, so you have: I = V/R. V is the 5 volts supplied by the Arduino, and you can see that if resistance (R) is really tiny (almost zero), current (I) gets huge! Followed by… bzorch.
Figuring out the right R value for your circuit is a blog post unto itself. In the meantime, there are plenty of online calculators, such as this current-limiting resistor calculator for LEDs.
The early-earliest revisions of the Arduino included a resistor on pin 13. Current versions do still have a resistor in series with the on-board LED attached to pin 13, but not with the pin itself. This was likely what confused me and keeps this incorrect advice bouncing around the ‘net. When a friendly electrical engineer at TechShop set me straight, it was, of course, after my article had hit the newsstands.
Ah, well. Another day, another chance to fry some components and become wiser. Forgive me, learn with me, use a resistor with your LEDs, and build great things!