Notes About Working with Various Arduino & Netduino Microcontroller Boards

Thursday, June 26, 2014

Two I2C Devices with the Same Address with an Arduino Due

The Arduino Due has two separate I2C connections, not just redundant pins connected to the same I2C pins on the processor (as on the Arduino Uno).  This makes it possible to use two I2C devices with the same device address.

Here is a quick example that uses two TMP102 breakout boards from Sparkfun. The new version of this board requires removing a solder jumper to change the device's I2C address.  Otherwise, the boards all use 0x48, regardless of how the ADD0 pin is connected.

Connections


TMP102 #1  Arduino Due
VCC        3.3V
GND        GND
SDA        SDA (D20) (Wire)
SCL        SCL (D19) (Wire)
ADD0       (Not connected)

TMP102 #2  Arduino Due
VCC        3.3V
GND        GND
SDA        SDA1 (Wire1)
SCL        SCL1 (Wire1)
ADD0       (Not connected)


Code


#include <Wire.h>

// Formula to convert Celsius to Fahrenheit
// This is not directly tied to the functioning of the TMP102
// and is a simple formula, so it useful to use #define for the 
// conversion function.
#define C_TO_F(x) (x * 1.8 + 32.0)
#define I2C_ADDR 0x48

void setup() {
  Wire.begin();
  Wire1.begin();
  Serial.begin(9600);
}

void loop() {
  Wire.requestFrom(I2C_ADDR, 2);
  byte msb1 = Wire.read();
  byte lsb1 = Wire.read();
  Wire1.requestFrom(I2C_ADDR, 2);
  byte msb2 = Wire1.read();
  byte lsb2 = Wire1.read();
  double t1 = (((msb1 << 8) | lsb1) >> 4) * 0.0625;
  double t2 = (((msb2 << 8) | lsb2) >> 4) * 0.0625;
  Serial.print(C_TO_F(t1), 3);
  Serial.print(" ");
  Serial.println(C_TO_F(t2), 3);
  delay(1000);
}

No comments:

Post a Comment