Please tell me why is it necessary to access pull-up resistors and how can we access them in arduino?


Pull-up resistors are used to define the state of an otherwise "floating" input by pulling the voltage at the pin to a set voltage.
The most typical example is a switch - let's say you have some signal, which is connected to your input through a switch. If the switch is open, the state of your input is "floating". If you try to digitalRead or analogRead that input, the value will be pretty much just noise.
But, if you have a resistor connected from your input to ground (pull-down) or VCC (pull-up) your reading will be consistent (HIGH or LOW, depending on where the resistor goes) while the switch is open.
When the switch is closed, the voltage at the pin will be pulled to the voltage at the other end of the switch. Because of the lower resistance of the switch relative to your pull-up resistor, this is the input level that will be read by the microcontroller. Pull-up resistors are usually pretty big value (5k-100k) in order to be much higher resistance than the switch and to decrease the current that is used by the switch to produce a signal.
If, for example, you have a pull-up resistor and a switch that connects your input to the ground, when the switch is open you will read HIGH and when it is closed you will read low. Note that many components act similar to switches, for example transistors, open-drain / open-collector devices and many more, so the switch was just an example.

You can use external pull resistors (just add a resistor from your pin to ground or VCC) or use the internal pull resistors of your MCU. The Arduino only has internal pull-up resistors (no internal pull-downs) - their value depends on the model, but usually is between 20K-150K.
To activate the internal pull-up resistor for a given pin on Arduino, use the following code:

pinMode(N, INPUT);
digitalWrite(N, HIGH):

where "N" is your pin number or with recent IDEs:

  • 1
    ok, +1, also change intenral to internal Jun 16 '16 at 20:38
  • 1
    You can also activate them with: pinMode (N, INPUT_PULLUP); which is the preferred method these days. I think in the case of some other architectures (like ARM) that function call generates the correct code whereas doing a digitalWrite like you showed might not.
    – Nick Gammon
    Jun 16 '16 at 21:47

A short answer from Arduino Playground:

Pull-up resistors are used in electronic logic circuits to ensure that inputs to the arduino settle at expected logic levels if external devices are disconnected or high-impedance. 'Just because you have nothing at all connected to an input pin doesn't mean it is a logical zero.'

Not the answer you're looking for? Browse other questions tagged or ask your own question.