Hardware Reference
In-Depth Information
function
printStatus
(
x
) {
console
.
log
(
'x.value = '
+
x
.
value
);
console
.
log
(
'x.err = '
+
x
.
err
);
}
What do you get if you read a GPIO port that has nothing attached to it:
1
or
0
? That de-
pends on many things. It's best not to count on either 1 or 0. A standard approach is to tie
the port to either ground or 3.3 V through a 1 kΩ (or so) resistor. If nothing else is at-
tached, the resistor will pull the input to either 0 V (ground) or 3.3 V. This is called a
pull-
up
or
pull-down
resistor, depending on the power supply to which it's attached.
You can then use a switch to connect the port to the opposite value. In the previous sec-
tion, the Bone was configured to use an internal pull-down resistor. When the button isn't
pushed, the GPIO read
0
because the pull-down resistor held it at 0 V. When the switch is
pushed, the GPIO port is attached to the 3.3 V source and a value of
1
is read.
Note that our examples set the fourth argument of
pinMode()
to be
pulldown
, to use
a resistor internal to the Bone to pull the pin down to ground when the switch is open. It is
also possible to configure for a
pullup
resistor that will pull the pin up to 3.3 V when
the switch is open. Notice that the top pushbutton in
Figure 2-6
is wired to ground and the
bottom is wired to 3.3 V.
Figure 2-6. Pushbutton with Bone configured for pull-up resistor
Therefore, if the top button isn't pushed, a value of 1 will be read. When the top button is
pushed, the GPIO port is attached to ground and a
0
is read. GPIO port
P9_42
works as
in
Example 2-3
.