/Arduino-TMP1075

Arduino TMP1075 Library

Primary LanguageC++GNU General Public License v3.0GPL-3.0

Arduino TMP1075 Library

This is a C++ Arduino library for the Texas Instruments TMP1075 temperature sensor. It uses the I²C interface and is intended as an LM75 replacement.

API

Constructor:

  TMP1075(TwoWire &wire, uint8_t i2cAdress = 0x48);

Arguments

  • wire [TwoWire] : The Wire interface used to transmit the data. The interface can be configured before calling begin(), e.g. to support alternative pin configurations.
  • i2cAdress [uint8_t] : The I²C address of the TMP1075 as configured using the A0, A1, A2 pins. The default is 0x48, which is the address when all three pins are tied low.

Methods

Important

Do remember to call the Wire.begin() function before calling any function to enable the I²C bus.

  void begin();

This function calls initialises the TMP1075 library by synchronizing the config register with the device. The I²C bus must be initialised first.

  uint8_t getDeviceId();

Returns

  • [uint8_t] : The sensor device ID. This should be 0x75, if everything is set up correctly.
  int32_t getTemperatureRaw();
  float getTemperatureCelsius();

Returns

  • [int16_t] : The raw temperature data as returned by the sensor. A 16-bit integer that represents the temperature range from –128 °C to 127.9375 °C in steps of 0.0625 K. The 4 LSB are always 0. To convert from raw values to Celsius one needs to calculate value / 16 * 0.0625. Note: Beware of bit shifts, because the return value is a signed integer.
  • [float] : The temperature in units of °C.
  void setConversionMode(const bool isSingleShot=false);

Arguments

  • isSingleShot [bool] : Setting this to true, will put the sensor in a low power state, that requires a call to startConversion()to start a new conversion.
  bool getConversionMode();

Returns

  • [bool] : The conversion mode as described above.
  void startConversion();

Immediately start a new conversion. The result will be available after the conversion time. See setConversionTime().

  void setConversionTime(const ConversionTime value=ConversionTime27_5ms);

Arguments

  • value [ConversionTime] : The conversion time can be one of the following:

    • 27.5 ms
    • 55 ms
    • 110 ms
    • 220 ms

    This can be set by the ConversionTime enum:

     enum ConversionTime : uint8_t {
       ConversionTime27_5ms = 0b00,
       ConversionTime55ms = 0b01,
       ConversionTime110ms = 0b10,
       ConversionTime220ms = 0b11,
     };

    The default is 27.5 ms. The same value as after resetting the device.

  ConversionTime getConversionTime();

Returns

  • [ConversionTime] : The conversion time as described above.
  void setFaultsUntilAlert(const ConsecutiveFaults value=Fault_1);

Arguments

  • value [ConsecutiveFaults] : The number of consecutive faults before the alert pin is triggered. It can be a number between 1-4. This can be set by the ConsecutiveFaults enum:
     enum ConsecutiveFaults : uint8_t {
       Fault_1 = 0b00,
       Fault_2 = 0b01,
       Fault_3 = 0b10,
       Fault_4 = 0b11,
     };
    The default is 1 fault. The same value as after resetting the device.
   ConsecutiveFaults getFaultsUntilAlert();

Returns

  • [ConsecutiveFaults] : The number of consecutive faults until an alert is triggered as described above.
  setAlertPolarity(const bool isHigh=false)

Arguments

  • isHigh [bool] : Setting this to true causes the alert pin to float high (using an external pull-up resistor) in case of an alert. Setting it to false will cause the pin to be pulled low in case of an alert. By default, the alert pin will be pulled low.
  bool getAlertPolarity();

Returns

  • [bool] : Returns whether the alert pin is pulled high (true) or pulled low (false) in case of an alert.
  void setAlertMode(const bool isInterrupt=false);

Arguments

  • isInterrupt [bool] : When set to true, the alert pin is SMBus compatible. Once the alert is active, the device will wait for an SMBus Alert command on the bus, then it will release the pin. If set to false, its pin will stay triggered until the alert condition is gone. By default, the alert pin is not SMBus compatible.
  bool getAlertMode();

Returns

  • [bool] : true if the alert is SMBus compatible and needs clearing, false if the alert stays active while the alert condition is active.
  void setHighTemperatureLimitRaw(const int16_t value=0x5000);
  void setHighTemperatureLimitCelsius(const float value=80.f);

Arguments

  • value [int16_t] : If the temperature goes above this value, an alert will be triggered. The scale is the same as getTemperatureRaw(). To disable the temperature alert, set the high value to 0x7FF0 and the low value to 0x8000. The default (also after reset) is 80 °C (0x5000).
  • value[float] The same as above, but in units of °C. The default (also after reset) is 80 °C.
  void setLowTemperatureLimitRaw(const int16_t value=0x4b00);
  void setLowTemperatureLimitCelsius(const float value=75.f);

This setting acts as a hysteresis. When in comparator mode, the alert, once triggered, will only be released, if the temperature drops below this value. When in interrupt mode, the alert will only trigger again, if the temperature was below this value and then rises above the high threshold. Arguments

  • value [int16_t] : If the temperature goes below this value, the alert will be released. The scale is the same as getTemperatureRaw(). To disable the alert, set the low value to 0x8000 and the high value to 0x7FF0. The default (also after reset) is 75 °C (0x4b00).
  • value[float] The same as above, but in units of °C. The default (also after reset) is 75 °C.
  int16_t getLowTemperatureLimitRaw();
  float getLowTemperatureLimitCelsius();

Returns

  • [int16_t] : See setLowTemperatureLimitRaw().
  • [float] : See setLowTemperatureLimitCelsius().
  int16_t getHighTemperatureLimitRaw();
  float getHighTemperatureLimitCelsius();

Returns

  • [int16_t] : See setHighTemperatureLimitRaw().
  • [float] : See setLowTemperatureLimitCelsius().

Examples

This is an example, which runs on the Arduino platform. The example was tested on an STM32F103RC using Arduino_STM32, but it should work on any Arduino platform.

#include <Arduino.h>  // Some Arduino cores use the arduino namespace. This is addressed in Arduino.h.
#include <Wire.h>    // TwoWires

#include <TMP1075.h>    // Use quotation marks if the library is in the same folder as the sketch
// #include "TMP1075/src/TMP1075.h"    // Use this import, if you copied the library folder into the sketch folder

//TwoWire wire = TwoWire(1, I2C_REMAP, 400*1000);     // Arduino_STM32 use I²C 1 with alternative pin mapping
TwoWire wire = Wire;
TMP1075::TMP1075 tmp1075 = TMP1075::TMP1075(wire);    // The library uses the namespace TMP1075

void setup() {
  Serial.begin(115200);
  wire.begin();  // See definition of wire above
  tmp1075.begin();  // Syncs the config register
}

void loop() {
  // The Teensy and STM32 will lock up, if the input buffer overflows
  while (Serial.available()) {
   Serial.read();
  }
  static unsigned long lastTime = millis();    // Initialize lastTime *once* during the first loop iteration
  if (millis() - lastTime > 1000) {
    lastTime = millis();
    tmp1075.setConversionTime(TMP1075::ConversionTime220ms);
    Serial.print(tmp1075.getTemperatureCelsius());
    Serial.print("\n");
  }
}

Versioning

I use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

License

This project is licensed under the GPL v3 license - see the LICENSE file for details