Interactive Media & Game Development
Worcester Polytechnic Institute

IMGD


IMGD-3xxx: HCI for Real, Virtual, and Teleoperated Environments
In-Class Exercise 2

Objective: The main objective for this exercise is to get you familiar and comfortable using I2C programming with the Arduino. This tutorial teaches the basic steps for allowing two Arduinos to communicate over the I2C bus. This configuration is useful when, for example, your application calls for more I/O pins than are available on a single Arduino, or when the devices you are controlling are distant from each other.

What to do: Background
To get an idea about what I2C is, please read this part of the Programming Interactivity book.

Initial set up

  1. Connect two Arduinos to two neighboring machines in the lab (or to two USB ports on the same machine)
  2. Using the wires that came with your Arduino Kit, connect Analog IN pin 4 on one of the Arduinos to Analog IN pin 4 on the other. Do the same for Analog pin 5.
  3. Now you can use the Wire library in your sketches.
Simple I2C Test
  1. Choose one of yor Arduinos to be used as the "Master", and the other as the "Slave". It doesn't matter which one you choose as the Master or Slave, as long as you remember which is which.
  2. Use this the Master-Sender code to create a new sketch on the Master.
  3. Use this the Slave-Receiver code to create a new sketch on the Slave.
  4. Open the Serial Moniter on both the Master and Slave machines. You should see a marching stream of output on the Slave. This is data sent from the Master.
Something More Interesting
  1. Create two new sketches: One on the Master called "i2c_Master_Blinker", and one one the Slave called "i2c_Slave_Blinker".
  2. Write code on the Master to read an integer from the serial port, and send it to the Slave.
  3. Write code on the Slave to read an integer from the Master, and blink an LED that number of times.
  4. The Slave should then send the integer, incremented by one, back to the Master, which should then blink an LED that number of times. You might want to include adequate delays between blinks so you can count them easily.
More Info:
A Wire reference page can be found here: http://arduino.cc/en/Reference/Wire
A set of Wire examples is available also.
Many off-the-shelf devices use I2C, so this code should come in handy.


Back to course page.