Have you ever come across the term pull-up or pull-down resistor and didn’t understand what it meant? They are often used, for instance, in the microcontrollers area. In this tutorial, we will understand what these terms mean and how to activate them using two methods: registers and high-level Arduino functions.
Pull-up and Pull-down resistor
If you have ever worked with microprocessors, you know perfectly well that they have digital input and output ports. From these gates, we can transform a voltage (which in the case of the Arduino varies between 0 and 5 volts) into a LOW or HIGH logical value, when the voltage value is below or above a certain threshold, respectively. These ports make it possible to get information to the processor from the exterior environment, and vise-versa.
You can create a pull-up circuit by connecting VCC to the digital port. Consequently, this will force the port to be in its HIGH logic state. When you connect GND to the gate by pressing the button, the electric current travels the path of least resistance, causing the port to be in its LOW logic state. Figure 1 presents a Pull-up resistor schematic where this will happen.

In contrast with the pull-up resistor, in the pull-down schematic, the port is directly connected to the GND, using a resistor. In this way, the port is set to the LOW logic state. Then, when the VCC is connected to the gate, pressing the button, the electric current travels the path of least resistance, and thus the logic level of the port is set to HIGH. Figure 2 presents the pull-down schematic.

Note that when you are reading a falling edge with your Arduino or ESP board, you will get the fastest input by using a pull-up resistor. However, if you are reading a rising edge, the quickest input is provided by a pull-down resistor.
In a final analysis, the application of the pull-up and pull-down resistors guarantees no unwanted variations, in the logic level of the gates, due to some type of electromagnetic interference. You can see a post of ours in which pull-up resistors were applied at the following link.
Code
The Arduino boards only have internally available pull-up resistors, thus, if you want to do a pull-down resistor, you will need to do it externally. The code below shows you how to activate the internal pull-up using registers (more complex, see [1] and [2]) and high-level Arduino functions (less complex).
int value=0;
void setup() {
// DDxn bit in DDRx register controls the direction - input(0)/output(1) pinMode()
DDRG = 0x00;
// PORTxn bit in PORTx register controls the output (high/low digitalWrite())
PORTG = 0b00010000;
// PUD in MCUCR disables all pull-up resistors if it is set to 1
// You don't need to care about MCUCR, unless you want to disable pull-up on all pins.
Serial.begin(9600);
}
void loop() {
value=PING & 0b00010000; //reads port value
value = value >>4;
Serial.println(value); // Writes to serial 0(LOW) or 1(HIGH)
}
Below is presented a high-level code approach. This code shows how to turn port 4, of an Arduino mega, into an input with a pull-up resistor. With this code, we will read in the serial the value 0 (LOW) when the input receives the GND voltage and 1 (HIGH) when the gate gets the VCC value.
void setup() {
pinMode(4, INPUT_PULLUP); // Defines port4 as input with pullup
Serial.begin(9600);
}
void loop() {
Serial.println(digitalRead(4)); // Writes to serial 0(LOW) or 1(HIGH)
}
References
[2] https://wiki.wpi.edu/robotics/Port_manipulation_and_digitalWrite()