Getting to know the Raspberry Pi I2C bus

Lead Image © photoestelar, 123RF.com

I-TWO-C

When the Rasp Pi GPIO connection capabilities are insufficient for your project, you can turn to the industry-standard I2C data bus to communicate with actuators and sensors.

Users rely on the very efficient GPIO interface of the Raspberry Pi for many types of hardware projects. However, some functions, such as analog input and output, are generally not supported by the GPIO and therefore are incorporated in Rasp Pi projects in a limited fashion – if at all.

Similarly, the Raspberry Pi's one lone PWM outlet can quickly put the brakes on a project. Software solutions like ServoBlaster [1] offer a convenient fix, although they are restricted in terms of usefulness. If you really need more digital I/O, though, you will quickly run up against hard limits.

The I2C bus is a simple and professional solution for projects of large scope, and the Raspberry Pi has two corresponding interfaces on-board. In a series of articles, I discuss a number of semiconductors, each of which has an interface for the I2C bus.

General Information

The I2C bus is a serial master-slave bus suitable for communication over short distances – within a circuit board or within a device. The early 1980s technology came out of Philips Semiconductors (now part of NXP Semiconductors) for building control electronics in entertainment systems.

Data transmission occurs synchronously over two bidirectional lines: the serial data line, SDA, and the serial clock line, SCL. Resistors pull both lines up to a positive potential. The master node specifies the speed and operating mode and initiates communication byte for byte. The transmission speed of the bus varies from 100Kbps bidirectional in standard mode up to 5Mbps unidirectional in ultrafast mode (Table 1).

Table 1

I2C Clock Rates

Mode

Maximum Transmission Rate

Direction

Standard mode

100Kbps

Bidirectional

Fast mode

400Kbps

Bidirectional

Fast mode Plus

1Mbps

Bidirectional

High-speed mode

3.4Mbps

Bidirectional

Ultrafast mode

5.0Mbps

Unidirectional

The I2C bus works with an address range of 7 bits, for up to 128 addresses. However, 16 of these addresses are reserved for special tasks, leaving 112 free. The eighth bit tells the slave whether it should receive data from the master or transmit data to the master.

Usually, you can only choose lower bits of the addresses on the slaves because the upper bits are predefined. The best way to figure out which addresses are available is to check the information sheet that comes with the product. Table 2 shows some examples of various address spaces.

Table 2

I2C Address Space

Type

Function

Address

PCF8574

Port expander

0x20-0x27

PCF8574A*

Port expander

0x38-0x3F

PCF8591

ADC/DAC

0x90-0x9F

PCF8583

Clock/Calendar

0xA0-0xA2

*Only the address space differs between PCF8574 and PCF8574A.

If you are working on a large project and need more than the 112 available addresses, you can attach a bus multiplexer to the primary I2C bus; I will devote a section to this specialty component in a future article.

The I2C bus is susceptible to disturbances because it was originally designed to bridge just a few centimeters. Various possibilities exist to help improve the electrical characteristics of the bus, ranging from adaptation of the pull-up resistors to tunneling through CAN bus drivers. I will devote more attention to the question of different I2C bus drivers in a future article.

Testing

You will find an I2C interface directly on the Rasp Pi GPIO. Pins P1-03 (P1 header, pin 3), SDA_1, and P1-05, SCL_1, already have the necessary 1.8kohm pull-up resistors built-in that pull the Rasp Pi to 3.3V on idle. You can access a second I2C interface via the P5 connector, but you will first have to solder this onto the back of the board. You can communicate with the second interface via the P5-3 (SDA_0) and P5-4 (SCL_0) pins, although they have no premounted pull-up resistors.

To run a test, you can attach a PCF8574 microcontroller [2] to the I2C bus (Figure 1) and then observe the most important of its basic functions. The circuit diagram available online [3] for this simple test setup includes four LEDs and four switches.

Figure 1: A PCF8574 microcontroller and four LEDs, each with its own switch.

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content