Temperature Sensor

Description: Measuring Temperature using the TMP102

Tutorial Level: BEGINNER

In this tutorial, we will use an Arduino and a TMP102 temperature sensor to measure the ambient temperature in a room. The tutorial's goals are two fold. The first is to show an example temperature sensing node. The second is to demonstrate the Arduino as an I2C interface for ROS. The TMP102 is one of many pieces of hardware that use I2C to communicate. You can now interface SPI/I2C to ROS with ease using an Arduino.

Hardware

For this tutorial, you will need an Arduino and a TMP102 Breakout board from Sparkfun. This board is a great little temperature sensor that can measure temperature with a 0.625 degree C resolution. Connecting it to an Arduino is very straightforward. It is a 3.3V sensor communication over I2C so it connects to the Arduino's 3.3V output, GND, SDA, SCL pins. SDA and SCL are the I2C data line and I2C clock line respectively. They are exposed on the Arduino board as analog pins 4 and 5.

tmp102.png

Diagram from here

Code

   1 #include <WProgram.h>
   2 #include <Wire.h>
   3 #include <ros.h>
   4 #include <std_msgs/Float32.h>
   5 
   6 
   7 //Set up the ros node and publisher
   8 std_msgs::Float32 temp_msg;
   9 ros::Publisher pub_temp("temperature", &temp_msg);
  10 ros::NodeHandle nh;
  11 
  12 int sensorAddress = 0x91 >> 1;  // From datasheet sensor address is 0x91
  13                                 // shift the address 1 bit right,
  14                                  //the Wire library only needs the 7
  15                                 // most significant bits for the address
  16 
  17 void setup()
  18 {
  19   Wire.begin();        // join i2c bus (address optional for master)
  20 
  21   nh.initNode();
  22   nh.advertise(pub_temp);
  23 
  24 }
  25 
  26 long publisher_timer;
  27 
  28 void loop()
  29 {
  30 
  31   if (millis() > publisher_timer) {
  32   // step 1: request reading from sensor
  33     Wire.requestFrom(sensorAddress,2);
  34     delay(10);
  35     if (2 <= Wire.available())  // if two bytes were received
  36     {
  37       byte msb;
  38       byte lsb;
  39       int temperature;
  40 
  41       msb = Wire.receive();  // receive high byte (full degrees)
  42       lsb = Wire.receive();  // receive low byte (fraction degrees)
  43       temperature = ((msb) << 4);  // MSB
  44       temperature |= (lsb >> 4);   // LSB
  45 
  46       temp_msg.data = temperature*0.0625;
  47       pub_temp.publish(&temp_msg);
  48     }
  49 
  50   publisher_timer = millis() + 1000; //publish once a second
  51   }
  52 
  53   nh.spinOnce();
  54 }

The special bit of code in this example is the use of Arduino's Wire library. Wire is a I2C library that simplifies reading and writing to the I2C bus.

Testing

roscore

rosrun rosserial_python serial_node.py /dev/ttyUSB0

rostopic echo temperature

Wiki: rosserial_arduino/Tutorials/Measuring Temperature (last edited 2011-09-19 22:03:23 by hardiksharma)