TMP105 with Arduino Uno

First, log in to your Simuli account and navigate to the Simuli Virtual Lab. Here we will create a new emulated instance of the Arduino Uno. Click on the Launch button under the Arduino to get started.

This will open the configuration menu. First, provide a name for the project. Then, we will add the TMP105 Temperature Sensor by selecting it from the list of available components. Finally, review that the name for the project and the selected sensors are correct and click on the Launch button.

Once we have clicked on the Launch button, a new instance of the Arduino Uno will be created. It can take a few minutes, so be patient.

If you have not set up your Arduino IDE, follow the guide linked below.

pagePrerequisites for Arduino Development

Once you have the Arduino IDE ready to go, copy the code given below and paste it in the IDE.

#include <Wire.h>

class TMP105 {
public:
  bool begin(int addrSel = 0) {
    switch (addrSel) {
    case 0:
      i2cAddr = 0x48;
      break;
    case 1:
      i2cAddr = 0x49;
      break;
    default:
      return false;
    }
    Wire.begin();
    return true;
  }

  bool readTemperature(float *temp) {
    return getTemp(REG_TEMP, temp);
  }

  bool setResolution(int bits) {
    byte cfg;

    if (!getCfg(&cfg)) {
      return false;
    }
    cfg &= ~(0x60);
    switch (bits) {
    case 9:
      break;
    case 10:
      cfg |= 0x20;
      break;
    case 11:
      cfg |= 0x40;
      break;
    case 12:
      cfg |= 0x60;
      break;
    default:
      return false;
    }
    return setCfg(cfg);
  }

  bool getCfg(byte *cfg) {
    Wire.beginTransmission(i2cAddr);
    int size = Wire.write(REG_CFG);
    if (Wire.endTransmission() != 0) {
      return false;
    }
    if (size != 1) {
      return false;
    }
    if (Wire.requestFrom(i2cAddr, 1) != 1) {
      return false;
    }
    *cfg = Wire.read();
    return true;
  }

  bool setCfg(byte cfg) {
    Wire.beginTransmission(i2cAddr);
    int size = Wire.write(REG_CFG);
    if (size == 1) {
      Wire.write(cfg);
    }
    if (Wire.endTransmission() != 0) {
      return false;
    }
    if (size != 1) {
      return false;
    }
    return true;
  }

  bool getTLow(float *temp) {
    return getTemp(REG_TLOW, temp);
  }

  bool setTLow(float temp) {
    return setTemp(REG_TLOW, temp);
  }

  bool getTHigh(float *temp) {
    return getTemp(REG_THIGH, temp);
  }

  bool setTHigh(float temp) {
    return setTemp(REG_THIGH, temp);
  }

private:
  bool getTemp(byte reg, float *temp) {
    Wire.beginTransmission(i2cAddr);
    int size = Wire.write(reg);
    if (Wire.endTransmission() != 0) {
      return false;
    }
    if (size != 1) {
      return false;
    }
    if (Wire.requestFrom(i2cAddr, 2) != 2) {
      return false;
    }
    signed char msb = Wire.read();
    byte lsb = Wire.read();
    *temp = msb + lsb / 256.0;
    return true;
  }

  bool setTemp(byte reg, float val) {
    byte msb, lsb;

    if (val >= 0) {
      msb = (byte)val;
      lsb = (byte)((val - msb) * 256);
    }
    else {
      msb = (byte)(256 + val);
      lsb = (byte)((256 + val - msb) * 256);
    }
    Wire.beginTransmission(i2cAddr);
    int size = Wire.write(reg);
    if (size == 1) {
      Wire.write(msb);
    }
    if (size == 1) {
      Wire.write(lsb);
    }
    if (Wire.endTransmission() != 0) {
      return false;
    }
    if (size != 1) {
      return false;
    }
    return true;
  }

  static const byte REG_TEMP = 0x00;
  static const byte REG_CFG = 0x01;
  static const byte REG_TLOW = 0x02;
  static const byte REG_THIGH = 0x03;
  int i2cAddr;
};

TMP105 tmp105;

void setup() {
  tmp105.begin();
  tmp105.setResolution(12);
  Serial.begin(115200);
}

void loop() {
  float temp;

  if (tmp105.readTemperature(&temp)) {
    Serial.println("Temperature: " + String(temp) + " C");
  }
  else {
    Serial.println("Cannot read temperature!");
  }
  delay(1000);
}

Now save the sketch in a folder and keep a note of that folder. We now need to get the compiled binaries for the sketch. Go to Sketch and find Export Compiled Binary. The program will take some time to compile. Once the compilation is complete, go back to IoTIFY Virtual Lab and open the instance we created.

A new tab will open and we can interact with our Arduino. First, we need to uploadour compiled binary. Click on the Arduino, this will open a file explorer, navigate to where you had stored the Arduino sketch. In the same folder, a file called sketch.ino.standard.hex will be present (where sketch is the name of the Arduino sketch). Select this file and click on open. Now the hex file will be uploaded. Finally, reset the Arduino so that it loads the new file. To reset, just click the red power button above the top left corner of the Arduino. Now you can see the reading of the TMP105 sensor in the terminal. If you change the values on the sensor, it will also be reflected in the terminal.

Now that you know how to get the readings from the TMP105 sensor, you could try to make an alert system, which would turn on LEDs when the temperature goes above a certain threshold. You need an if statement to compare the current temperature value with the threshold and then switch on the LED if the condition is satisfied Keep building and have fun! 🎉

Last updated