I²C Basics |
When should I use I²C vs SPI? |
I²C devices are generally slower and lower cost than SPI. Multiple
I²C device systems require fewer signals to implement. Some "exotic"
functions are only available in I²C - car stereo tuners, amps,
and tone controls, for example. |
I²C Signals - just SCL and SDA |
I²C has only two signals - clock (SCL) and data (SDA). There
are no chip selects. These are the only signals (OK, you also
need power and ground) needed for an I²C system of arbitrary
complexity. Old I²C chips are limited to 100 KHz max SCL. Newer
parts can operate up to 400 KHz or faster. |
I²C Slave Addressing |
Each I²C slave device has a 'base address' assigned by the
manufacturer. Some devices have up to three address inputs which
are typically wired to a fixed value by circuit board traces
or jumpers. This enables up to eight instances of that slave
device on a single I²C net. For example, the PCA9554 has a base
address of 0x20, but the address bits extend that to 0x20..0x27. |
Party line SCL and SDA |
Multiple I²C slaves share the same SCL and SDA lines. To
avoid clashes, each slave must have a unique address. |
Slaves always ACK, so check for this |
Slaves always drive a bit cell active/low to ACKnowledge each I²C transmission. Your code should check to see that an ACK is present, otherwise the rest of the I²C data cycle is suspect. |
Read the slave device data sheets |
This sounds simple but it's a recurring support issue. Read
the data sheets! They are there for a reason (that is: they
contain important information you need to use
the part successfully), and most vendors do a good job of
documenting their parts (they don't want support headaches). |
Consider CMOS vs TTL voltage levels |
I²C devices can have CMOS or TTL voltage levels, and many
can operate from 5 volts or considerably less. Read the data
sheets
to see what your device needs for proper
input and output
interfacing. Here's an overview of JStamp
CMOS and TTL voltage level interfacing (also applies
to JStik and SaJe) and a page on TStik
interfacing and voltage levels. The same CMOS/TTL
rules apply to any standard devices. For example, the PCF8574/A
are not TTL-level compatible when powered by 5 volts but
are usable with TTL inputs when powered by 3.3 or 3.0 volts.
A better device is the PCA9554/A which has TTL level inputs
when powered by 2.7 to 5 volts. |
Start simple |
Start talking to to a slave device in the simplest possible
fashion to get things working. Then add complexity. Some slaves
can be quite complex (eeproms and multi-mode
DACs come to mind). Don't try to write and test the whole
slave driver at once - get a simple test case working first.
Start with I2C.ping() which just looks for an ACK from a slave
at that address, and returns boolean true if successful. |
Master/slave |
Typically I²C systems have one master and one
or more slaves, but there's nothing preventing a multi-master
system. Slaves are typically polled by the
master, but a slave can also emit an interrupt on a separate
signal, to tell the master when it needs servicing. There's
nothing in the I²C (at least that's my memory) specification
to arbitrate between multiple masters, so you would need to
figure
this
out yourself. |
I²C can have a native hardware and/or a firmware implementation |
Since all an I²C master needs to do is emit data synchronized
with a clock, this is simple hardware to design. It is much
like a simplified UART transmit section. I²C is also easily implemented in firmware. |
I²C clock is not critical in terms of speed or duty cycle,
only the edges matter |
This makes I²C an easy protocol to implement, and somewhat noise tolerant. As long as the data is stable around the time of the appropriate clock edge(s), and there are no spurious clock edges, you should obtain a very reliable connection. |
I²C is open-collector, needs pullups |
I²C signals are party-line and therefore open-collector or open-drain, meaning they are only actively pulled low. Pullup resistors are needed to create a valid inactive/high level. Some I²C slave devices have pullups built-in, but many do not. On a party line you don't want every device to have its own pullups, or the load will be too high. So check your device data sheets and make sure that you have appropriate pullups. Depending on your system, the pullup could be to 5V, 3.3V or something close to that. Start with 10 KOhms on both the clock and data lines. |