官术网_书友最值得收藏!

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.

Engage thrusters

Launching IDLE3 from the desktop

  1. Now, we have to get started with programming the LED blinking example in IDLE3.
  2. 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()
  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 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()
  2. 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)
  3. We can set the pin to logical high (3.3 V) as follows:
        GPIO.output(8,GPIO.HIGH)
  4. We can set the pin to logical low (3.3 V) as follows:
        GPIO.output(8,GPIO.LOW)
  5. 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)
  6. 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.

主站蜘蛛池模板: 山东| 育儿| 禄劝| 东乌珠穆沁旗| 丹巴县| 贡山| 石门县| 兴山县| 侯马市| 太谷县| 双流县| 仙居县| 高雄市| 东丽区| 沅陵县| 宜春市| 论坛| 安阳县| 宿迁市| 贵德县| 常熟市| 湘乡市| 麦盖提县| 周宁县| 红原县| 四子王旗| 邵东县| 玛多县| 台北市| 松阳县| 慈溪市| 无棣县| 衡南县| 上饶县| 进贤县| 蚌埠市| 吕梁市| 通化市| 商水县| 尉氏县| 江门市|