In Arduino, we have to define the behavior of the pins with the help of the pinMode() function that either pin should behave as an input or output. We can also define the behavior of the pin as an input_pullup, now the question that arises in mind is what this input_pullup does? The input_pullup adds built-in resistance to the electrical circuit.

In this write-up, the utilization of the input_pullup has been explained with the help of an example.

What is the INPUT_PULLUP in Arduino

In making circuits, we add resistors with the components like LEDs, sensors, we can also use the input_pullup. The input_pullup can add resistance up to 20k ohms and is embedded on the atmega chip (any board of Arduino). The configuration makes the input high when the button switch is open and low when it is closed. The input_pullup works the same as the input reads the input from the sensor or component and adds the resistance to it.

Example: INPUT_PULLUP in Arduino

To understand the working and utilization of the input_pullup, we consider a simple circuit of a LED, push-button, and an Arduino UNO. We will connect one leg of the push-button to the ground of Arduino and the other leg with pin 2 of Arduino. Then will connect a LED with pin 13 of Arduino and the negative terminal of LED to the ground on Arduino.

The circuit diagram of this circuit will be like this:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/PULLUP-in-Arduino-1.png" data-lazy- height="320" src="data:image/svg xml,” width=”320″>

Now consider the following code of Arduino:

int ledpin=13, pbutton=2;


void setup() {


Serial.begin(9600);


pinMode(pbutton, INPUT_PULLUP);


pinMode(ledpin, OUTPUT);

}


void loop() {


int x = digitalRead(pbutton);


Serial.println(x);


  if (x == 1) {


    digitalWrite(ledpin, 1);


  } else {


    digitalWrite(ledpin, 0);


  }

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/PULLUP-in-Arduino-2.png" data-lazy- height="404" src="data:image/svg xml,” width=”758″>

Explanation: Three variables are declared of integer data type; x, ledpin, and pbutton. Pin number 13 is assigned to the ledpin and pin 2 is assigned in pbutton. Then with the help of the pinMode() function, we have defined the behavior of pbutton to take input with some resistance using the input_pullup.

In the loop section of code, we are taking the value of pbutton and saving it in the variable “x” and then applying condition on the value of x, we are setting the state of the LED, for example, if the button is open the output LED should be turned on and if the button is closed, the LED should be turned off.

The input_pullup adds the resistance of 20k ohms when the push button is pressed(hold) so the input becomes zero and when the push button is released, the resistance becomes equal to zero, and the voltage becomes around 5 volts. This output can be observed from the LED as well as the value of x is printed on the serial monitor through serial communication at a baud rate of 9600.


We have also added the animation in which you can see the output on the hardware as well on the serial plotter:

<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/PULLUP-in-Arduino-3.gif" height="362" src="data:image/svg xml,” width=”640″>

The hardware circuit on the breadboard is:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/PULLUP-in-Arduino-4.jpg" data-lazy- height="900" src="data:image/svg xml,” width=”1600″>

The serial plotter output is:

<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/PULLUP-in-Arduino-5.gif" height="508" src="data:image/svg xml,” width=”813″>

When the button is pressed the graph line goes to 0 and when it is released the value goes to 1 and this can be seen from the serial monitor output:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/PULLUP-in-Arduino-6.png" data-lazy- height="275" src="data:image/svg xml,” width=”847″>

Conclusion

The input_pullup is used to define the behavior of the pin as an input and add the resistance of around 20k ohms to the pin. The input_pullup is different from the input function; if the pin is set to input, it will only take the input and if it is set as input_pullup, then it will add the resistance of about 20k ohms to the input and make it high. In this write-up, with the help of an example, we have explained the input_pullup and its works.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/hammad–150×150.jpg61f7a3a430a79.jpg" height="112" src="data:image/svg xml,” width=”112″>

Hammad Zahid

I’m an Engineering graduate and my passion for IT has brought me to Linux. Now here I’m learning and sharing my knowledge with the world.