Why is the resistor there?

Resistor will drop the voltage for the LED, which only requires about 2V. Arduino outputs 5V, and this would damage your LED if connected directly.

This is what it looks like, but technically it is incorrect. The LED does not require about 2V. The voltage drop across an LED is a characteristic of the device, not a rating. When you have current flowing through the LED then some voltage is dropped across it. Since an LED is not linear you cannot apply Ohm's law to it to determine it's 'resistance'. Well, actually you can, but you shouldn't since you would be wrong.

The LED has a current rating that has to be observed, not a voltage rating. The typical recommended current for a red led is 20 mA. The maximum permissible rating is higher but you don't gain significant brightness with more than 20mA. When the 20 mA is flowing through the LED there will be a voltage drop across in that is typically in the neighborhood of 1.7 volts as mentioned in a previous post. You can't apply Ohm's law to the LED but you can apply Ohm's law to the series resistor. So the trick is to apply Ohm's law to the resistor in order to get the desired current and, due to the two devices being in series, that same current will flow through the LED.

Assume that you apply 5V and you have the correct resistor in series with your red LED. There will be 20 mA flowing through each device and there will be about 1.7 V across the LED. This means that the other 3.3V is across the resistor. You can use Ohm's law to calculate the resistance, which comes out to 165 ohms. You next select the closest standard resistor value that you have on hand. If you have a 150 ohm resistor then the overall circuit current will be higher than 20 mA but still within the LED rating. If you have a 180 ohm resistor then the current will be lower than 20 mA but still quite usable. You don't have to be too precise here because (1) the LED will look the same over a wide range of currents and (2) the 1.7 V used in the calculations was an approximation in the first place.

Want to run the same LED on 12V (not from an Arduino output of course)? There will now be about 10.3 volts across the resistor and for 20 mA you will need a 515 ohm resistor. You may happen to have a 510 ohm resistor on hand but a 470 or 560 ohm device will also work.

Other colors of LEDs have different voltage drops when operated at their recommended current. Many require more than 20 mA to give sufficient brightness and most have higher voltage drops at the recommended current. So you wind up with less voltage across the series resistor and more current through it resulting in significantly lower resistance values.

In all cases don't forget to check on the required power rating for the series resistor. For the red LED example this would be less than 0.1 watt so a 1/4 w resistor would be OK but a 1/8 w resistor would be pushing things a bit.

Don