basic concept about resistors

I have trouble understanding why do I need resistor in this picture. Can't I make a complete circuit without the resistor?

I thought resistors are only for decreasing voltage before it goes into things like LEDs so that it doesn't get burnt out.

I just started learning arduino and have forgotten all the basic electro concept I learned back in school.

Any help is much appreciated.

The purpose of a resistor is to limit current - not voltage.

In this case, we have a momentary button - these buttons have their "contacts" connect via opposite corners of the device. So - for the same side of the device, both pins are electrically connected. Thus, those on the right hand side are connected together, and those on the left hand side are also connected together - but the left and right sides are electrically seperated - aka "open" - until the button is pressed - aka "shorted".

So - with that in mind, you can see that the digital pin 2 goes to the right side of the button, and that the resistor on that same side is connected, and then the other side of that resistor is connected to ground. This is called a "pull-down" resistor configuration; it's purpose is to ensure that the digital pin is kept LOW when the button isn't pressed (otherwise, the pin is in a state known as "floating" - which is an indeterminate state, and could be affected by the environment - causing it to be HIGH randomly - the resistor to ground prevents this).

Now - you have asked "Can't I just leave the resistor off?" - Well, if all you wanted to do was to keep a pin at a LOW state all the time, that would certainly be possible (provided you never told the pin to be an output and drive it HIGH - which would then dump a bunch of current - unlimited by a resistor! - and burn the pin out)...

But we have another possibility here: What happens when you press the switch?

Well - you have 5V coming from the Arduino, going to the switch - which is now closed - and what if that resistor wasn't there?

Well - the current would take the path of least resistance (!) and go straight to ground - effectively shorting your power supply (the source of the 5V) and likely burning out the regulator (or catching the switch or wires on fire, or both - but in the case of the Arduino's voltage regulator, it would likely just cause the Arduino to reset - or the polyfuse to blow).

That resistor is there to prevent this in the situation that the switch is pressed (and the contacts closed) - so that the digital line on Pin 2 of the Arduino will be raised HIGH (some current will also flow to ground - but not much).

Perhaps easier to visualize with a schematic diagram.

pulldown.png

Or, ditch the resistor, wire the switch from Gnd to the pin, No connection to 5V.

Then use the internal pullup resistor to hold the pin high when the switch is not pressed.

pinMode(inputPin, INPUT_PULLUP);

and

if (digitalRead (inputPin) == LOW){
// switch was pressed, do something
}
else{
// switch was not pressed, do something else, or do nothing
}

Thanks a lot~ that clears up so many questions :slight_smile:
two more questions though,
If I understand it correctly, even with resistor, current will still flow to the ground, that means there's small amount of short-circuiting happening.
In arduino's case, how much current flow through the gnd pin without damaging the hardware?

There's no short circuiting.
There is +5 - resistor/input pin - Gnd.
The resistor limits the current flow to 0.1ma to 0.25mA.

Each Gnd pin can accomodate 200mA.

Pull up resistors are very crucial part of microcontrollers. Well, most replies to the post has explained well but otherwise resistors serve the process of limiting current to the LEDs.As you may know the LED's take about 40mA of current to glow.

Most small LED's work fine at less than 20mA. The maximum that an Arduino pin will handle is 40mA.

I concur... always assume a standard red or green unmarked/unknown LED will only need about 10-20 mA for ample brightness. 40mA will exceed the max rating of many LED's.

Some LEDs will tolerate 35-40mA if they are pulsed on, datasheet for the LED will have a note like this:

"Peak Forward Current (1/10th duty cycle, 0.1ms pulse width)"

The problem is heat dissipation. Once overheated, it degrades the LED life and impacts brightness.

Max continuous on current is generally 20mA.
The LEDs with thousands of mcd of output rating will be really bright at just a few mA, like 5!
At 20, break out the shades 8)

I have a some white LEDs that are too bright to look at directly with 3mA through them.
It all depends on what LED you have.

Same here
http://www.mpja.com/5mm-White-LED-10000mcd-Non-Diffused/productinfo/17145%20OP/
Running them around 2-3mA.