The most common mistake when connecting a switch to a digital input pin is to wire one side of the switch to the input pin and the other to ground and stop there. When the switch is pressed the pin goes Low, but when it is not pressed the input pin is connected to nothing, in a "floating" state, and swings between High and Low and behaves erratically. Pull-up and pull-down resistors solve this problem. This article explains the principle of pull-ups and pull-downs, how to choose the resistance, the internal connections of a 4-pin tact switch, and contact bounce and how to deal with it.
What happens when an input pin floats
A CMOS input pin has a very high impedance, so its voltage changes with just surrounding noise (near a finger, signals on adjacent wiring). If nothing is connected, the output changes randomly, or it sits at an in-between voltage and the IC draws needless current. An input pin must always be firmly connected to either High or Low.
Pull-up and pull-down
- Pull-up resistor: connects the input pin to +V through a resistor. The switch goes between the input pin and GND. When not pressed it is High, and when pressed it is pulled to GND and becomes Low.
- Pull-down resistor: connects the input pin to GND through a resistor. The switch goes between the input pin and +V. When not pressed it is Low, and when pressed it becomes High.
In digital circuits, the pull-up approach is more widely used. A switch to ground is easy to wire, and many microcontrollers provide internal pull-up resistors.
Choosing the resistance
| Value | Characteristics |
|---|---|
| 1 kΩ – 4.7 kΩ | Fast and robust against noise, but the current when pressed is large (1–5 mA at 5 V) |
| 10 kΩ | The most common choice. 0.5 mA when pressed |
| 47 kΩ – 100 kΩ | Low current draw, suited to battery circuits, but weak against noise |
When the switch is pressed, a current of supply voltage ÷ resistance flows through the resistor (0.5 mA for 5 V and 10 kΩ). It has to be well above the leakage current of the input pin, so avoid resistors that are too large (several MΩ). On lines whose signal changes quickly, like I2C, the pull-up value affects the speed (I2C Wiring Basics).
Internal connections of a 4-pin tact switch
A small push switch (tact switch) has four pins but only one contact. The two pins on one side of the body are always connected inside, and only when you press the button do they connect to the opposite pair.
- If you use two pins on a diagonal (for example, pin 1 and pin 4), it is always the right contact even if you mix up the orientation.
- If you use only two pins on the same side, they are always connected even when not pressed, so the switch looks broken.
- If you are unsure of the orientation, set the multimeter to continuity and check pairs of pins. The pair that beeps when not pressed is the same side.
The tact switch in Patterndo (small 3×4 holes, large 4×4 holes) is also modeled like the real part, with the two longer-side pins connected together and the shorter side as the pressed contact.
Contact bounce
A mechanical switch's contacts make and break several times for a few ms when they close. To the human eye it is one press, but a fast digital circuit reads it as several presses. This is why, when connected to a counter, the number goes up several times with one press.
There are three fixes.
- Software: ignore the input for 10 to 20 ms after the value changes. This is the easiest on a microcontroller.
- RC filter + Schmitt trigger: put a resistor and capacitor on the switch pin (for example, 10 kΩ and 100 nF → a 1 ms time constant) and feed it into a Schmitt trigger input (such as a 74HC14), and the jitter is filtered (Understanding RC Time Constants and Capacitor Charging).
- SR latch: connect a switch with two contacts (SPDT) to a latch made from two NAND gates, and only the first contact is reflected.
DIP switches and slide switches
A DIP switch gathers several small switches in one body and is used to enter values you set once (addresses, modes). The arrangement is the same as with a tact switch: one side of each switch goes to the pull-up (or pull-down) resistor and the input pin, and the other to GND (or +V). The 4-channel DIP switch in Patterndo can be clicked to toggle ON and OFF, so you can check input changes in simulation. An example using it is in A Seven-Segment Display Selected with a DIP Switch.
Summary
A digital input must always be given a firm level with a pull-up or pull-down, and the value is usually 10 kΩ. For a tact switch, using the diagonal pins is safest, and jitter from a button is handled by software or an RC filter. Next, see how to handle unused inputs in 74LS and 74HC Logic Gate Basics.