Now take the end that was in +5V and put it into one of the GND connections on the
Arduino. As you would expect, the Serial Monitor should now display zeros.
A typical use for an input pin is to connect a switch to it.
Figure 6-6
shows how you
might be expecting to connect your switch.
The problem with this is that
if the switch is not closed,
then the input pin is not
connected to anything. It is said to be floating, and could easily give you a false reading.
You need your input to be more predictable, and the way to do this is with what is called a
pull-up resistor.
Figure 6-7
shows the standard use of a pull-up resistor.
It has the effect
that
if the switch is open, then the resistor pulls up the floating input to 5V. When you
press the
switch and close the contact, the switch overrides
the effect of the resistor,
forcing the input to 0V. One side-effect of this is, while the switch is closed, 5V will be
across the resistor, causing a current to flow. So, the value of the resistor is selected to be
low enough to make it immune from any electrical interference, but at the same time high
enough to prevent excessive current drain when the switch is closed.
Dostları ilə paylaş: