Interaction Design WikiElectrical Engineering

I2C (en)

German Version


The I2C interface was originally developed by Phillips to allow communication of individual components. Some manufacturers also use the designation TWI Interface (Two-Wired-Interface). As the name suggests, the I2C interface uses two signal lines to communicate with the peripherals. One line is the clock line (SCL) and the other is the data line (SDA). Both lines are connected to VCC with a pullup resistor.

Recently, more and more sensors have come onto the market that use the I2C interface. Especially acceleration sensors and sensors for distance measurement belong to them. Here is a small overview.


Sniffer Code

This example will show any devices connected to the I2C bus, together with their address,

i2c Address Sniffer  Expand source
#include <Wire.h> 
 
void setup()
{
  Wire.begin();
 
  Serial.begin(9600);
  Serial.println("I2C Sniffer");
}
 
void loop()
{
  byte error, address;
  int nDevices;
 
  Serial.println("Scanning...");
 
  nDevices = 0;
  for(address = 1; address < 127; address++ )
  {
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
 
    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println(" !");
      nDevices++;
    }
    else if (error==4)
    {
      Serial.print("Unknown error at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");
 
  delay(4000);
}


Example

1x Arduino
1x MMA7455
2x Widerstand 4.7k

The MMA7455L is a very low cost accelerometer from Freescale. It is accessed via the I2C interface and can be operated at three different levels (2g, 4g, 8g). To make the handling as easy as possible there is a library for Arduino and the sensor comes on a breakout board.

The connection of the accelerometer (MMA 7455L) is relatively simple. The i2c interface requires only two lines (Analog4/SCL and Analog5/SDA) of which one serves the clock and one the data. To ensure communication, a pullup resistor to VCC (3.3V) must be added to each of the SCL and SDA lines. The sensor runs with 2.4-3.7V which is why we connect it to the 3.3V output of the Arduino. GND is connected to the GND of the Arduino as usual.

Functions

MMA_7455
This function instantiates the MMA_7455 library and activates the wire library which is needed for the communication via I2C.

initSensitivity(int)
Here you define how sensitive the sensor should react. This depends strongly on the use of the sensor. The sensitivity can be specified via the variable. The following applies: 2=2g, 4=4g and 8=8g.

calibrateOffset(float, float, float)
Here the offset of the sensor can be calibrated. Corrections can be made via the three float values. In order to determine the correct values for this, the values of the sensor should first be read out without calibration when it is lying on a flat surface. The so obtained values should be corrected so that for xVal+float = 0, yVal+float = 0 and zVal+float = 64 comes out. Then the calibrateOffset() function in setup() can be called once with the corrected values.

readAxis(char)
The sensor is read out here. By passing the corresponding parameter ('x', 'y' or 'z'), the desired axis is addressed.

Beispiel MMA7455 Expand source
#include <Wire.h> //Include the Wire library
#include <MMA_7455.h> //Include the MMA_7455 library

MMA_7455 mySensor = MMA_7455(); //Make an instance of MMA_7455
char xVal, yVal, zVal; //Variables for the values from the sensor

void setup()
{
  Serial.begin(9600);
  // Set the sensitivity you want to use
  // 2 = 2g, 4 = 4g, 8 = 8g
  mySensor.initSensitivity(2);
  // Calibrate the Offset, that values corespond in
  // flat position to: xVal = -30, yVal = -20, zVal = +20
  // !!!Activate this after having the first values read out!!!
  //mySensor.calibrateOffset(5, 20, -68);
}

void loop()
{
  xVal = mySensor.readAxis(‘x’); //Read out the ‘x’ Axis
  yVal = mySensor.readAxis(‘y’); //Read out the ‘y’ Axis
  zVal = mySensor.readAxis(‘z’); //Read out the ‘z’ Axis
  Serial.print(xVal, DEC);
  Serial.print("\t");
  Serial.print(yVal, DEC);
  Serial.print("\t");
  Serial.println(zVal, DEC);
}