User Guide
Table Of Contents
166
Building the Circuit
The previous circuit has a problem. If the GPIO pin (the middle
line on the diagram) is accidently set to a high output (either by
the user or by the Pi when it is starting-up) then pressing the
switch causes a short circuit and can damage the Pi.
Build the circuit in Figure 7. It relies on the Pi’s internal pull-up
resistor but also adds a 330 Ω resistor to protect the Pi if the
GPIO pin is changed to an output.
If the GPIO is a high output then the electricity flows through the
330 Ω resistor and this is enough to protect the Pi. The resistor
must be a high-enough value to protect the Pi but low enough
so that electricity prefers to flow to ground when you press the
switch, rather than into the GPIO pin.
Figure 7. Basic input – schematic (left), and breadboard (right)
With the GPIO pin configured as an input and the Pi’s internal
pull-up resistor enabled, if the switch is open then the pull-up
brings the GPIO pin high. If the switch is pressed, the connection
to ground drains the electricity from the pin and this causes it to
go low.
Reading a GPIO Pin from a Terminal Window
To read the state of GPIO2 from the terminal or shell, you need
to export the pin to the file system.