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


Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

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

  • IT leaders are embracing edge computing as a powerful way to maximize the business value of initiatives such as the internet of things, mobile computing and Digital Transformation. Edge computing improves time to action, enables analytics in near real-time and conserves vital network resources. However, whichever technology you use to build edge computing micro data centers, you must make sure that you focus on Availability and deploy modern Availability solutions to ensure that all the data is safe. Read …

  • Using Internet of Things (IoT) technologies, colleges and universities are linking anything and everything to create new applications that elevate the campus experience. It's called a Smart Campus. And it's transforming campus life for students, faculty and visitors. When everything is connected, devices and applications can interact with users and with each other in ways that weren't possible before: digital classrooms, connected campus ID cards, connected lighting and building environmental systems and …

Most Popular Programming Stories

More for Developers

RSS Feeds

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