Skip to content

mrkaleArduinoLib/gbj_tmp102

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gbjTMP102

Library for the temperature sensors TMP102 with two-wire (also known as I2C) bus interface.

  • Sensor can have following addresses, which allow up to 4 sensors present on the same two-wire bus:
    • 0x48 for ADD0 pin connected to GND (ground)
    • 0x49 for ADD0 pin connected to V+ (power supply positive rail)
    • 0x4A for ADD0 pin connected to SDA (serial data rail of the two-wire bus)
    • 0x4B for ADD0 pin connected to SCL (serial clock rail of the two-wire bus)
  • The library provides measured temperature in degrees of Celsius.
  • For conversion among various temperature unit scales and for calculating dew point temperature use library gbjAppHelpers.
  • The sensor in normal mode has 12-bit resolution with sensitivity 0.0625 °C/bit within the measurement range -55 °C ~ +128 °C.
  • The extended mode has 13-bit resolution, but the same sensitivity and it just extends the upper temperature measurement range up to +150 °C.
    • Switching (reconfiguration) to extended mode from normal mode or vice-versa needs a time delay cca 350 milliseconds after it in order to settle the sensor and stabilize the temperature conversion.
    • Without the delay after switching to extended mode the reading is doubled to real temperature at first reading after switching mode.
    • Without the delay after switching to normal mode the reading is halved to real temperature at first reading after switching mode.
    • Library does not have implemented such specific delay after mode switching due to small usefulness of the extended mode.
    • Library caches pointer and configuration register.

Particle hardware configuration

  • Connect microcontroller's pin D0 to sensor's pin SDA (Serial Data).
  • Connect microcontroller's pin D1 to sensor's pin SCL (Serial Clock).

Arduino UNO hardware configuration

  • Connect microcontroller's pin A4 to sensor's pin SDA (Serial Data).
  • Connect microcontroller's pin A5 to sensor's pin SCL (Serial Clock).

Espressif - ESP8266, ESP32 default hardware configuration

  • Connect microcontroller's pin D2 to sensor's pin SDA (Serial Data).
  • Connect microcontroller's pin D1 to sensor's pin SCL (Serial Clock).

Dependency

Particle platform

  • Particle.h: Includes alternative (C++) data type definitions.

Arduino platform

  • Arduino.h: Main include file for the Arduino SDK version greater or equal to 100.
  • inttypes.h: Integer type conversions. This header file includes the exact-width integer definitions and extends them with additional facilities provided by the implementation.
  • TwoWire: I2C system library loaded from the file Wire.h.

Custom Libraries

  • gbjTwoWire: I2C custom library loaded from the file gbj_twowire.h, which provides common bus functionality.

Constants

The library does not have specific error codes. Error codes as well as result code are inherited from the parent library gbjTwoWire only. The result code and error codes can be tested in the operational code with its method getLastResult(), isError() or isSuccess().

Sensor addresses

  • Addresses::ADDRESS_GND: ADD0 pin connected to GND pin (default).
  • Addresses::ADDRESS_VCC: ADD0 pin connected to positive power supply rail.
  • Addresses::ADDRESS_SDA: ADD0 pin connected to serial data rail of two-wire bus.
  • Addresses::ADDRESS_SCL: ADD0 pin connected to serial clock rail of two-wire bus.

Referencing constants

In a sketch the constants can be referenced in following forms:

  • Static constant in the form gbj_tmp102::<enumeration>::<constant> or shortly gbj_tmp102::<constant>, e.g., gbj_tmp102::Addresses::ADDRESS_GND or gbj_tmp102::ADDRESS_GND.
  • Instance constant in the form <object>.<constant>, e.g., sensor.ADDRESS_GND.
gbj_tmp102 sensor = gbj_tmp102(sensor.CLOCK_400KHZ)

Configuration

The configuration of the sensor is realized by the configuration register, which consists of several configuration bits determining its behavior. The library stores (caches) the value of the configuration register in its instance object.

The sensor configuration implemented in the library is based on updating cached configuration value in advanced and finally to send that value to the sensor and write all configuration bits to configuration register at once in order to reduce communication on the two-wire bus in contrast to sending configuration bits to the sensor individually.

It is good practice or sometimes necessary to read the configuration register right before using getters, especially those related to particular configuration bits in order to get its current value.

Interface

Main

Setters

Getters

Other possible setters and getters are inherited from the parent library gbjTwoWire and described there.

gbj_tmp102()

Description

The library does not need special constructor and destructor, so that the inherited ones are good enough and there is no need to define them in the library, just use it with default or specific parameters as defined at constructor of parent library gbjTwoWire.

  • Constructor sets parameters specific to the two-wire bus in general.
  • All the constructor parameters can be changed dynamically with corresponding setters later in a sketch.

Syntax

gbj_tmp102(uint32_t clockSpeed, uint8_t pinSDA, uint8_t pinSCL)

Parameters

  • clockSpeed: Two-wire bus clock frequency in Hertz.

    • Valid values:ClockSpeeds::CLOCK_100KHZ, ClockSpeeds::CLOCK_400KHZ
    • Default value: ClockSpeeds::CLOCK_100KHZ
  • pinSDA: Microcontroller's pin for serial data. It is not a board pin but GPIO number. For hardware two-wire bus platforms it is irrelevant and none of methods utilizes this parameter for such as platforms for communication on the bus. On the other hand, for those platforms the parameters might be utilized for storing some specific attribute in the class instance object.

    • Valid values: positive integer
    • Default value: 4 (GPIO4, D2)
  • pinSCL: Microcontroller's pin for serial clock. It is not a board pin but GPIO number. For hardware two-wire bus platforms it is irrelevant and none of methods utilizes this parameter for such as platforms. On the other hand, for those platforms the parameters might be utilized for storing some specific attribute in the class instance object.

    • Valid values: positive integer
    • Default value: 5 (GPIO5, D1)

Returns

Object performing the sensor management. The constructor cannot return a result or error code directly, however, it stores them in the instance object.

Example

The method has all arguments defaulted and calling without any parameters is equivalent to the calling with all arguments set by corresponding constant with default value:

  gbj_tmp102 sensor = gbj_tmp102(); // It is equivalent to
  gbj_tmp102 sensor = gbj_tmp102(sensor.CLOCK_100KHZ, D2, D1)

Back to interface

begin()

Description

The method takes, sanitizes, and stores sensor parameters to a class instance object and initiates two-wire bus.

  • The method sets parameters specific to the sensor itself.
  • The method resets the sensor to its power-up state.
  • The method resets the sensor by the general call software reset sending the code 0x06 to the two-wire bus at address 0x00 and reads the content of the configuration register to the library instance object.
  • All the method parameters can be changed dynamically with corresponding setters later in a sketch.
  • The method enables to set the sensor's address according to the ADDR pin, if it is wired to a microcontroller's pin.

Syntax

ResultCodes begin(Addresses address)

Parameters

  • address: One of two possible 7 bit addresses of the sensor corresponding to the ADDR pin connection.

Returns

Some of result or error codes.

Example

The method has all arguments defaulted and calling without any parameters is equivalent to the calling with all arguments set by corresponding constant with default value:

gbj_tmp102 sensor = gbj_tmp102();
setup()
{
    sensor.begin();  // It is equivalent to
    sensor.begin(sensor.ADDRESS_GND);
}

Back to interface

reset()

Description

The method resets the sensor by writing power-up value to the configuration register and its cache in the instance object. It causes resetting all internal registers to their power-up values, which determine following configuration and values:

  • Upper alert temperature limit 80 °C.
  • Lower alert temperature limit 75 °C.
  • Normal mode (12 bit).
  • Conversion rate 4 Hz.
  • Continuous power mode (shutdown mode off).
  • Thermostat (comparator) mode.
  • Alert pin active low.
  • Alert active.
  • One fault for fault queue.
  • One-shot conversion off.

Syntax

ResultCodes reset()

Parameters

None

Returns

Some of result or error codes.

See also

begin()

Back to interface

measureTemperature()

Description

The method measures temperature.

  • The method reads the temperature register with value from recent conversion.
  • The continuous mode should be configured before the measurement, if in meanwhile the one-shot mode has been set, but just once.
  • Right after power-up or resetting of the sensor the continuous mode is set by default.

Syntax

float measureTemperature()

Parameters

None

Returns

Temperature in centigrade or erroneous value returned by getErrorValue(). The error code can be tested in the operational code with the method getLastResult(), isError(), or isSuccess().

See also

measureTemperatureOneshot()

Back to interface

measureTemperatureOneshot()

Description

The method configures shutdown mode and one-shot conversion of the sensor. It waits until conversion finishes and returns ambient temperature in centigrade.

  • The method is useful at very long periods (couple of minutes and hours) between measurements in order to save power consumption.

Syntax

float measureTemperatureOneshot()

Parameters

None

Returns

Temperature in centigrade or erroneous value returned by getErrorValue(). The error code can be tested in the operational code with the method getLastResult(), isError(), or isSuccess().

See also

measureTemperature()

Back to interface

setConfiguration()

Description

The method writes the new content of the configuration register stored in the instance object (configuration cache) to the sensor. This content should has been prepared by methods of type configXXX right before.

Syntax

ResultCodes setConfiguration()

Parameters

None

Returns

Some of result or error codes.

See also

getConfiguration()

Back to interface

getConfiguration()

Description

The method reads configuration register and its value stores in the instance object, so that it caches it and enables it for corresponding getters.

Syntax

ResultCodes getConfiguration()

Parameters

None

Returns

Some of result or error codes.

See also

setConfiguration()

Back to interface

setAlertLow(), setAlertHigh()

Description

The particular method writes either lower or upper temperature limit.

  • A method for particular temperature limit does not check its relation to the related limit. It should be done in a sketch. So that, those methods allows to set high limit less than low limit.

Syntax

ResultCodes setAlertLow(float temperatureLow)
ResultCodes setAlertHigh(float temperatureHigh)

Parameters

  • temperatureLow: Low temperature limit in centigrade.

    • Valid values: -55.0 ~ 150.0
    • Default value: 75.0
  • temperatureHigh: Hight temperature limit in centigrade.

    • Valid values: -55.0 ~ 150.0
    • Default value: 80.0

Returns

Some of result or error codes.

See also

getAlertLow(), getAlertHigh()

Back to interface

getAlertLow(), getAlertHigh()

Description

The particular method reads upper or lower temperature limit from the sensor.

Syntax

float getAlertLow()
float getAlertHigh()

Parameters

None

Returns

Lower or upper temperature limit in centigrade or erroneous value returned by getErrorValue(). The error code can be tested in the operational code with the method getLastResult(), isError(), or isSuccess().

See also

setAlertLow(), setAlertHigh()

Back to interface

setUseValuesTyp(), setUseValuesMax()

Description

The particular method sets the internal flag whether typical or maximal values from the datasheet should be used regarding conversion time.

Syntax

void setUseValuesTyp()
void setUseValuesMax()

Parameters

None

Returns

None

Back to interface

configAlertActiveLow(), configAlertActiveHigh()

Description

The particular method updates alert activity bit state in the cached configuration value before its sending to the sensor by the method setConfiguration().

Syntax

void configAlertActiveLow()
void configAlertActiveHigh()

Parameters

None

Returns

None

See also

getAlertActiveLow(), getAlertActiveHigh()

setConfiguration()

Back to interface

getAlertActiveLow(), getAlertActiveHigh()

Description

The particular method determines flag about alert activity mode from the cached configuration value.

Syntax

bool getAlertActiveLow()
bool getAlertActiveHigh()

Parameters

None

Returns

Flag about set particular alert activity mode.

See also

configAlertActiveLow(), configAlertActiveHigh()

getConfiguration()

Back to interface

getAlert()

Description

The method provides flag about state of alert pin from cached configuration value.

  • It is suitable for detecting the alert by software without need of hardware sensing the ALERT pin of the sensor.

Syntax

bool getAlert()

Parameters

None

Returns

Flag about ALERT pin state.

See also

getConfiguration()

Back to interface

configExtendedMode(), configNormalMode()

Description

The particular method turns on corresponding resolution mode in the cached configuration value before its sending to the sensor by the method setConfiguration().

  • At normal mode the resolution is the 12-bit resolution.
  • At extended mode the resolution is the 13-bit resolution.
  • Extended mode does not increase sensitivity, just extents the upper temperature measurement range from +128 to +150 centigrades. So that in normal working conditions it is not very useful.
  • After changing resolution mode and writing it to the sensor it is needed to wait cca 350 milliseconds in order to settle the sensor and provide conversion. Otherwise the first conversion after changing resolution to extended mode from normal one doubles the measured temperature and after changing to normal mode from extended one halves the temperature, which might confuse follow-up logic or controlling mechanizm.
  • The library does not have extra delay after resolution change implemented, so that it must be enforced in a sketch.

Syntax

void configExtendedMode()
void configNormalMode()

Parameters

None

Returns

None

See also

getExtendedMode(), getNormalMode()

setConfiguration()

Back to interface

getExtendedMode(), getNormalMode()

Description

The particular method determines flag about resolution mode state from the cached configuration value.

Syntax

bool getExtendedMode()
bool getNormalMode()

Parameters

None

Returns

Flag about set particular resolution mode.

See also

configExtendedMode(), configNormalMode()

getConfiguration()

Back to interface

configShutdownMode(), configContinuousMode()

Description

The particular method turns on corresponding power mode in the cached configuration value before its sending to the sensor by the method setConfiguration().

  • At shutdown mode the sensor turns on all its system except the serial interface and reduces power consumption. This mode is utilized by the method measurementTemperatureOneshot() for one-shot temperature measurement.
  • At continuous mode the sensor performs continuous temperature conversion according to its current conversion rate.

Syntax

void configShutdownMode()
void configContinuousMode()

Parameters

None

Returns

None

See also

getShutdownMode(), getContinuousMode()

setConfiguration()

Back to interface

getShutdownMode(), getContinuousMode()

Description

The particular method determines flag about power mode state from the cached configuration value.

Syntax

bool getShutdownMode()
bool getContinuousMode()

Parameters

None

Returns

Flag about set particular power mode.

See also

configShutdownMode(), configContinuousMode()

getConfiguration()

Back to interface

configInterruptMode(), configThermostatMode()

Description

The particular method turns on corresponding action mode in the cached configuration value before its sending to the sensor by the method setConfiguration().

  • At interruption mode the sensor generates a short impulse on ALERT pin at reaching particular temperature limit with particular polarity according to the alert activity mode. The impulse is active until next reading a sensor's register, usually the temperature one.
  • At termostat mode the sensor changes state of ALERT pin at reaching a temperature limit with particular polarity according to the alert activity mode and keeps it until reaching another temperature limit.

Syntax

void configInterruptMode()
void configThermostatMode()

Parameters

None

Returns

None

See also

getInterruptMode(), getThermostatMode()

setConfiguration()

Back to interface

getInterruptMode(), getThermostatMode()

Description

The particular method determines flag about action mode state from the cached configuration value.

Syntax

bool getInterruptMode()
bool getThermostatMode()

Parameters

None

Returns

Flag about set particular action mode.

See also

configInterruptMode(), configThermostatMode()

getConfiguration()

Back to interface

configOneshotMode()

Description

The method turns on one-shot temperature measurement mode in the cached configuration value before its sending to the sensor by the method setConfiguration().

Syntax

void configOneshotMode()

Parameters

None

Returns

None

See also

getOneshotMode()

setConfiguration()

Back to interface

getOneshotMode()

Description

The method provides current consecutive faults in form of value of pair of fault queue bits from the cached configuration value. That value can be compared to corresponding library class constants in order to determine number of consecutive faults.

Syntax

bool getOneshotMode()

Parameters

None

Returns

Flag about set one-shot measurement mode.

See also

configOneshotMode()

getConfiguration()

Back to interface

configConversionRate_025hz(), configConversionRate_1hz(), configConversionRate_4hz(), configConversionRate_8hz()

Description

The particular method sets conversion rate bits in the cached configuration value before its sending to the sensor by the method setConfiguration().

  • The rate is determined by desired conversion frequency in Hertz denoted in the method's name.

Syntax

void configConversionRate_025hz()
void configConversionRate_1hz()
void configConversionRate_4hz()
void configConversionRate_8hz()

Parameters

None

Returns

None

See also

getConversionRate()

setConfiguration()

Back to interface

getConversionRate()

Description

The method provides current conversion rate as a frequency in Hertz based on conversion rate bits from the cached configuration value.

Syntax

float getConversionRate()

Parameters

None

Returns

Conversion frequency in Hertz.

See also

configConversionRate_025hz(), configConversionRate_1hz(), configConversionRate_4hz(), configConversionRate_8hz()

getConfiguration()

Back to interface

configConversionPeriod_4000ms(), configConversionPeriod_1000ms(), configConversionPeriod_250ms(), configConversionPeriod_125ms()

Description

The particular method sets conversion rate bits in the cached configuration value before its sending to the sensor by the method setConfiguration().

  • The rate is determined by desired conversion time period in milliseconds denoted in the method's name.
  • The particular method set the corresponding conversion frequency as a reciprocal value.

Syntax

void configConversionPeriod_4000ms()
void configConversionPeriod_1000ms()
void configConversionPeriod_250ms()
void configConversionPeriod_125ms()

Parameters

None

Returns

None

See also

getConversionPeriod()

setConfiguration()

Back to interface

getConversionPeriod()

Description

The method provides current conversion rate as a time period in milliseconds based on conversion rate bits from the cached configuration value.

Syntax

uint16_t getConversionPeriod()

Parameters

None

Returns

Conversion time period in milliseconds.

See also

configConversionPeriod_4000ms(), configConversionPeriod_1000ms(), configConversionPeriod_250ms(), configConversionPeriod_125ms()

getConfiguration()

Back to interface

configFaults1(), configFaults2(), configFaults4(), configFaults6()

Description

The method sets fault queue bits in the cached configuration value before its sending to the sensor by the method setConfiguration().

  • The number of faults is determined by a digit in the method's name.
  • The number determines a number of consecutive faults (breaking a particular temperature limit) to activate particular alert. That number eliminate a environmental noise.

Syntax

void configFaults1()
void configFaults2()
void configFaults4()
void configFaults6()

Parameters

None

Returns

None

See also

getFaults()

setConfiguration()

Back to interface

getFaults()

Description

The method provides current number of consecutive faults based on fault queue bits from the cached configuration value.

Syntax

uint8_t getFaults()

Parameters

None

Returns

Number of conscutive faults for a temperature alert activation.

See also

configFaults1(), configFaults2(), configFaults4(), configFaults6()

getConfiguration()

Back to interface

getErrorValue()

Description

The method returns virtually wrong temperature value at erroneous measurement usually at failure of two-wire bus.

Syntax

float getErrorValue()

Parameters

None

Returns

Erroneous temperature value. The error code can be tested in the operational code with the method getLastResult(), isError(), or isSuccess().

See also

measureTemperature()

measureTemperatureOneshot()

Back to interface

About

Library for temperature sensor TMP102 on two-wire (I2C) bus.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages