Reading I2C Data from a second TMP007

Need help with the code to read data from a second IR sensor on the I2C buss. The second sensor has a separate address. Adafruit example code works great with one sensor but I cannot figure out how to add the required code to read from the second address. Not A C programmer and several days of trial and error and reading have not yielded anything except error codes!

I will be connecting a total of (4) IR sensors to this bus if that is important.

Any help would be greatly appreciated . . .

Ken

Three sections below separated by "/code"

  • running code? (not sure what the terminology is)
  • source code
  • header file

/code

#include <Wire.h>
#include "Adafruit_TMP007.h"

// Connect VCC to +3V (its a quieter supply than the 5V supply on an Arduino
// Gnd -> Gnd
// SCL connects to the I2C clock pin. On newer boards this is labeled with SCL
// otherwise, on the Uno, this is A5 on the Mega it is 21 and on the Leonardo/Micro digital 3
// SDA connects to the I2C data pin. On newer boards this is labeled with SDA
// otherwise, on the Uno, this is A4 on the Mega it is 20 and on the Leonardo/Micro digital 2

Adafruit_TMP007 tmp007;
//Adafruit_TMP007 tmp007(0x41); // start with a diferent i2c address!

void setup() {
Serial.begin(9600);
Serial.println("Adafruit TMP007 example");

// you can also use tmp007.begin(TMP007_CFG_1SAMPLE) or 2SAMPLE/4SAMPLE/8SAMPLE to have
// lower precision, higher rate sampling. default is TMP007_CFG_16SAMPLE which takes
// 4 seconds per reading (16 samples)
if (! tmp007.begin()) {
Serial.println("No sensor found");
while (1);
}
}

void loop() {
float objt = tmp007.readObjTempC();
Serial.print("Object Temperature: "); Serial.print(objt); Serial.println("*C");
float diet = tmp007.readDieTempC();
Serial.print("Die Temperature: "); Serial.print(diet); Serial.println("*C");

delay(1100); // 1.1 seconds per reading for 4 samples per reading
}

/code

#include "Adafruit_TMP007.h"

#if !defined(ARDUINO_ARCH_SAM) && !defined(ARDUINO_ARCH_SAMD) && !defined(ESP8266)
#include <util/delay.h>
#endif
//#define TESTDIE 0x0C78
//#define TESTVOLT 0xFEED

Adafruit_TMP007::Adafruit_TMP007(uint8_t i2caddr) {
_addr = i2caddr;
}

boolean Adafruit_TMP007::begin(uint8_t samplerate) {
Wire.begin();

write16(TMP007_CONFIG, TMP007_CFG_MODEON | TMP007_CFG_ALERTEN |
TMP007_CFG_TRANSC | samplerate);
write16(TMP007_STATMASK, TMP007_STAT_ALERTEN |TMP007_STAT_CRTEN);
// enable conversion ready alert

uint16_t did;
did = read16(TMP007_DEVID);
#ifdef TMP007_DEBUG
Serial.print("did = 0x"); Serial.println(did, HEX);
#endif
if (did != 0x78) return false;
return true;
}

//////////////////////////////////////////////////////

double Adafruit_TMP007::readDieTempC(void) {
double Tdie = readRawDieTemperature();
Tdie *= 0.03125; // convert to celsius
#ifdef TMP007_DEBUG
Serial.print("Tdie = "); Serial.print(Tdie); Serial.println(" C");
#endif
return Tdie;
}

double Adafruit_TMP007::readObjTempC(void) {
int16_t raw = read16(TMP007_TOBJ);
// invalid
if (raw & 0x1) return NAN;
raw >>=2;

double Tobj = raw;
Tobj *= 0.03125; // convert to celsius
#ifdef TMP007_DEBUG
Serial.print("Tobj = "); Serial.print(Tobj); Serial.println(" C");
#endif
return Tobj;
}

int16_t Adafruit_TMP007::readRawDieTemperature(void) {
int16_t raw = read16(TMP007_TDIE);

#if TMP007_DEBUG == 1

#ifdef TESTDIE
raw = TESTDIE;
#endif

Serial.print("Raw Tambient: 0x"); Serial.print (raw, HEX);

float v = raw/4;
v *= 0.03125;
Serial.print(" ("); Serial.print(v); Serial.println(" *C)");
#endif
raw >>= 2;
return raw;
}

int16_t Adafruit_TMP007::readRawVoltage(void) {
int16_t raw;

raw = read16(TMP007_VOBJ);

#if TMP007_DEBUG == 1

#ifdef TESTVOLT
raw = TESTVOLT;
#endif

Serial.print("Raw voltage: 0x"); Serial.print (raw, HEX);
float v = raw;
v *= 156.25;
v /= 1000;
Serial.print(" ("); Serial.print(v); Serial.println(" uV)");
#endif
return raw;
}

/*********************************************************************/

uint16_t Adafruit_TMP007::read16(uint8_t a) {
uint16_t ret;

Wire.beginTransmission(_addr); // start transmission to device
#if (ARDUINO >= 100)
Wire.write(a); // sends register address to read from
#else
Wire.send(a); // sends register address to read from
#endif
Wire.endTransmission(); // end transmission

Wire.beginTransmission(_addr); // start transmission to device
Wire.requestFrom(_addr, (uint8_t)2);// send data n-bytes read
#if (ARDUINO >= 100)
ret = Wire.read(); // receive DATA
ret <<= 8;
ret |= Wire.read(); // receive DATA
#else
ret = Wire.receive(); // receive DATA
ret <<= 8;
ret |= Wire.receive(); // receive DATA
#endif
Wire.endTransmission(); // end transmission

return ret;
}

void Adafruit_TMP007::write16(uint8_t a, uint16_t d) {
Wire.beginTransmission(_addr); // start transmission to device
#if (ARDUINO >= 100)
Wire.write(a); // sends register address to read from
Wire.write(d>>8); // write data
Wire.write(d); // write data
#else
Wire.send(a); // sends register address to read from
Wire.send(d>>8); // write data
Wire.send(d); // write data
#endif
Wire.endTransmission(); // end transmission
}

/code

/code

#if (ARDUINO >= 100)
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "Wire.h"

// uncomment for debugging!
//#define TMP007_DEBUG 1

#define TMP007_VOBJ 0x00
#define TMP007_TDIE 0x01
#define TMP007_CONFIG 0x02
#define TMP007_TOBJ 0x03
#define TMP007_STATUS 0x04
#define TMP007_STATMASK 0x05

#define TMP007_CFG_RESET 0x8000
#define TMP007_CFG_MODEON 0x1000
#define TMP007_CFG_1SAMPLE 0x0000
#define TMP007_CFG_2SAMPLE 0x0200
#define TMP007_CFG_4SAMPLE 0x0400
#define TMP007_CFG_8SAMPLE 0x0600
#define TMP007_CFG_16SAMPLE 0x0800
#define TMP007_CFG_ALERTEN 0x0100
#define TMP007_CFG_ALERTF 0x0080
#define TMP007_CFG_TRANSC 0x0040

#define TMP007_STAT_ALERTEN 0x8000
#define TMP007_STAT_CRTEN 0x4000

#define TMP007_I2CADDR 0x40
#define TMP007_DEVID 0x1F

class Adafruit_TMP007 {
public:
Adafruit_TMP007(uint8_t addr = TMP007_I2CADDR);
boolean begin(uint8_t samplerate = TMP007_CFG_4SAMPLE); // by default go highres

int16_t readRawDieTemperature(void);
int16_t readRawVoltage(void);
double readObjTempC(void);
double readDieTempC(void);

private:
uint8_t _addr;
uint16_t read16(uint8_t addr);
void write16(uint8_t addr, uint16_t data);
};

/code

Here is the modified code as you suggest with the compilation error. Not sure if I did it correctly.

Code: [Adafruit_TMP007 tmp007;
Adafruit_TMP007 1tmp007(0x41); // start with a diferent i2c address!

void setup() {
Serial.begin(9600);
Serial.println("Adafruit TMP007 example");

// you can also use tmp007.begin(TMP007_CFG_1SAMPLE) or 2SAMPLE/4SAMPLE/8SAMPLE to have
// lower precision, higher rate sampling. default is TMP007_CFG_16SAMPLE which takes
// 4 seconds per reading (16 samples)
if (! tmp007.begin()) {
Serial.println("No sensor found");
while (1);
}
}

void loop() {
float objt = tmp007.readObjTempC();
Serial.print("Object Temperature: "); Serial.print(objt); Serial.println("*C");
float diet = tmp007.readDieTempC();
Serial.print("Die Temperature: "); Serial.print(diet); Serial.println("*C");

delay(1100); // 1.1 seconds per reading for 4 samples per reading

float objt1 = 1tmp007.readObjTempC();
Serial.print("Object Temperature 1: "); Serial.print(objt1); Serial.println("*C");
float diet1 = 1tmp007.readDieTempC();
Serial.print("Die Temperature 1: "); Serial.print(diet1); Serial.println("*C");

delay(1100); // 1.1 seconds per reading for 4 samples per reading
}

Arduino: 1.6.5 (Windows 8.1), Board: "Adafruit Feather M0 (Native USB Port)"

tmp007:27: error: expected unqualified-id before numeric constant
tmp007.ino: In function 'void loop()':
tmp007:51: error: unable to find numeric literal operator 'operator"" tmp007.readObjTempC'
tmp007:53: error: unable to find numeric literal operator 'operator"" tmp007.readDieTempC'
expected unqualified-id before numeric constant

]

Yes, I caught that after I posted . . . . all works fine now . . . such a simple solution when you have experts to rely on.

Thanks Again,

Ken