A digital input left unconnected is not at zero and not at one; it is an antenna with an identity crisis, drifting with nearby signals, static and the weather inside the chip. Connect a pushbutton between an input pin and the supply, and while the button is open the pin floats, reading random values and sometimes oscillating fast enough to waste power and confuse software. The cure is a resistor that weakly holds the pin in its resting state: to the supply for a pull-up, to ground for a pull-down, letting the switch overwhelm it decisively when pressed.
The value is a compromise measured in microamps and microseconds. Too high, say a megohm, and the hold is feeble: leakage currents and interference can still bully the pin, and in fast circuits the resistor with the pin's capacitance forms an RC filter that slows edges. Too low, say 100 ohms, and the pressed switch wastes real current and the resistor needs a power rating. The everyday answer is 4.7 to 10 kilohms for logic inputs, lower for long cable runs and electrically noisy places, higher for battery designs where every microamp of standing current counts against the cell.
Pull-ups are built into many chips as weak internal resistors you enable in software, adequate for short board-level runs. The I2C bus is the famous special case: both of its lines are open-drain, driven low by devices and returned high only by pull-up resistors, so the resistor value sets the bus speed limit against the cable capacitance, a genuine design calculation on longer runs. Keypads, encoder outputs, reset lines and configuration straps all lean on the same humble idea: never let a node decide for itself. When a prototype misbehaves randomly, a floating input is among the first suspects, and a resistor is among the cheapest exorcisms. The same idea scales up beyond logic pins. Power switches and gate drivers use pull-downs to guarantee a MOSFET stays off while its controller boots, because a floating gate at power-up can half-turn-on a device and cook it in seconds. Relay and lamp drivers use them to define the dark state. Even the humble doorbell circuit of a microcontroller board, its reset line, is a pull-up holding the device awake until a button or a supervisor chip says otherwise. Whenever a schematic leaves a node connected to exactly one thing that sometimes lets go, ask what holds that node the rest of the time. If the answer is nothing, you have found either a bug or the place your resistor belongs.
The free calculator that goes with this guide:
Open the voltage divider calculator