When using a microcontroller, when the pin we use as an input is not set to "HIGH" or "LOW" state, but its state "floats" between these two values, we say that it is in a state of high impedance. This is a condition that we definitely want to avoid because it leads to irregularities (unpredictability) in work and that is why we introduce a concept like"pull-up"and"pull-down"resistors. These are not special types of resistors, but resistors that allow us to maintain the desired state of the logic circuit regardless of the conditions. Arduino pin is connected via this resistor to a constant value, either to the + 5V power supply or to the GND (ground). In the first case it is a "pull-up", while in the second we are talking about a "pull-down" resistor.
The "pull-up" resistor in the previous figure is shown on the left. What it allows is to maintain the digital input (pin) at a high voltage level (hereinafter referred to as "HIGH"). Vcc is a 5V power supply, which corresponds to the "HIGH" state, while Vout is the terminal we connect to the Arduino pin configured as input. So, until we press the S1 key, the Arduino pin will be in the "HIGH" state, when the Arduino pin key is pressed, it is connected to ground and set to a low voltage level (hereinafter referred to as "LOW"). Due to this feature, maintaining the "HIGH" state on the Arduino pin resistor R1 is called a "pull-up" resistor.
The "pull-down" resistor is shown on the right in the previous figure. Contrary to the previous way of connecting the resistor to the input pin of the Arduino board, it allows us to maintain the state of the input to the "LOW" value. This is possible because the Arduino pin is directly connected to ground (zero potential point) via resistor R1. The moment the S1 key is pressed, the Arduino pin connects to the power supply Vcc and brings it to the "HIGH" state. Due to this feature, maintaining the "LOW" state on the Arduino pin of resistor R1 is called "pull-down" resistor.
The sketch and expanation
"Pull up" sketch
The push button is connected to pin 2 which is configured to operate in "INPUT_PULLUP" mode. A serial connection speed of 9600 baud has also been defined. The variable "buttonState" saves the state of the push button - it will store information about whether the key was pressed or not. As said, with a "pull up" resistor, the pin is tied to the "HIGH" level, when the key is pressed it is tied to ground, ie set to "LOW". When the button is pressed the buttonState is set to the value "0". The statement in the "if" structure will then be true and the message "Button pushed!" Will be printed.
"Pull down" sketch
The key is connected to pin 2 which is configured to operate in "INPUT_PULLUP" mode. A serial connection speed of 9600 baud has also been defined. The variable "buttonState" saves the state of the push button - it will store information about whether the button was pressed or not. As said, with the "pull down" resistor, the pin is tied to the "LOW" level, when the button is pressed it is connected to the power source, ie set to "HIGH". When the button is pressed the buttonState is set to the value "1". The statement in the "if" structure will then be true and the message "Button pushed!" Will be printed.
This video is available in english language too.
The push button allows the user to flow current from one part of the circuit to another. It works exactly the same as a suspension bridge - when the bridge is raised, it is not possible to cross from one end to the other, when it is lowered, the crossing is possible. Also, the current cannot pass from one side of the circuit to the other until the moment the button is pressed.
The scheme is given in the following figure: