Hot Swap Devices and Increase Arduino Interface Options with I2C

Don't be afraid of the bus

After a short period of time, beginners working with the Arduino development boards often find themselves wanting to work with a greater range of input or sensor devices—such as real-time clocks, temperature sensors, or digital potentiometers.

However each of these can often require connection by one of the two digital data buses, known as SPI and I2C. After searching around the Internet, inexperienced users may become confused about the bus type and how to send and receive data with them, then give up.

This is a shame as such interfaces are quite simple to use and can be easily understood with the right explanation. For example let’s consider the I2C bus. It’s a simple serial data link that allows a master device (such as the Arduino) to communicate with one or more slave devices (such as port expanders, temperature sensors, EEPROMs, real-time clocks, and more).

Connecting I2C devices to an Arduino is simple—by using two wires from the Arduino and a couple of resistors, the slave devices can each connect to the bus (as well as power) without any complex wiring—for example:

On your Arduino Uno (the most popular board) the SDA is pin A4 and SCL is pin A5. Apart from the simplicity of the I2C bus, one of the best features is that it is “hot swap” capable. In other words, you can physically disconnect or connect I2C devices to the bus without having to power-down the entire project.

Arduino R3 pinout diagram by Nick Gammon.

When writing a sketch to use an I2C device in Arduino, you only need a few lines for initial setup:

#include “Wire.h”

and the following inside void setup():

Wire.begin();

From then it’s a simple process of determining the device’s bus address. This is a number unique to each type of device (a simple analogy would be a telephone number). This can be found in the data sheet or from the retailer, and is usually a hexadecimal number, such as 0x50. Next, you need the location addresses in the device where data is written to or read from.

A location address can be thought of as a number on a row of storage locations such as boxes. For example, when using the inexpensive Microchip 24LC512 EEPROM—it has 65,536 locations, each numbered from zero to 65,535. To write a value to a location you simply use:

Wire.beginTransmission(device address);
Wire.write(data);
Wire.endTransmission();

For example, if you wanted to write the value 1000 in the third memory location in the EEPROM mentioned earlier, the three lines to use would be:

Wire.beginTransmission(0x50); // contact the EEPROM
Wire.write((byte)(3 >> 8)); // left part of pointer address
Wire.write((byte)(3 & 0xFF)); // and the right
Wire.write(1000); // write the number to the location
Wire.endTransmission(); // and we're finished

Then to retrieve the data from the address—it’s a little bit easier. Once again you contact the device with:

Wire.beginTransmission(device address);

then tell the device which memory location you want to read, then put that information into a variable.

Arduino Workshop

From the simple sequence of functions described you can now see how simple it is to use the I2C bus with Arduino. There’s nothing to be worried about, and using the bus opens up a whole world of possibilities for your future projects. So what are you waiting for?

All this and more is covered in Chapter Seventeen of my new book, Arduino Workshop: A Hands-On Introduction with 65 Projects, inside which the complete novice can start with the Arduino platform from blinking an LED to creating quite interesting and useful projects.

Australian author John Boxall has been writing Arduino tutorials, projects, and kit and accessory reviews for several years at www.tronixstuff.com. Arduino Workshop is his first book.

tags: , , , , ,