# What is the resistor for ?

In 2 of the examples there is a resistor used and I don't understand why.

That same leg of the button connects through a pull-down resistor (here 10k ohm) to ground

Connect the anode (the longer, positive leg) of your LED to digital output pin 9 on your board through a 220 ohm resistor.

Why could not the connector go directly to pin 9 and ground ?
How did the developers calculate the values, 10k, and 220 ?

Electricity is weird stuff.

Thanks

Current limiting.

Digital Read Serial: Pull-down. If the button is open, there is nothing that would define at which voltage the input pin is. It would not go back to low after the button is pressed, and it could pick up all kinds of EMI.
Imagine this: The button floats the (tiny capacitor at the) pin with charge, the pull-down drains it. You may imagine it like water.
Calculation: It doesn't matter so much. It shouldn't be too low, because else a lot of current is wasted through the pull-down (and it might get hot or burn in the worst case), and it shouldn't be too large because lese it wouldn't drain the pin fast enough. Aim for something in the one or two digit kOhm range. Often, it doesn't matter much.

Blink: It limits the current. The LED alone does not limit the current from the pin sufficiently. If you just connect the led, a lot of current will be drawn from the pin and it (the led or the pin or both) will burn.
Calculation: Use Ohm's law, such that 20mA at max may flow. If you don't want to make it as bright and efficient as possible, make the resistor large enough to do that job alone, or almost alone. R=U/I -> I=U/R -> I = 5v/220Ohm = 22.7mA. The LED will take it below 20mA.

In the button example, you connect the digital pin to ground, through a resistor, otherwise it can cause strange behaviors on other circuits when the button is not being pressed. I forgot this on one of my projects, and it caused some analog readings to be unreliable. It essentially connects the digital pin to ground when the button is not pressed. Then when the button is pressed, the resistor is needed so the current flowing through the button goes to the digital pin, and not to ground; since it will take the path of least resistance.

In the LED example, the resistor is used to limit the current. You determine the size based on the LED's datasheet.

A quick Google search for "why use a resistor with an LED" provides lots of information if you want to know more:

http://led.linear1.org/why-do-i-need-a-resistor-with-an-led/

Thank you very much, and Stoopalini, I welcome the link as i am eager to learn.
I am comfortable with the programming side but the EE stuff is a mystery.