Hardware Reference
In-Depth Information
interrupt occurs), and possibly others with different specialized functions. To
read or write this data, you need to know several details:
The slave address
The register number
If it is a read or write operation
The length of the data to be received
It is important to know exactly how much data is to be sent and received.
Each I 2 C device is different and will function in a different way. Devices that
have only one writable register might accept a single byte of data directly and
will place that byte into the register. Other devices with several writable regis-
ters might require you to send the register number, followed by the contents,
or maybe send the contents of all the registers in multiple writes. I 2 C describes
a way to send data and receive data, but for your own implementation, it is up
to you what you need.
All Arduinos have a pair of I 2 C pins. The Arduino Due has two separate
I 2 C buses, SDA and SCL, as well as SDA1 and SCL1. The pins reserved for I 2 C
operations are listed in Table 8-1.
Table 8-1: I 2 C Pins on Diff erent Arduino Boards
BOARD
SDA
SCL
Uno
A4
A5
Ethernet
A4
A5
Mega2560
20
21
Leonardo
2
3
Due
20
21
Communicating
To communicate on the I 2 C bus, the Wire library must i rst be initialized. As
with all Arduino libraries, you must import the Wire library. This is done by
either adding the library from the Arduino IDE (Sketch
Import Library
Wire) or by manually typing in the sketch.
#include <Wire.h>
To declare the Arduino as an I 2 C device, call Wire.begin() . If the Arduino
is used as a slave, you must specify an address.
Wire.begin(address); // configures the Arduino as an I2C slave
 
Search WWH ::




Custom Search