Reader small image

You're reading from  Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)

Product typeBook
Published inSep 2015
Reading LevelBeginner
Publisher
ISBN-139781783551590
Edition1st Edition
Languages
Right arrow
Author (1)
Dan Nixon
Dan Nixon
author image
Dan Nixon

Dan Nixon is a software and electronics engineer living in the north of England. He has past experience of creating software for data analysis, process control, and business intelligence applications. In most of these projects, Python was one of the main languages used. Dan previously authored another book on the uses of the Raspberry Pi, called Raspberry Pi Blueprints, and has worked on many personal projects that use both Python and the Raspberry Pi.
Read more about Dan Nixon

Right arrow

Chapter 6. Accessing the GPIO Pins

In this chapter, we will look at the simplest way of interfacing with digital electronics using the General Purpose Input and Output (GPIO) port on the Raspberry Pi. To do this, we will be using the RPi.GPIO module, which is installed as standard on the newer versions of Raspbian.

We will also take a quick look at some of the basics of digital electronics and the additional methods of communication offered by the Raspberry Pi, including UART.

Note that some of the examples later in the chapter will require some additional electronic components, all of which can be purchased from standard high street electronics retailers.

The components required are:

  • A small breadboard

  • Some 0.1" male to female jumper wires

  • A push to make a switch

  • Some LEDs

  • The appropriate resistor for the LEDs

The exact value of the resistor can be calculated using an online tool, such as the one found at ledcalc.com. Here, the supply voltage should be 3.3V, the LED current should be 20mA, and...

Digital electronics


Before we start interfacing the Pi to any electronics, we will first take a look at some of the fundamentals of digital electronics.

Logic in digital electronics is composed of two states: high and low. Typically, low is represented by the signal being close to the ground (0V) and high is represented by being close to a reference voltage (usually, the operating voltage of the logic device). In the case of the Raspberry Pi, it is 3.3V.

Note

Be sure that any device that you connect to the Raspberry Pi via the GPIO port can operate at 3.3V.

The point at which a signal changes between these two states is known as an edge. This can either be rising or falling depending on the direction of the state change, as shown in the following diagram:

Another concept we will see later in this chapter is Pulse Width Modulation (PWM). This is the concept of simulating an analog signal (a constant signal at a voltage between the high and the low levels) by creating a digital signal and varying...

The GPIO library


The RPi.GPIO module provides a simple interface to the basic digital logic functionality of the GPIO header as well as software synthesized PWM, which allows the Pi to output an analog like signal.

Before starting to use the GPIO pins, it is important to know what each pin can do and what it is connected to. The following diagram shows the pinouts for each version and revision of the Raspberry Pi:

Note

Note that the Raspberry Pi 2 has the same pinout as the Raspberry Pi B+.

Typically, when using the GPIO pins, you should avoid using the GPIOs 14 and 15 as they are by default used for a serial terminal which provides access to the shell running on the Pi. GPIOs 0, 1, 2, and 3 should also be avoided as they are used for the I2C interface, which requires a pull-up resistor to be used on the pins that is fitted by default and can cause issues with some devices.

Single LED output

To demonstrate the basic use of the GPIO library, we will create a simple script that will flash an LED...

Universal Asynchronous Receiver/Transmitter (UART)


The Raspberry Pi also has a Universal Asynchronous Receiver/Transmitter (UART) connection on the GPIO header that can be used to interface with external hardware (such as an Arduino). Fortunately, it is easy to use through the pySerial Python library (https://pypi.python.org/pypi/pyserial), which provides access to the serial ports on a variety of platforms.

Setting up the serial port

Before we can use the serial port on the Raspberry Pi, there are a couple of configuration files that must first be changed to stop Raspbian from using the port as an additional terminal, which would otherwise cause communication issues if another piece of software attempted to use the port.

The first file that requires modification is /boot/cmdline.txt. Open this file as root using the following command in the terminal:

sudo nano /boot/cmdline.txt

Here, we need to remove console=ttyAMA0,115200 from the single line of the file; doing so tells the Pi not to use...

Additional libraries


While the RPI.GPIO module will certainly be sufficient for simple digital logic, you may find that you need to interface to a device that requires the use of one of the communication protocols supported by the Raspberry Pi, such as I2C or SPI.

Fortunately, there are Python libraries available that can do this; for I2C there is smbus-cffi (https://pypi.python.org/pypi/smbus-cffi) and SPI can be used through py-spidev (https://github.com/doceme/py-spidev).

Summary


In this chapter, we looked at the ways in which the Raspberry Pi can interact with external hardware through basic digital logic and how this can be done using the RPI.GPIO Python module.

In the next chapter, we will continue looking at Pi specific hardware when we take a look at the camera module and its accompanying Python library.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Getting Started with Python and Raspberry Pi (Redirected from Learning Python By Developing Raspberry Pi Applications)
Published in: Sep 2015Publisher: ISBN-13: 9781783551590
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.
undefined
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 $15.99/month. Cancel anytime

Author (1)

author image
Dan Nixon

Dan Nixon is a software and electronics engineer living in the north of England. He has past experience of creating software for data analysis, process control, and business intelligence applications. In most of these projects, Python was one of the main languages used. Dan previously authored another book on the uses of the Raspberry Pi, called Raspberry Pi Blueprints, and has worked on many personal projects that use both Python and the Raspberry Pi.
Read more about Dan Nixon