Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Raspberry Pi Embedded Projects Hotshot

You're reading from   Raspberry Pi Embedded Projects Hotshot Enter the world of mechatronic systems with the Raspberry Pi to design and build 12 amazing projects

Arrow left icon
Product type Paperback
Published in Feb 2015
Publisher
ISBN-13 9781849696227
Length 250 pages
Edition 1st Edition
Concepts
Arrow right icon
Toc

Table of Contents (15) Chapters Close

Preface 1. Hello World FREE CHAPTER 2. A Raspberry WebIDE Example 3. The Arduino Raspberry Pi Interface 4. Christmas Light Sequencer 5. Internet of Things Example – An E-mail Alert Water Fountain 6. Raspberry Pi as a Personal Assistant 7. Raspberry Pi-based Line Following Robot 8. Connect Four Desktop Game using Raspberry Pi 9. The Raspberry Pi-enabled Pet/Wildlife Monitor 10. Raspberry Pi Personal Health Monitor 11. Home Automation using Raspberry Pi 12. Using a Raspberry Pi for Science and Education 13. Tips and Tricks Index

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.

You have been reading a chapter from
Raspberry Pi Embedded Projects Hotshot
Published in: Feb 2015
Publisher:
ISBN-13: 9781849696227
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime