1. Principle of Pull-Up Resistors
The definition of a pull-up resistor is as follows: Connect a resistor between the signal line and the power supply (VCC), and the signal line is pulled up to a high level by default through the resistor. When there is no other drive, the signal line remains at a high level; when the device actively pulls it down, the current flows to GND through the resistor, and the signal becomes a low level.
The definition of the pull-up resistor is very clear. At first glance, it seems that we know its function, but we cannot deeply understand from the definition why it can output a high level by default when there is no other drive, and output a low level when there is a device drive. Next, I will explain the principle from my understanding perspective.
Let's look at this diagram; this is a basic frame diagram of a pull-up resistor. When we connect a pull-up resistor, the resistance value of this resistor is generally between 1kΩ ~ 100kΩ, which is a very large resistor. When the external device (referring to the switch in the diagram here) has no drive, the line leading to GND is disconnected. That is to say, we only need to look at the line from VCC to the blue point and then to the output (it is not a closed loop). Since the resistance value of this pull-up resistor is very large, the current in this line is particularly small (only microampere-level leakage current, which is close to 0). Therefore, the voltage division on this pull-up resistor, that is, its voltage, is infinitely close to 0 and can be ignored (U=IxR≈0). So the potential at VCC is equal to the potential at the blue point, which is also the potential at the output, so the output is a high level. This explains why it outputs a high level by default when the device has no drive.
Next, let's see why it outputs a low level when the device is driving. When the device is driving (that is, when the switch in the diagram is closed), a closed loop is formed at this time. Generally speaking, this output line is connected to other circuit parts, so the current starts to shunt from VCC to the blue point: part of the current flows to the output path, and part flows to GND. According to circuit knowledge, the potential difference between VCC and the blue point is equal to VCC, which means the potential of the blue point is 0V, that is, a low level. This explains why it outputs a low level when the device is driving (switch closed).
2. Principle of Pull-Down Resistors
The definition of a pull-down resistor is as follows: Connect a resistor between the signal line and the ground (GND), and the signal line is pulled down to a low level by default through the resistor. When there is no other drive, the signal line remains at a low level; when the device actively pulls it up, the current flows to VCC through the resistor, and the signal becomes a high level.
Similarly, we can only know the function of the pull-down resistor from this definition, but not how the principle comes about. Next, let's look at the following basic frame of the pull-down resistor (understand it in combination with the diagram):
Like the pull-up resistor, the resistance value of the pull-down resistor is generally between 1kΩ ~ 100kΩ, which is a very large resistor. When the external device (referring to the switch in the diagram here) has no drive, just like the pull-up resistor, the current in this line leading to the output is very small (only microampere-level leakage current, which is close to 0) (here you can also think that there is no positive pole of the power supply at all, so the current is directly 0; it is also acceptable to understand that there is current because there is some current inside the chip). Therefore, the voltage division of this pull-down resistor, that is, its voltage, is infinitely close to 0 and can be ignored (U=IxR≈0). So the potential at GND is equal to the potential at the blue point, which is also the potential at the output, so the output is a low level. This explains why it outputs a low level by default when the device has no drive.
Next, let's see why it outputs a high level when the device is driving. When the device is driving (that is, when the switch in the diagram is closed), a closed loop is formed at this time. Generally speaking, this output line is connected to other circuit parts, so the current starts to shunt from VCC to the blue point: part of the current flows to the output path, and part flows to GND. According to circuit knowledge, the potential of VCC is the same as that of the blue point (or the voltage of this pull-down resistor is VCC), so this output is naturally a high level. This explains why it outputs a high level when the device is driving.
3. Analysis Case of Pull-Up Resistors
Based on the pull-up resistor principle I just talked about, let's take a rotary encoder as an example (only look at the pull-up resistor R1 on the left; the right side is the same) to explain the level change at point A: when the red part is disconnected, it is a high level; when it is closed, it is a low level.
1. When the red part is disconnected: At this time, there will be no shunting at the red point below R1 (because the red part is disconnected, so that side is an open circuit). The current will flow from VCC through R1, then through R3, and finally flow out to point A. Since the resistance value of the pull-up resistor is very large (and this cannot be regarded as a closed loop), the current is almost 0. That is to say, according to the formula U=IR, the voltage divided by R1 and R3 is almost 0. So the potential at VCC is approximately equal to the potential at point A, which is a high level.
2. When the red part is connected (that is, closed): A closed loop is formed. The current directly flows from VCC to R1, then to the GND of the circuit at point C, and does not flow to R3 (it can be simply understood that R3 is short-circuited). Then the potential at point A is equal to the potential at the red point below R1, and since the potential at the red point below R1 is equal to the potential at point C (which is equal to GND), the potential at point A is 0, that is, a low level. Through this way of understanding, we can clearly know why the pull-up resistor has such a function.