All of our examples so far show and use an internal pullup.
There are also internal pull down resistors on many microcontroller inputs. In this case you connect your switch's non-digital-pin to Vcc instead of Ground. Vcc might be 5V or 3.3V depending on the 'logic level' of your microcontroller.
But what if your microcontroller does not have an internal pullup or pulldown?
No worries! You can use an external resistor!
Connect one end to the same microcontroller input pin you use for the switch, and the other end to the positive voltage (usually 5 or 3.3 volts). Resistors can be connected either way, they are not polarized so you cannot make the connections "backwards".
The value of the resistor is not usually critical. any resistor between 1,000 ohms (1 KΩ) and 100,000 ohms (100KΩ) should work. If the resistor is too small, you may waste current pushing the button. If too large, the pull up effect may be so weak as to not be detected by the pin electronics.
A value of 10,000 ohms (10KΩ) or so is a good value and has become a standard for pull up resistors. That's why you'll see so many of them.
You can buy resistors nearly everywhere or find them in salvaging old, unused electronics.
Or you can get them at the Adafruit shop
Above, an external 10K ohm resistor is connected between the microcontroller pin used (#2 pin) and Vcc, which is +5 volts for this board (5V pin). The switch (a push button in this vase) has one connection to ground, another to the resistor and the microcontroller pin.
The microcontroller pin, if read in code, should be HIGH
if the switch is not pressed, LOW
if the switch is pressed.
This method can be used for nearly any type of digital circuit looking for a high or low depending on a switch state.
Text editor powered by tinymce.