Communicating Using I2C and SPI
Communicating Using I2C and SPI
13.0 Introduction
The I2C (Inter-Integrated Circuit) and SPI (Serial Peripheral Interface) standards were
created to provide simple ways for digital information to be transferred between sensors
and microcontrollers such as Arduino. Arduino libraries for both I2C and SPI make it
easy for you to use both of these protocols.
The choice between I2C and SPI is usually determined by the devices you want to
connect. Some devices provide both standards, but usually a device or chip supports
one or the other.
I2C has the advantage that it only needs two signal connections to Arduino—using
multiple devices on the two connections is fairly easy, and you get acknowledgment
that signals have been correctly received. The disadvantages are that the data rate is
slower than SPI and data can only be traveling in one direction at a time, lowering the
data rate even more if two-way communication is needed. It is also necessary to connect
pull-up resistors to the connections to ensure reliable transmission of signals (see the
introduction to Chapter 5 for more on pull-ups).
The advantages of SPI are that it runs at a higher data rate, and it has separate input
and output connections, so it can send and receive at the same time. It uses one addi-
tional line per device to select the active device, so more connections are required if
you have many devices to connect.
Most Arduino projects use SPI devices for high data rate applications such as Ethernet
and memory cards, with just a single device attached. I2C is more typically used with
sensors that don’t need to send a lot of data.
This chapter shows how to use I2C and SPI to connect to common devices. It also
shows how to connect two or more Arduino boards together using I2C for multiboard
applications.
421
I2C
The two connections for the I2C bus are called SCL and SDA. These are available on
a standard Arduino board using analog pin 5 for SCL, which provides a clock signal,
and analog pin 4 for SDL, which is for transfer of data (on the Mega, use digital pin 20
for SDA and pin 21 for SCL). Uno rev 3 boards have extra pins (shown back in Rec-
ipe 1.2) that duplicate pins 4 and 5. If you have such a board, you can use either set of
pins. One device on the I2C bus is considered the master device. Its job is to coordinate
the transfer of information between the other devices (slaves) that are attached. There
must be only one master, and in most cases the Arduino is the master, controlling the
other chips attached to it. Figure 13-1 depicts an I2C master with multiple I2C slaves.
Boards introduced with Arduino 1.0 such as the Leonardo board have
the SCL and SDA lines duplicated on pins next to the AREF pin. This
new location for these pins enables future boards to always have the I2C
connections in the same physical position.
Slave devices are identified by their address number. Each slave must have a unique
address. Some I2C devices have a fixed address (an example is the nunchuck in Rec-
ipe 13.2) while others allow you to configure their address by setting pins high or low
(see Recipe 13.7) or by sending initialization commands.
Arduino uses 7-bit values to specify I2C addresses. Some device data
sheets use 8-bit address values. If yours does, divide that value by 2 to
get the correct 7-bit value.
For examples that use a logic-level translator, see the discussion on the ITG-3200 in
Recipe 6.15 and the HMC5883 in Recipe 6.16.
SPI
Recent Arduino releases (from release 0019) include a library that allows communica-
tion with SPI devices. SPI has separate input (labeled “MOSI”) and output (labeled
“MISO”) lines and a clock line. These three lines are connected to the respective lines
on one or more slaves. Slaves are identified by signaling with the Slave Select (SS) line.
Figure 13-4 shows the SPI connections.
The pin numbers to use for the SPI pins are shown in Table 13-1.
Table 13-1. Arduino digital pins used for SPI
SPI signal Standard Arduino board Arduino Mega
SCLK (clock) 13 52
MISO (data out) 12 50
MOSI (data in) 11 51
SS (slave select) 10 53
See Also
Applications note comparing I2C to SPI: http://www.maxim-ic.com/app-notes/index
.mvp/id/4024
Arduino Wire library reference: http://www.arduino.cc/en/Reference/Wire
Arduino SPI library reference: http://www.arduino.cc/playground/Code/Spi
Solution
BlinkM is a preassembled color LED module that gets you started with I2C with min-
imal fuss.
Insert the BlinkM pins onto analog pins 2 through 5, as shown in Figure 13-5.