GPIO programming using Python
In this section, we will ensure that the library is correctly installed and add the user to the group. This will enable the user to use the GPIO pins without having root privileges. This will be followed by the section on getting started with GPIO control programming in Python.
Engage thrusters
In order to get started with programming in the Raspberry Pi, we will launch Python IDLE3 from the desktop.
- Now, we have to get started with programming the LED blinking example in IDLE3.
- This LED blinking sample code is as follows:
from time import sleep from quick2wire.gpio import pins, Out with pins.pin(7, direction=Out) as out_pin: while True: out_pin.value = 1 sleep(1) out_pin.value = 0 sleep(1) out_pin.unexport()
- We will import the
sleep
class from thetime
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
- We also need the pin class from the
quick2wire
GPIO library:from quick2wire.gpio import Pin
- We need to set the output pin that we will be using in the example:
LED_output = Pin(8, Pin.Out)
- We can set the pin to the logical high (3.3 V) as follows:
LED_output=1
- We will set the pin to the logical low (0 V) as follows:
LED_output=0
- We will execute the same thing using an infinite
while
loop:while True: LED_output=1 sleep(1) LED_output=0 sleep(1)
- 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. - 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
- Another alternative is to use RPi.GPIO (https://pypi.python.org/pypi/RPi.GPIO). It comes as a standard package along with the Raspbian Wheezy OS. Let's 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) GPIO.output(8,GPIO.LOW) while True: GPIO.output(8,GPIO.HIGH) sleep(1) GPIO.output(8,GPIO.LOW) sleep(1) GPIO.cleanup()
- After importing the required modules, we get started with setting up the pin numbering mode. There are two types of pin numbering modes, namely:
- The BCM Pin numbering mode: The pin numbers are based upon the pin numbers of the BCM chip.
- The Board numbering mode: The pin numbers are based upon the pin numbers of the Raspberry Pi GPIO header.
- In this example, we will set the BCM numbering mode and set pin 8 as the output:
GPIO.setmode(GPIO.BCM) GPIO.setup(8,GPIO.OUT)
- We can set the pin to logical high (3.3 V) as follows:
GPIO.output(8,GPIO.HIGH)
- We can set the pin to logical low (3.3 V) as follows:
GPIO.output(8,GPIO.LOW)
- Now, the LED can be made to blink with a 1 second delay:
while True: GPIO.output(8,GPIO.HIGH) sleep(1) GPIO.output(8,GPIO.LOW) sleep(1)
- When the program is interrupted by typing CTRL + C, we have to clean up and release any occupied GPIO resources:
GPIO.cleanup()
Objective complete – mini debriefing
In this section, we finished writing a program to make an LED blink. In the next section, we will put a circuit together that makes an LED blink.