2

I'm a beginner in Arduino and can't understand some things. How does input pin in Arduino work? I want to set one of the pins as input. When should I use input and when input_pullup? What are the differences between them?

Maciaz99
  • 41
  • 1
  • 3
  • Kinda late comment, but I'm surprised that more people haven't asked this question before, similar to how MANY people have asked about millis... – Coder9390 Dec 17 '21 at 01:08

2 Answers2

4

Input pin working-

An input pin on an arduino board uses something called Tristate buffer to expect a voltage at the pin.

Input pullup-

When a pin is configured just as an input (without a definite voltage connected to it), the pin will return random values based on the electrical interference present around it, for example the neighbouring pin.

So to solve this issue, input pullups are used. Essentially it sets the default state of the pin to HIGH or 1, so that the pin is not affected by interference.

You can experience this yourself using the following program, with a pushbutton from Ground to pin 7, and the Serial monitor-

No pullup-

void setup(){
// Dont have to put any pin definitions here as by default it input
// This code will return random values until the button is pressed
Serial.begin(9600);
}

void loop(){ Serial.println(digitalRead(7)); }

Now for the one with Pullup-

void setup(){
pinMode(7, INPUT_PULLUP);
Serial.begin(9600);
// This code will return 1/HIGH until the button is pressed
}

void loop(){ Serial.println(digitalRead(7)); }

Random info- There's input pulldown on some boards, guess what that does?

John Burger
  • 1,875
  • 1
  • 12
  • 23
Coder9390
  • 472
  • 6
  • 22
1

The pin inputs are sensitive enough to nearby electrical noise that if the pin is floating - not positively pulled up to the supply voltage, or down to ground - the input will read randomly HIGH or LOW. A pullup resistor of 10K Ohms or so connected between the pin and the supply voltage is strong enough to overcome the noise, but is itself easily overcome by a signal applied to at the pin input:

schematic

simulate this circuit – Schematic created using CircuitLab

This means that when the pin is not connected, it will have stable HIGH reading. While you can put the pullup on your circuit board, as in the schematic above, Arduinos have a built-in pullup resistor that can be connected to the pin simply by selecting 'INPUT_PULLUP' in your code, and it does exactly the same thing. It is a convenient way to save space and a component on your circuit board or make it slightly quicker to wire your breadboard. And in the case of a breadboard in particular, the plug-wires are excellent antennas for picking up noise, while the built-in pullup resistor is within the chip, close the pin-receiver, and it makes the circuit dramatically more stable.

JRobert
  • 15,246
  • 3
  • 23
  • 51