Reader small image

You're reading from  Raspberry Pi Essentials

Product typeBook
Published inApr 2015
Publisher
ISBN-139781784396398
Edition1st Edition
Right arrow
Author (1)
Jack Creasey
Jack Creasey
author image
Jack Creasey

Jack Creasey has been in the technology industry for more than 30 years, the last 15 years as a Senior Program Manager in hardware and software design. His expertise includes designing, developing, and teaching IT courseware. He is an avid inventor and holds 13 patents in hardware and software design. After retiring early from the computing industry, Jack avidly participates in social network groups that share his passion for next generation technology solutions.
Read more about Jack Creasey

Right arrow

Chapter 6. Driving I2C Peripherals on the Raspberry Pi

In the previous chapter, we tested the single-bit digital I/O ports built into the Raspberry Pi SOC. Now it's time to shift gears to another built-in interface for hardware interfaces.

In this chapter, we will talk about an interface bus that uses a simple message communication protocol that communicates from a master device to a slave device. You will see this referred to as I2C (pronounced I-squared-C) and sometimes as (System Management Bus) SMBus in most Pi technical documentation. Each has different capabilities, but they are essentially identical in protocol. For our needs, we will treat the names as interchangeable, but to be more accurate—we will use I2C.

After completing this chapter, you will be able to:

  • Understand the characteristics of the I2C protocol and its peripheral addressing scheme

  • Read and write to interfaces on the I2C bus

  • Create a program to control a multichannel PWM controller

Getting started with I2C


The specifications for both I2C and SMBus are quite complex. For more in-depth documentation on the differences between I2C and SMBus, refer to the following resources:

The I2C bus is a shared-media multi-drop architecture with only two lines: (1) serial data (SDA) and (2) serial clock (SCL)—not including the ground. The bus is in a quiescent state when no transactions are being sent or received and both the SDA and SCL lines are pulled high by resistors to 3.3V in the Pi (or 5V in other implementations). The master and slave devices only ever pull the data line low, which is described as a wired OR connection. The following diagram shows the general I2C architecture and its location on the Pi I/O connector. Pins 3 and 5 can also be used as GPIO ports but not while you have I2C enabled, so you lose two GPIO ports.

At the protocol level, I2C is similar in concept to the RS232 serial...

Project 1 – Python control of a PWM board driving RC servo motors


You should now have a reasonable understanding of what is required to program an I2C PWM board, so let's look at some Python code to drive the Adafruit 16 channel PWM board.

Note

The python-smbus library is compatible only with Python 2 at the time of writing. To run PCA9685, you have to use IDLE and not IDLE3. To run from the command line, use Python and not Python 3. As with the RPi-GPIO library, the SMBus library needs to have root privileges.

First, create a new /home/pi/servo project directory for the files and download pca9685.py from the Chapter 6 folder at http://1drv.ms/1ysAxkl into the directory.

Then, open the pca9685.py file in the idle development environment, remembering to use the sudo idle command to start it.

Code block 1 – imports, constants, and variables

In this code block, we import the minimum amount for our functionality. Note here that the bus number i2c-1 or i2c-0 may vary depending on the Raspberry Pi version...

Summary


Now that we've completed the sixth chapter, let's review some of the main tasks you learned:

  • Determine when I2C is the best solution to drive hardware

  • Create Python software to use the python-smbus module to drive an interface

  • Drive servos via the I2C interface and the pitfalls and restrictions that apply to their use

In the next chapter, you will look at the design of a mobile Pi system, including power systems and remote access.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Raspberry Pi Essentials
Published in: Apr 2015Publisher: ISBN-13: 9781784396398
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
Jack Creasey

Jack Creasey has been in the technology industry for more than 30 years, the last 15 years as a Senior Program Manager in hardware and software design. His expertise includes designing, developing, and teaching IT courseware. He is an avid inventor and holds 13 patents in hardware and software design. After retiring early from the computing industry, Jack avidly participates in social network groups that share his passion for next generation technology solutions.
Read more about Jack Creasey