Arduino & I2C Module
| Gene's Quick Lab Ref
IIC/I2C/TWI/SP Serial Interface Module For Arduino | EMBEDDED COMPUTING
2017 NOVEMBER | by Gene Casanova
Senior Systems/Network/Internet Engineer
I2C stands for "Inter-integrated Circuit Protocol"; a protocol intended to enable multiple "slave" digital integrated circuits (IC chips) to communicate with one or more "master" IC chips; creating one complete integrated system.
The name I2C translates to "Inter IC". Sometimes the bus is referred to as "IIC" or "I²C" bus.
The I2C bus was designed by the Philips company during the early 1980s to enable easy communication between components, all located on a circuit board; forming one integrated system. I2C was developed as a very short distance communications method within a single printed circuit board (PCB).
I2C requires two signal wires to exchange information.
Why Use I2C?
Compare the options for IC to IC communications.
- Serial Communications (serial port) - an asynchronous communications link; inherently suited to communications between only two devices.
- Serial Peripheral Interface (SPI) - requires at least 4 physical links; MISO, MOSI, SCK, and one specific dedicated-link for each IC-device. Connecting a single 'SPI master' to a single 'SPI slave' with an 'SPI bus' requires four lines. Each additional 'SPI slave' requires one additional 'chip select' I/O pin on the 'SPI master'. The rapid proliferation of pin connections makes it undesirable in situations where lots of IC devices must be SPI-slaved to one SPI-master. The large number of connections for each IC-device makes routing signals, more difficult in tight PCB layouts. SPI only enables one 'master' on the bus, but it does support an arbitrary number of 'slaves' (subject only to the drive capability of the IC-devices connected to the bus, and the number of 'chip select' (CS) pins available.
- SPI is good for high data rate full-duplex (simultaneous sending and receiving of data) connections, supporting clock rates upwards of 10MHz (and thus, 10 million bits per second) for some devices, and the speed scales nicely. The hardware at either end is usually a very simple shift register, enabling easy implementation in software.
- I2C - Supports up to 1008 'slave' IC-devices, and requires only 2 wires. I2C can support a multi-master system, enabling more than one 'master' to communicate with all IC-devices on the bus. SPI 'master' devices, cannot communicate to other master devices on the bus and must take turns using the bus lines.
- Data rates fall between asynchronous serial and SPI. Most I2C IC-devices can communicate at 100kHz or 400kHz.
- There is overhead with I2C; for every 8 bits of data to be sent, one extra bit of meta data (the "ACK/NACK" bit) must be transmitted.
- Hardware required to implement I2C, is more complex than SPI, and less than asynchronous serial.
- I2C can be simple to implement in software.
I2C At The Hardware Level
Each I2C bus consists of two signals: SCL and SDA. SCL is the clock signal, and SDA is the data signal.
The SCL, clock signal, is always generated by the current bus master; some slave devices may force the clock low at times, to delay the master sending data; or, to require more time to prepare data before the master attempts to send. This is referred to as "clock stretching".
The I2C bus drivers are "open drain"; meaning they can pull the corresponding signal line LOW, but cannot drive it HIGH. There can be no bus contention, where one IC-device is trying to drive the line HIGH while another, tries to pull it LOW. This eliminates the potential for damage to the I2C bus drivers, and excessive power dissipation in the system. Each signal-line has a pull-up resistor on it, to restore the signal to HIGH when no device is asserting it LOW.
Pull-up resistor selection varies with IC-devices connected to a I2C bus. A good practise is to start with 4.7k ohms, and adjust down if necessary. I2C is a relatively robust protocol, and can be used with short runs of wire (2-3m). For long runs, or systems with lots of devices, smaller resistors are better.
An Arduino library named "Wire", enables an Arduino to communicate with I2C / TWI enabled IC-devices.
On an Arduino systemboard with the 'R3' layout (1.0 pinout), the 'SDA' (DATA line) and 'SCL' (CLOCK line) are on the pin header close to the 'AREF' pin.
The 'Arduino Due' has two I2C/TWI interfaces; 'SDA1' and 'SCL1' are near to the 'AREF' pin, and the additional interface on pin 20 and 21.
The table below provides the location of the TWI pins on various Arduino systemboards.
|Systemboard||I2C / TWI Pins|
SDA | SCL
As of Arduino 1.0, the library was rewritten to be consistent with other read/write libraries. The previous send() and receive() functions, have been replaced with, read() and write() functions.
I2C Addresses - 7 & 8 Bit
There are 7- and 8-bit versions of I2C addresses.
7-bits identify the IC-device, and the eighth-bit selects write-to or read-from the IC-device.
The 'Wire' library, uses 7-bit addresses throughout.
Sample code with 8-bit addresses, can be edited, dropping the low-bit (shift the value one bit to the right) in an address to get an address between 0 and 127. Notice, addresses from 0 to 7 cannot be used; because these are reserved. The first available address is 8.
A pull-up resistor is needed when connecting SDA/SCL pins.
The MEGA 2560 systemboard, has pull-up resistors on pins 20 - 21.
Arduino & I2C Module Connection
The 2 I2C signal pins on the differencet Arduino systemboards, are show in the following table:
|Arduino Systemboard||I2C / TWI Pins|
SDA | SCL
|MEGA2560 PRO MINI||20||21|
Connect VCC to 5+.
Connect GND to GND.
A total of 2 simple connections.
Arduino & I2C Sketch
* I2C Serial Interface Module
* Pin Connections:
* SCL = A5 Analog I/O Pin Arduino UNO
* SDA = A4 Analog I/O Pin Arduino UNO
* VCC = 5V
* GND = GND
Addressing Multiple Devices With I2C
Analog I/O pins A4, and A5 are the I2C interface pins on an Arduino UNO systemboard.
The Arduino functions as a I2C master and each connected sensor to the IC2 interface, is a I2C slave.
One signal line is the Serial Data Line (SDA). The second signal line is the SERIAL CLOCK LINE (SCL).
1 Pull-Up Resistor from SDA to Ground, and 1 Pull-Up resistor from SCL and ground; mount both near the Arduino UNO I/O pins. 1 resister each is used for one I2C BUS line.
Refer to UM10204
I2C-BUS Specification And User Manual Rev. 6 - 4 April 2014
Voltage on SCL line must not be greater than 0.4V; with a maximum currect-load Imax of 3mA. This is the limit of the switching transistor in the I2C IC device in the closed (circuit enabled) mode of operation.
The minimum Pull-Up Resistor is provided by the worst case.
5V VCC, 0.4V SCL, and 3mA, Minimum resistor must be 1.53K.
3.3V VCC - 0.4V / 3mA = 970 ohms minumum resistor value.
How many I2C devices can be connected to a I2C BUS?
It depends on the I2C sensor modules (breaout boards).
Breakout Boards contain Pull-Up Resistors.
Typical breakout boards have a 2.2K to 10K Pull-Up resistor.
When multiple breakout-boards are connected to the I2C bus, the Pull-Up resistors are being connected in parallel.
R = 1 / (1/10K x 10) = 1,000 (1K ohms) 10 connected breakout boards.
Minimum Resistor for 5V = 1.53K Ohms
Minimum Resistor for 3.3V = 970 Ohms
What Resistor Is On A Breakout Board>
User a ohm meter, and measure the resisitance between the SERIAL CLOCK LINE (SCL) and VCC with circuit not connected.
How Many Devices?
When using 7-bit addresses, 128 Devices.
When using 10-bit addresses, 1024 Devices.
Master to master and master to slave serial communications can be done.
Private tutoring in Milwaukee WI may be available.
Contact to continue the conversation.
reference: https://www.arduino.cc, http://www.i2c-bus.org
Use The Technology Wisely & Keep It Simple
Need More? Need Help? ........Software, Network, System & Data Center Builder & Developer | Development Services Available - Freelance Small Jobs; To Outsourced Long Term Service Provider Contract Labor Available.
CGI Computer Wares | EST 1979