Determining LED Resistor Values
LEDs operate by passing current through them which cause them to emit light. The more current that flows through the LED, the brighter the LED will be. LEDs typically don't limit the amount of current on their own, so if you hook an LED up to 5V and ground, you will typically get a very bright LED for a second or two before it fails.
To avoid damaging the LED or possibly the drive circuit that is driving the LED, Most LEDs will require a current limiting resistor (also sometimes called a voltage dropping resistor) in series with the drive pin of the LED to limit the current to a safe level. There are some LEDs that have built-in current limiting resistors, but these are rarely encountered.
If the LED is composed of multiple LEDs within the same package with multiple drive pins such as is found with RGB or 7-Segment LEDs, each of the drive pins will require their own current limiting resistor. Using a single resistor on the common pin which attaches to either Vcc or ground is possible, but then the brightness of the LED will vary depending on which pins are being driven and that strategy generally gives unsatisfactory results.
Calculating the correct resistor value to use is fairly straightforward using the following formula.
R = Resistor value in ohms
Vcc = Circuit operating voltage which will typically be 5V or 3.3V
Vled = Voltage drop across the LED
Iled = Desired current flow through the LED in amps
As an example, if the circuit is operating at 5V, the voltage drop across the LED is 2V and you want a drive current of 20mA, the formula looks like the following:
In this case, the correct resistor value is 150 ohms.
Sometimes values may not be known, but they can experimentally determined or estimated well enough to avoid damage.
If the maximum current for an LED is not known, it is generally safe to assume that most LEDs will operate up to at least 20mA. 20mA is also the maximum current draw that it is safe to assume that output pins of a microcontrollers can support so when in doubt, keep things down around 20mA or less. Many LEDs will have good brightness down at around 10mA and running at a lower current reduces the stress on everything and reduces overall power consumption. This can be especially important for battery operated equipment.
If the voltage drop of an LED is not known, simply hook the LED up to 5V and ground using a larger current limiting resistor of at least 330 or 470 ohms to ensure that currents are kept at a safe level. Then measure the voltage drop across the 2 leads of the LED using a voltmeter. Note that the exact voltage drop will vary a bit depending on how much current is flowing through the LED.
There are some LEDs that may require stronger drive than 20mA such as some IR LEDs or high power LEDs, but these will generally be provided with the necessary information to drive them properly. Because of the stronger drive requirement, these LEDs are typically driven by specialized driver circuits rather than directly from the microcontroller pins.