Post comments on this article and find any associated files and/or downloads at
■ FIGURE 4.
■ FIGURE 5. Schematic
for Experiment 3.
from ground to + 3.3V.
The software is also easy to
modify. Only two changes are
needed: Replace the GPIO.setup
command with the correct syntax for
a pull-down resistor, and replace the
As I mentioned earlier, GPIO
pins 2 and 3 function somewhat
differently when used as input pins
due to the external I2C 1.8K pullup
resistors that are soldered in place on
the Pi’s PCB. As a result, if you
enable either an internal pull-up or
pull-down resistor, you end up with
three resistors in the circuit, with two
of them in parallel (see Figure 5). I
used the GPIO 2 pin to test both
circuits (but the GPIO 3 pin functions
identically) and here’s what I found:
The pull-up version (left side of
Figure 5) works, but it also works
without enabling the pull-up resistor,
so there’s no need to use the internal
0 in the if statement with a 1:
if GPIO.input(switch) == 1:
To me, this makes more sense;
if the input is raised to high (i.e., the
switch is pressed), the output
becomes high (i.e., the LED is lit).
The pull-down version (right side
of Figure 5) doesn’t work at all.
Whether the switch is pressed or not,
the external 1.8K resistor pulls the
voltage high enough so that the input
is always in a high state.
So, modify your breadboard
circuit, download the
switchPUD_DOWN.py program to
your Pi, and run it. The program
functions identically to the earlier
switchPUD_UP.py program, but I’ll
sleep a little better because the
program is more logically consistent.
Experiment 3: What
about the GPIO 2
and 3 pins?
GPIO pin and still need an input, I’ll
use the GPIO 2 or 3 pin without
enabling either internal resistor. In
other words, I’ll use the onboard
1.8K I2C resistor as a pull-up. If you
want to experiment with this option,
Figure 6 shows a photo of the
breadboard circuit I used for a switch
on the GPIO 2 pin, and a suitable
Python program ( switchGPIO2.py) is
included in this month’s downloads.
Again, don’t forget to insert a 470Ω
resistor in the four-pin female header
as shown in the photo.
If you want to experiment with
either of the circuits in Figure 5,
don’t forget that a 470Ω resistor
needs to be installed in the four-pin
female header that’s in line with the
pin that you choose (GPIO 2 or 3). If
you leave that resistor out, there will
be no electrical connection between
the GPIO pin and your breadboard,
so switch presses will have absolutely
no effect on the input pin.
Now that we have a basic
understanding of how to use the
internal pull-up/down resistors on the
Pi’s GPIO pins, we can move on to
implementing our Cylon Eye project.
What's a Cylon Eye,
Of course, you’re free to draw
your own conclusions, but
here’s what I’ve decided after
experimenting with the Pi’s input
1. Pull-down resistors are
preferable because their use results in
more logical programming. Therefore,
I’ll avoid using GPIO pins 2 and 3 for
digital inputs whenever I can.
2. If I’ve already used every other
If you’re a science fiction fan
(and old enough!), you probably
already know the answer to that
question; if not, here’s a brief
explanation: The Cylon Eye was first
seen in the 1978 television series
Battlestar Galactica. This science
fiction show included a race of
robots called Cylons who had a
single, sinister red “eye” that
repetitively scanned the environment.
The original show only lasted one
season, but ever since the majority of
introductions to microcontroller
programming have included some
form of an LED-based Cylon Eye
project — probably because it’s so
14 December 2013