0% found this document useful (0 votes)
96 views5 pages

Communicating Using I2C and SPI

The document discusses communicating with devices using I2C and SPI protocols on Arduino boards. It describes: 1) I2C uses just two connections (SCL and SDA) but has lower data rates than SPI. It is well suited for sensors that don't require high data transfer. SPI uses separate input/output connections allowing full duplex communication and higher data rates, but requires more connections. 2) The Arduino Wire library handles low-level I2C functionality. Devices have addresses and specific data must be sent/received according to their datasheets. Logic level translators allow use of 3.3V I2C devices with 5V Arduino boards. 3) SPI uses separate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views5 pages

Communicating Using I2C and SPI

The document discusses communicating with devices using I2C and SPI protocols on Arduino boards. It describes: 1) I2C uses just two connections (SCL and SDA) but has lower data rates than SPI. It is well suited for sensors that don't require high data transfer. SPI uses separate input/output connections allowing full duplex communication and higher data rates, but requires more connections. 2) The Arduino Wire library handles low-level I2C functionality. Devices have addresses and specific data must be sent/received according to their datasheets. Logic level translators allow use of 3.3V I2C devices with 5V Arduino boards. 3) SPI uses separate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CHAPTER 13

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.

Figure 13-1. An I2C master with one or more I2C slaves

I2C devices need a common ground to communicate. The Arduino Gnd


pin must be connected to ground on each I2C device.

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.

422 | Chapter 13: Communicating Using I2C and SPI


I2C and SPI only define how communication takes place between devices—the mes-
sages that need to be sent depend on each individual device and what it does. You will
need to consult the data sheet for your device to determine what commands are required
to get it to function, and what data is required, or returned.
The Arduino Wire library hides all the low-level functionality for I2C and enables sim-
ple commands to be used to initialize and communicate with devices. Recipe 13.1
provides a basic introduction to the library and its use.

Migrating Wire code to Arduino 1.0


The Arduino Wire library has been changed in release 1.0 and you will need to modify
sketches written for previous releases to compile them in 1.0. The send and receive
methods have been renamed for consistency with other libraries:
Change Wire.send() to Wire.write().
Change Wire.receive() to Wire.read().
You now need to specify the variable type for literal constant arguments to write, for
example:
Change Wire.write(0x10) to Wire.write((byte)0x10).

Using 3.3 Volt Devices with 5 Volt Boards


Many I2C devices are intended for 3.3 volt operation and can be damaged when con-
nected to a 5 volt Arduino board. You can use a logic-level translator such as the
BOB-08745 breakout board from SparkFun to enable connection by converting the
voltage levels (see Figure 13-2). The level translator board has a low-voltage (LV) side
for 3.3 volts and a high-voltage (HV) side for 5 volts.

Figure 13-2. Using a 3.3V device with a logic-level translator

13.0 Introduction | 423


For a 3.3V I2C device, connect the LV side as follows:
• Upper TXI pin to I2C SDA pin
• Lower TXI pin to I2C SCL pin
• LV pin to I2C VCC (power) and 3.3 volt power source
• GND pin to I2C Gnd
Connect the HV side as follows:
• Upper TXO pin to I2C SDA pin
• Lower TXO pin to I2C SCL pin
• HV pin to Arduino 5 volt power source
• GND pin to Arduino Gnd
You can connect multiple I2C devices using a single logic-level translator, as in
Figure 13-3.

Figure 13-3. Connecting multiple 3.3V and 5V I2C devices

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.

424 | Chapter 13: Communicating Using I2C and SPI


Figure 13-4. Signal connections for SPI master and slaves

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

13.1 Controlling an RGB LED Using the BlinkM Module


Problem
You want to control I2C-enabled LEDs such as the BlinkM module.

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.

13.1 Controlling an RGB LED Using the BlinkM Module | 425

You might also like