Often we are interested in allowing one to use a button to establish a voltage that serves as input signal to a digital device such as a chipKIT™ board. One might think that the button could be used either to attach a voltage source to a chipKIT input pin or to disconnect a voltage source from the pin and that would be the end of the story. However, implementing a proper button-sensing circuit actually requires a bit more effort than that. To ensure that the voltage properly varies between known states, together with the button, we need to use either a pull-down or a pull-up resistor as described here. In the circuits we show here, the resistor will simply be labeled as having an unspecified resistance R. In practice, pull-down and pull-up resistors will typically have a resistance in the range of 10 kΩ to 40 kΩ. The precise value is not that important but we do want them to be relatively large so that only small amounts of current flow through them (i.e., and thus they consume only a small amount of power).
Before we begin looking at our specific button circuit, we must understand how to interpret what we'll see. Figure 1 shows a button circuit with a pull-down resistor when the button is open (so no current can flow). The schematic symbol for a resistor is the jagged collection of lines labeled “R,” which is drawn below the button. The top of the circuit is connected to a 3.3V source. The bottom of the circuit is connected to ground (0V). In circuit diagrams (schematics), as shown in Fig. 1, ground is often represented as three short horizontal lines of decreasing length (another common symbol for ground is an isosceles triangle with one of the tips pointing down). A voltmeter, which allows us to read voltages at different points in the circuit, is also depicted in Fig. 1. The voltmeter tells us the voltage between the two points to which its two “probes” are attached. In this figure, one of these probes is always attached to ground while the other probe is moved to three different points. The value that the voltmeter is reading is displayed in the voltmeter “window.” In the circuits' schematics in Fig. 1, the source of the voltage is not explicitly drawn. Nevertheless, it is understood that there is a voltage source present that provides a path for current to flow from ground (at the bottom of the circuit) to the point labeled 3.3V (at the top of the circuit) and that this source establishes the indicated voltage (i.e., 3.3V).
Figure 1. Button circuit with a pull-down resistor and the button open. (a.) Voltmeter attached to 3.3V supply. (b.) Voltmeter attached to ground. (c.) Voltmeter attached above the resistor.
Before giving further consideration to Fig. 1, we want to mention that a circuit (or a portion of a circuit) that provides no path for current to flow is known as an open circuit. When two points in a circuit are connected, with no resistance between them, these points are said to be shorted together. We sometimes simply say that there is a short circuit between these two points. What we mean by a short circuit and an open circuit is further explained in the links available via the boxes to the right.
We will assume that push-to-close button is used in the circuit. For this type of button, the button must be pressed to create a path for current to flow. In other words, we need to press the button to “close” the circuit (or to create a short circuit between two points).
Returning to Fig. 1, in Fig. 1(a), one of the probes is attached to the 3.3V source and thus the meter reads 3.3V, in spite of the fact that the button is open (and no current is flowing). If we move the probe so that it is attached to ground, as shown in (b), the voltmeter reads 0V. This is because both probes are attached to ground and thus there is no voltage difference between these probes. In (c), the probe is moved to the point above the resister but below the open button. The voltmeter again reads 0V. This is because there is nothing to supply a voltage at this point; there is nothing to establish a flow of current through the resistor.
Figure 2 shows what happens when the button is pressed, i.e., the circuit is closed, and the probe remains in the same location as shown in Fig. 1(c). There is now a direct path from the 3.3V power supply to the probe. We see that when the button is open (Fig. 1(c)), the resistor serves to pull the voltage down to 0V at this probe location, but if the button is closed (Fig. 2), the probe is attached to the 3.3V supply.
Now let's consider a circuit where we switch the location of the button and the resistor so that the resistor is directly tied to the 3.3V supply and the button is directly connected to ground, as shown in Fig. 3. In this figure, the voltmeter is still measuring the voltage between ground and the point that is between the button and the resistor. In Fig. 3(a), the button is open, and no current can flow to ground, and yet the voltmeter measures 3.3V. This is because if there is any difference in potential across the resistor, charge flows through the resistor. However, once on the other side, there is no place for the charge to go. Hence, the charge quickly builds on the other side of the resistor to the level where there is no difference in potential across the resistor. Thus, current flow stops.
In Fig. 3(b) the button is closed, providing a direct connection between the probe and ground. In this case, the probe measures 0V.
Figure 3. A circuit with button and pull-up resistor (a.) when the button is open and (b.) when the button is closed/pressed.
In the circuit in Figs. 1 and 2, the resistor serves to pull down the voltage (of the probe) to 0V when the button is open. On the other hand, in the circuit in Fig. 3, the resistor serves to pull up the voltage (of the probe) to 3.3V when the button is open. Thus, resistors used in this way are known as pull-down or pull-up resistors.
To use either of these circuit with a chipKIT board, the point between the button and the resistor would be connected to an input pin. Thus, if one uses a pull-down resistor, as in Figs. 1 and 2, the input pin will “see” 0V (logical LOW if the button is not pressed and 3.3V (logical HIGH) if it is pressed. Conversely, if one uses a pull-down resistor, the input pin will “see” 3.3V (LOW) if the button is not pressed and 3.3V (HIGH) if it is pressed.