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

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.

主站蜘蛛池模板: 平原县| 贵溪市| 湘阴县| 额济纳旗| 饶河县| 东阿县| 饶河县| 扎囊县| 天水市| 宜都市| 梁平县| 楚雄市| 安康市| 平远县| 新和县| 蓝山县| 鄂托克旗| 嵊泗县| 大冶市| 宁都县| 深泽县| 澄江县| 福鼎市| 古交市| 会同县| 义乌市| 鄂温| 图们市| 堆龙德庆县| 芜湖县| 九寨沟县| 五大连池市| 肇东市| 吉水县| 河池市| 社会| 遂川县| 武邑县| 岳阳市| 武宣县| 辉南县|