Getting Started with the Raspberry Pi 2: Basic Input and Output

The Raspberry Pi 2 has a number of input and output channels, or general purpose I/O (GPIO) ports, available for programmatic control. Python has been one of the more popular languages on the Raspberry Pi since its introduction and remains so with the Pi 2. A quick web search for Raspberry Pi Python will return a large number of results.

The default NOOB installation that we're using for these articles comes with the RPi.GPIO library installed. RPi.GPIO contains everything you'll need to manipulate the GPIO pins to implement a wide range of projects. We'll walk you through the basics of the library in this article and finish up with an example or two in the final installment.

Before we get into the code, we should explain a little about the GPIO pins on the Raspberry Pi 2. The site has a much more detailed write-up of the GPIO pins here. For now, we'll just go over the basics available on the main GPIO header. The first types of pins are single input and output lines. These eight individual lines must be configured programmatically as either input or output and are typically used to read a switch or drive an LED. You can also connect to I2C and SPI devices plus traditional serial communication. One additional pin is available for pulse-width modulation (PWM) used in controlling motors.

One of the best ways to investigate any Python library is from the command prompt. Launching Python 2 from the Programming menu will open a new window where any legal Python statement is fair game.

import RPi.GPIO

This will load the RPi.GPIO module and make all methods and properties available. Typing the command dir(GPIO) will show all attributes of the GPIO module. Figure 1 shows the output of this command.

Figure 1: The output of the dir(GPIO) command

Another way to accomplish the same thing is to use the autocomplete function available from Python. Figure 2 shows what happens when you type part of a command and then press the tab key. In this case, the first available option after typing GPIO.set is displayed. You could just as easily type GPIO period-tab to see the first available option.

Figure 2: Pressing the tab key activates the autocomplete function

You will find a complete description of all GPIO methods on the project site. It includes a number of example programs showing off a variety of ways to use the GPIO pins. At the simplest level, you issue single commands such as GPIO.output(pin, value) where pin represents the specific pin and value would be either a zero or one, depending on the wiring. Conversely, you would use the following statement to read a value of an input:

pinstate = GPIO.input(pinnumber)

If you need to wait for a switch action you could use the wait_for_edge() function or the event_detected() function if you don't want to block program execution. We'll look at several of these techniques in the final installment of this series.

Related Articles


  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Organizations today are under tremendous pressure to deliver a higher quality of products and services at lower costs, and to do so using existing resources. Any expenditure companies do make to help them achieve this goal is expected to deliver a measurable, hard-dollar ROI — and to deliver it quickly. The Internet of Things (IoT) is creating new opportunities for companies to enhance their products, gain business insights and differentiate their offerings. This whitepaper defines an ROI model for …

  • Old Habits Die Hard: The report, which is in its fifth year, polled 1,100 senior IT security executives at large enterprises around the world and indicates an ongoing disconnect between the security solutions organizations spend money on and the ability of those solutions to protect sensitive data. While 30 percent of respondents classify their organizations as 'very vulnerable' or 'extremely vulnerable' to data attacks (and the number of breaches continues to rise) the two top spending priorities are network …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date