I'm new to Arduino and also new to electronics. I'm following the tutorial here. I'm not understanding the importance of the resistor in this schematic diagram. Can someone please help me on this?
That resistor, where positioned, is called a pull-down resistor, and if replaced by a straight wire, that circuit would short out when the switch was closed.
The purpose of this is that when a digital input pin is not connected, it floats, and if the switch were open that pin could see upwards of 2v floating value from external and internal noise. With Digital circuitry, any value in this float range can be invalid and return invalid results.
A pull down resistor will generally have a very high ohmic value >5kohm, as to allow as little current pass as possible. Here, you are shorting the pin to ground through the resistor so it will not have a LOW value when the switch is open, instead of possibly invalid.
When the switch is closed, you will have 5v passing through switch into the pin, to be read as a HIGH value, also sending 5v across a 10k or so resistor to ground, consuming only .5mA of current.
It you want to see what I mean by a floating value, debug the output of any analog pin with a wire going nowhere, and watch as the value changes. Try touching the wire, now add a pull down to ground, or try pulling it up to 5v and observe the behavior.
Without the resistor, closing the switch would connect the positive supply directly to the negative supply. This would cause the voltage across all digital components in the circuit to drop close to 0V, crashing the whole thing.
In the above circuit there are several other resistances that are being ignored for simplicity and because most of them are extremely tiny. However, one of the resistances is significant, and that it the internal resistance of the power supply.
Without the resistor shown in the schematic, the internal resistance of the battery becomes the most significant resistance in the circuit and the voltage measure at D2 would remain near zero when the switch is closed and very near zero when the switch is opened.
If the resistor shown were several thousand ohms or greater, the voltage at D2 would measure close to 5 volts when the switch is closed and zero when the switch is open (however D2 will be subject to picking up any nearby static electricity or strong electromagnetic fields when the switch is open).
To prevent D2 from being subject to spurious noise, resistors are often used between signal points and ground to dampen any such undesired noise. To better imagine how this works, imagine a 100 megohm resistor connected to D2 with the other end hanging out into the air. The unconnected end of the large resistor is acting like an antenna picking up any possible noise in the vicinity. Because the resistor shown in the diagram is so much smaller then the imaginary resistor, the noise will seem to be shorted out to ground in comparison.
In a very real sense, this schematic is telling you to add in a known resistor to ground or suffer from the effects of all the resistors not show; some being very very high resistance and some being nearly non-existent.
Check out the internal pull-up resistor (disconnected by default) for Arduino as well. Save yourself wiring up an extra resistor.
pinMode(5, INPUT_PULLUP); // Enable internal pull-up resistor on pin 5
From SparkFun: Pull-up Resistors