A more elegant way to read multiple TMP275 sensors via I2C

Hi,

Wondering if I might ask for some help making this code a bit less clumbsy :slight_smile:

Basically I have 8 TMP275 http://www.ti.com/lit/ds/symlink/tmp275.pdf Sensors each with its own I2C address (48 to 4F) and I am trying to read each in turn and then powering the arduino down to sleep for 5 mins before repeating - my function to read the TMPs is looking pretty bad so hoping there is a much nicer way....

The configuration register I am using hopfully sets ShutDown mode to save battery, 12 Bit mode for best resolution and one shot mode - ie 11100001

How would I best polish this bit of old rusty code into somrthing a bit less emabrassing?

#include <Wire.h>;

char tempHighByte;
char tempLowByte;
float temperature1;
float temperature2;

void setup()

{  
Serial.begin(9600);
Wire.begin();

Serial.println("Starting.....");

}

void loop(){
  
Wire.beginTransmission(0x48);
  Wire.write(1);
  Wire.write(B11100001);
  Wire.endTransmission();
  Wire.beginTransmission(0x48);
  Wire.write(0);
  Wire.endTransmission();
  Wire.requestFrom(0x48,2);
  delay(250);
  tempHighByte = Wire.read();
  tempLowByte  = Wire.read();
  int t = word(tempHighByte,tempLowByte)/16;
  temperature1=t/16.0;
Serial.println(temperature11,4);
delay(1000);

Wire.beginTransmission(0x49);
  Wire.write(1);
  Wire.write(B11100001);
  Wire.endTransmission();
  Wire.beginTransmission(0x49);
  Wire.write(0);
  Wire.endTransmission();
  Wire.requestFrom(0x49,2);
  delay(250);
  tempHighByte = Wire.read();
  tempLowByte  = Wire.read();
  int t = word(tempHighByte,tempLowByte)/16;
  temperature2=t/16.0;
Serial.println(temperature2,4);
delay(1000);

//etc etc for all 8 I2C address


}

Wondering if I might ask for some help making this code a bit less clumbsy :slight_smile:

Sure. Look at these two blocks of code:

Wire.beginTransmission(0x48);
  Wire.write(1);
  Wire.write(B11100001);
  Wire.endTransmission();
  Wire.beginTransmission(0x48);
  Wire.write(0);
  Wire.endTransmission();
  Wire.requestFrom(0x48,2);
  delay(250);
  tempHighByte = Wire.read();
  tempLowByte  = Wire.read();
  int t = word(tempHighByte,tempLowByte)/16;
  temperature1=t/16.0;
Serial.println(temperature11,4);

and

Wire.beginTransmission(0x49);
  Wire.write(1);
  Wire.write(B11100001);
  Wire.endTransmission();
  Wire.beginTransmission(0x49);
  Wire.write(0);
  Wire.endTransmission();
  Wire.requestFrom(0x49,2);
  delay(250);
  tempHighByte = Wire.read();
  tempLowByte  = Wire.read();
  int t = word(tempHighByte,tempLowByte)/16;
  temperature2=t/16.0;
Serial.println(temperature2,4);

The only differences are the address of the sensor. Create a function that takes a sensor address and call it 8 times, instead of copying and pasting the same code 8 times.

The only differences are the address of the sensor.

and the name of the var holding the value :wink:

and the name of the var holding the value

That would still be used to hold the value returned by the function.

Thanks Folks,

I have been trying to get the function below to work by passing the variables in on a function call like: readTMP275(1,0x48); and hopefully having it return a float as the variable temp1 etc.

I think this could work but I expect my code falls down a little, expecially when I try to join the variable 'temp' with 'tmpX' at the end of the function - if this looks like a good option how would I write this to actually work :slight_smile:

void readTMP275(int tmpX, int tmpADD)
{
  Wire.beginTransmission(tmpADD);
  Wire.write(1);
  Wire.write(B11100001);
  Wire.endTransmission();
  Wire.beginTransmission(tmpADD);
  Wire.write(0);
  Wire.endTransmission();
  Wire.requestFrom(tmpADD,2);
  delay(250);
  tempHighByte = Wire.read();
  tempLowByte  = Wire.read();
  int t = word(tempHighByte,tempLowByte)/16;
  temp[tmpX]=t/16.0;
}

most functions return a value that can be assigned to the variable of choice

float readTMP275(int Address)
{
  Wire.beginTransmission(Address);
  Wire.write(1);
  Wire.write(B11100001);
  Wire.endTransmission();

  Wire.beginTransmission(Address);
  Wire.write(0);
  Wire.endTransmission();

  Wire.requestFrom(Address,2);
  delay(250);
  tempHighByte = Wire.read();
  tempLowByte  = Wire.read();
  int t = word(tempHighByte,tempLowByte)/16;
  return t/16.0;
}

...
temp[i] = readTMP275(0x49);

Thanks Rob,

I can see how that works - will give it a pop.

Now I just have to hope I got the I2C call on the TMP275 right :slight_smile:

at least now you have to fix it only in one place :wink:

4 sure :slight_smile: ... I'm slow learning how to speak NiceDuino rather ClunkDuino....