Print this Page

TMP102 Temperature Sensor Breakout Board

TMP102 Breakout Board → Temperature Sensor

The TMP102 is an ambient temperature sensor capable of detecting .0625ºC changes between -25 and +85°C, with an accuracy of 0.5°C. It does all of this while only consuming 10µA. The breakout board makes it easier to connect the sensor to a breadboard and wire it up for different applications.The TMP102 is an I2C device, so it will actually tell you the temperature, not send an analog signal that you then need to interpret.

To use the TMP102 temperature sensor we will connect it to the Gertboard and program the Atmega chip using the Arduino IDE, in a similar way to how we did for the servo motors. The diagram is the one of Figure above, which we will need to adjust to use with the Gertboard rather that Arduino. The easiest way to connect them is to attach the TMP102 on a breadboard and then make connections on the breadboard (see the beginning of this video) http://www.youtube.com/watch?v=UYeKRngfAmI). You will need to have the connections done for programming the ATmega chip through SPI interface, as seen in Figure 4 from the servo motors tutorial (see http://blog.soton.ac.uk/pi/servo-motors/). You will also need to have two jumpers installed for serial connection, as seen in Figure 6 from the servo motor tutorial. Now you can make the connections above: 3V3 (from the top left of the Gertboard) to Vcc on the Temp Sensor. GND (from the pins on the right column of jumper J25 located on the left side of the Gertboard) to GND and ADD0 on the TMP102. Finally Analog pin 4 (PC4 on J25) to SDA and analog pin 5 (PC5 on J25) to SCL.

In the code, we will need the Wire library for communicating between the Temperature Sensor and the Gertboard. The address pin (ADD0) is used to change the address the sensor is located at. This is useful if you need more than one of these connected to the same device and still call them independently even on the same bus. We are grounding this pin so that the sensor will use the address of 72 (0×48 in hex). As you can see, we are not using the Alert pin so it is left unconnected.

Once all the connections have been done, copy the following code into the Arduino IDE and upload it onto the Atmega on the Gertboard. See http://blog.soton.ac.uk/pi/servo-motors/for more details on how to do this. To view the temperature, open a terminal and then open minicom by typing sudo minicom ama0. The temperature will start displaying on the screen, updating at the interval given by the delay.

#include <Wire.h>
int tmp102Address = 0x48;

void setup(){
  Serial.begin(9600); //Set up serial connection at 9600 baud rate
  Wire.begin(); //Initialise the wire library
}

void loop(){

  //Call the function which reads the temperature in celsius
  float celsius = getTemperature(); 
  //Displays the temperature on the screen
  Serial.print("Celsius: ");
  Serial.println(celsius);

  //Converts to Fahrenheit and prints the temp in Fahrenheit on the screen
  float fahrenheit = (1.8 * celsius) + 32;  
  Serial.print("Fahrenheit: ");
  Serial.println(fahrenheit);

  delay(200); //Controls the speed at which readings are displayed
}

//Function reading the temp in celsius
float getTemperature(){
  //Request two readings from the temperature sensor, specifying the address to read from and the number of readings
  Wire.requestFrom(tmp102Address,2); 

  //MSB will contain the integer part of the temperature reading
  byte MSB = Wire.read();
  //LSB will contain the decimal part of the temperature reading
  byte LSB = Wire.read();

  //This line maps the MSB and LSB (full explanation below)
  int TemperatureSum = ((MSB << 8) | LSB) >> 4; 

  //Convert the temperature reading to celsius and return it
  float celsius = TemperatureSum*0.0625;
  return celsius;
}

The line int TemperatureSum = ((MSB << 8) | LSB) >> 4; is a bit more complicated and can be explained as follows: It maps the MSB and LSB (e.g. 24 + 0.4 = 24.4) by first extending the MSB to 16bits (MSB << 8). The MSB is a byte (8bit) reading so by shifting it 8 bits to the left we will result with a 16bit number with the last 8 bits being 0. The second step is applying the OR operator which will map the LSB to the last 8 bits of MSB (which are all 0 at the moment). Finally, the TemperatureSum variable is converted into a 12bit number (by the >> 4 operation which shifts the number 4 bits to the left, hence the last 4 bits will be lost), which is the required format of the temperature reading.

http://bildr.org/2011/01/tmp102-arduino/

Permanent link to this article: https://blog.soton.ac.uk/pi/tmp102-temperature-sensor-breakout-board/