LM95172 is a 13 to 16 bits wide temperature range automotive-grade temperature sensor in a ceramic SOIC package. Besides its wide range temperature measurements capability (-40 Celsius to 200 Celsius), it can also be programmed to trigger over-temperature/under-temperature alarms and perform one-shot temperature conversation for low power applications.
In this post, I will go over the Arduino library I created for interfacing with the LM95172 sensor. You can download the library source code towards the end of the post. The library code has been compiled and tested using Arduino 1.0.1 IDE.
The following code snippet shows the minimum coding required for using this library with Arduino. By default the temperature readings returned is in Fahrenheit (it can be configured to return Celsius) and has 13 bits of resolution.
#include <LM95172.h> //CS Pin 2 //CLK Pin 3 //SIO Pin 4 LM95172 sensor(2, 3, 4); void setup() { Serial.begin(9600); sensor.init(); } void loop() { Serial.println(sensor.getTempReading()); delay(500); }
Parameters (e.g. unit, resolution, etc.) can be changed on-the-fly with the functions below. The library exposes all functionalities LM95172 provides and here is a list of the available function calls:
LM95172(int pinCS, int pinClk, int pinSIO)
Initializes an instance of the LM95172 sensor. pinCS/pinClk/pinSIO are the designated chip select/clock/serial input out put pins.
The chip is initialized to its default resolution (13 bits) which can be changed later via changeResolution function call.
init(), init(int theUnit)
Initializes the sensor. The optional parameter can be passed in to indicate whether the temperature reading is in Fahrenheit or Celsius.
This function must be called prior to retrieving the temperature reading, as according to the datasheet the device must be properly reset and brought to a well known state before proper operation. While it can be called many times to restore the device settings to default, only one time during the initial setup (i.e. within the setup function) is necessary.
e.g.
init(); // initialize, default to Fahrenheit init(LM95172::CELSIUS); // initialize to Celsius
resetSensor()
This function is called during init() and does not need to be called elsewhere in the user program.
changeResolution(byte cs)
Changes the temperature resolution. This function can be called at any time after the initialization of the device and will take effect in all subsequent readings until the resolution is changed again.
The valid resolution range is 13 to 16. If an out-of-bound value is provided, the resolution is defaulted to 16-bit.
e.g.
changeResolution(16) // changes the resolution to 16 bits.
getTempC(), getTempF()
These two functions return the conversion results in either Celsius or Fahrenheit explicitly. They should not be called directly for temperature readings. Use getTempReading to to obtain the current temperature depending on the unit setting.
The CS line is not toggled in these two functions, and you will need to toggle the CS manually if you intend to use these two functions directly.
getTempReading()
Returns the temperature reading in the unit defined during init. If no parameter is supplied during initialization, Fahrenheit will be used as the default unit.
setTripTemperatureC(double tLow, double tHigh)
Sets the temperature alarm trip temperature (Celsius). The trip point is tHigh and hysteresis is set to tLow.
getTripTemperatureC(double& tLow, double& tHigh)
Gets the trip temperatures previously set. By default (POR), tLow = 140 C and tHigh = 145. tLow and tHigh are both passed by reference and their contents will reflect the current settings (Celsius) upon the completion of the function call.
enableOneShot(boolean enabled)
When enabled, the sensor will return to sleep mode once a temperature reading has been obtained. This feature further reduces power consumption and is useful when dealing with battery powered devices.
sendCmd(byte reg, int rw, unsigned int &val)
This function handles the rudimentary communications with LM95127 and is used by other functions throughout the library. Besides using the functions provided by this library, you can also manipulate LM95172’s registers directly.
Since all commands must be initiated by a read from the temperature register, you will need to manually toggle the CS line when using sendCmd directly in order for the multi-byte command to be sent.
Here is an example of reading the manufacturer/product/step ID register:
unsigned int r = 0; digitalWrite(PIN_CS, LOW); //select the chip sensor.getTempC(); sensor.sendCmd(LM95172::REG_ID, LM95172::READ, r); digitalWrite(PIN_CS, HIGH); //deselect the chipThe code above will return 0x8030 (or 32816) which is the manufacturer/product and step ID.