Pull up/down resistor, what is it?

When looking at tutorials you might have encountered pull-up or pull-down resistors, but why are they needed?

If you look at your arduino micro controller and upload something like this

const int buttonPin = 2;

void setup()
{
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
}

void loop()
{
  Serial.println(digitalRead(buttonPin));
  delay(100);
}

and then takes a look in the serial monitor, you will see something like

1
1
1
0
0
0
1
0
1
0
0
0
1
1

and so on, totally random and might give more 1’s than 0’s if you move your hand closer to it.

This is happening because the pin “floats” and causes it to randomly change.

To prevent this from happening we can use a pull-up or pull-down resistor. And to put one into the circuit you simply put one leg of a 10kOHM resistor into digital pin 2 on the micro controller (because the sketch is using digital pin 2), and the other leg into 5V. You have then put a pull-up resistor on, and serial monitor will give you only 1’s.

Another way of doing this could be to use the internal pull-up resistor.

const int buttonPin = 2;

void setup()
{
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH);
}

void loop()
{
  Serial.println(digitalRead(buttonPin));
  delay(100);
}

This would set the pin as a input, and then pull it high, enabling the internal pull-up. It would do the same as before, and only return 1’s.

If you then would detect a button being pressed, you would connect one leg to pin 2 in the micro controller, and the other to GND. This would pull the pin down, and serial monitor would return 0’s instead of 1’s because it now got 0V instead of the 5V provided through the resistor.

“But isn’t it a shortage through the resistor then?” Yes it is, but because of the 10kOHM resistance through it, it doesn’t really matter, there would run so few mA through it you wouldn’t notice.

One comment

Leave a Reply