User Guide

162
To set GPIO2 as an output and turn it on:
1. Type the following command and then press Enter:
echo out > /sys/class/gpio/gpio2/direction
2. Type the following command and then press Enter:
echo 1 > /sys/class/gpio/gpio2/value
To turn GPIO2 off:
• Type the following command and then press Enter:
echo 0 > /sys/class/gpio/gpio2/value
When you are done, it is a good practice to unexport the pin so
that it is no longer under the control of the file system entries:
• Type the following command and then press Enter:
echo 2 > /sys/class/gpio/unexport
As a final note, outputting 0 actually connects the GPIO pin to
ground inside the Pi. So anything connected to that pin is also
brought to ground.
Controlling GPIO2 from Python 3
From Python, you can control the GPIO pins using the RPi.GPIO
module. This example sets GPIO2 as an output, and then blinks
the LED by repeatedly turning it on and off again. To do this, you
also need to import the time module, which contains a function
for causing a delay.
1. Click the Raspbian Menu button, point to
Programming, and then click Python 3.
2. In the Python Shell, on the Application Menu, click
File, and then click New Window.
3. On the Menu, click File, and then click Save As.
4. Save the file to your desktop, with the extension
.py.
5. At the top of the file, type the following statements: