Skip to main content

TMP1x2

Latest Version: 1.0.3

This is a driver library for the TMP102 and TMP112 digital temperature sensors. The TMP1x2 class allows you to read the current temperature, as well as configure various interupts.

You can view the library’s source code on GitHub. Click here to see information on the available versions of this library.

To add this library to your project, add #require "TMP1x2.class.nut:1.0.3" to the top of your device code.

Class Usage

Constructor: TMP1x2(i2c[, addr])

To instantiate a new TMP1x2 object, you need to pass in a configured I²C object and an optional I²C address. If no address is supplied, a default address of 0x90 will be used:

#require "TMP1x2.class.nut:1.0.3"

i2c  <- hardware.i2c89;
i2c.configure(CLOCK_SPEED_400_KHZ);

tmp <- TMP1x2(i2c);

Class Methods

read([callback])

The read() method reads and returns the the current temperature in celsius. If a callback is supplied, the read will execute asynchronously and the result will be passed to the callback function — if no callback is supplied, the read will execute synchronously and an object containing the sensor data will be returned.

tmp.read(function(result) {
    if ("err" in result) {
        server.log("Error Reading TMP102: " + result.err);
        return;
    }
    server.log(result.temp + " degrees C");
});

Note If an error occured during the read, an err key will be present in the data — you should always check for the existance of the err key before using the results.

setShutdown(state)

Sets the shutdown mode for the TMP1x2 sensor (1 or 0). When shutdown is set to 1, the tmp1x2 enters a low-power sleep mode. When shutdown is set to 0, the tmp1x2 maintains a continous converstion state.

function goToSleep() {
    // Turn off the tmp1x2 and go to sleep;
    tmp.setShutdown(1);
    imp.onidle(function() { server.sleepfor(3600); });
}

goToSleep();

getShutdown()

Returns the current shutdown mode state (0 or 1).

function onWake() {
    // Wake the tmp1x2 if it's asleep
    if (tmp.getShutdown() == 1) {
        tmp.setShutdown(0);
    }
}

setExtMode(state)

Sets the extended mode state (0 or 1). When extended mode is enabled, the tmp1x2 can read temperatures above 128°C.

// We need really high temperatures..
tmp.setExtMode(1);

getExtMode()

Returns the current extended mode state (1 or 0).

Interrupt Methods

The TMP1x2 class allows you to configure interrupts. However, it does not deal with configuring the interrupt pin, or handling interrupt callbacks. In order to use interrupts, we must do three things:

  • Connect the ALERT pin of the TMP1x2 to a GPIO pin on the imp
  • Configure the DIGITAL_IN pin and handle the interrupt.
  • Configure the interrupt parameters (with setHighThreshold(), setLowThreshold()), setModeComparator(), setModeInterrupt(), setActiveHigh() and setActiveLow())
#require "TMP1x2.class.nut:1.0.3"

// Configure the I2C bus
i2c  <- hardware.i2c89;
i2c.configure(CLOCK_SPEED_400_KHZ);

// Create the TMP1x2 object
tmp <- TMP1x2(i2c);

// Configure the alert pin to go high when temp is > 30C, or < 15C
tmp.setActiveHigh();
tmp.setHighThreshold(30);
tmp.setLowThreshold(15);
tmp.setModeInterrupt();

// Create our interrupt handler
function interruptHandler() {
    // Ignore falling edge case
    if (alert.read() == 0) return;

    // Log the temperature
    tmp.read(function(result) {
        if ("err" in result) {
            server.log("Error Reading TMP102: " + result.err);
            return;
        }
        server.log(result.temp + " degrees C");
    });
}

// Configure the interrupt pin
alert <- hardware.pin1;
alert.configure(DIGITAL_IN, interruptHandler);

setHighThreshold(threshold)

Sets the THigh threshold register (in °C).

See Using Interrupts for more information.

setLowThreshold(threshold)

Sets the TLow threshold register (in °C).

See Using Interrupts for more information.

setModeComparator()

Enables comparator mode. In comparator mode, the Alert pin is activated when the temperature equals or exceeds the value in the THigh register and it remains active until the temperature falls below the value in the TLow register.

See Using Interrupts for more information.

setModeInterrupt()

Enables interrupt mode. In interrupt mode, the Alert pin is activated when the temperature exceeds THigh or goes below TLow. The Alert pin is cleared when the host controller reads the temperature register.

Interrupt Conditions

After a cold boot, only a temperature exceeding THigh will trigger an Alert. Once an Alert has occurred it will be used to determine the conditions for the next event. Neither a warm boot not the installation of new application code will clear the interrupt state. A measured temperature higher than THigh will only trigger an Alert if the previous Alert was triggered by the temperature dropping below TLow, or a cold boot has taken place. The temperature dropping below TLow will only trigger an event if the previous Alert was triggered by the temperature exceeding THigh.

See Using Interrupts for more information.

setActiveLow()

Sets the interrupt pin to be active low.

See Using Interrupts for more information.

setActiveHigh()

Sets the interrupt pin to be active high.

See Using Interrupts for more information.

getHighThreshold()

Returns the THigh threshold in °C.

server.log(tmp.getHighThreshold());

getLowThreshold()

Returns the TLow threshold in °C.

server.log(tmp.getLowThreshold());

Release History

The Electric Imp Dev Center documents the latest version of the library. For past versions, please see the Electric Imp public GitHub repos listed below.

Version Source Code Notes
1.0.0 GitHub Initial release
1.0.1 GitHub Minor code changes
1.0.2 GitHub Minor code changes
1.0.3 GitHub Minor code changes

License

The TMP1x2 library is licensed under the MIT License.