Hardware Reference
In-Depth Information
3.
We will import the
sleep
class from the
time
module in the first line. This is
required to introduce a 1-second delay between turning the LED on and off every
other second:
from time import sleep
4. We also need the pin class from the
quick2wire
GPIO library:
from quick2wire.gpio import Pin
5.
We need to set the output pin that we will be using in the example:
LED_output = Pin(8, Pin.Out)
6. We can set the pin to the logical high (3.3 V) as follows:
LED_output=1
7. We will set the pin to the logical low (0 V) as follows:
LED_output=0
8. We will execute the same thing using an infinite
while
loop:
while True:
LED_output=1
sleep(1)
LED_output=0
sleep(1)
9. This will make the LED blink with a 1-second delay. We should also note the indent
on the blink sequence. The blink sequence has a different indent compared to the
while
loop. Hence, the code that is at a different indent is executed infinitely.
10. When the program is interrupted (by pressing
CTRL
+
C
on the keyboard), we need
to unexport the pins at exit:
out_pin.unexport()
An alternative to quick2wire - RPi.GPIO
1. Another alternaive is to use RPi.GPIO (
https://pypi.python.org/pypi/RPi.
perform a quick review of the code:
import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setup(8,GPIO.OUT)