Controlling the LM75 temperature sensor on the I2C bus
Alarm Output
For purposes of the next example, you should take a closer look at the LM75 registers. The 0x03h register contains the temperature at which the sensor triggers the alarm. Register 0x02h is set to the temperature level below which the alarm will turn off.
Both of the 16-bit registers are set up like register 0x00h. This means that the low-order byte delivers the temperature measurement as an integer, and the high-order byte delivers the fractional part (0.5). When interpreting the registers as 8 bit, only the integer portion of the value can be read.
There is one more comment that should be made about the value in the 0x02h register: Be sure it is several degrees lower than the value in 0x03h so the fan doesn't switch on and off rapidly (hysteresis).
The following example, in which the registers are interpreted as 8-bit registers, helps to illustrate the concept underlying the program. The 0x01h register specifies the switching behavior of the LM75. Additionally, Table 2 shows the setup for the 0x01h register and the functions encoded by the individual bits.
Table 2
0x01h Register
Bits | Function |
---|---|
D7 to D5 |
Always 0 |
D4 and D3 |
Error counter |
D2 |
OS polarity |
D1 |
OS behavior |
D0 |
Shutdown |
The code that regulates a fan is provided in Listing 3; you should set all bits in register 0x01h to 0 then compile and start the program with:
$ cc lm75control.c -lwiringPi $ ./a.out Temp:21 [...] Temp:23
Listing 3
lm75control.c
#include <wiringPiI2C.h> #include <stdio.h> int main (void) { int tein=26; int taus=23; int handle = wiringPiI2CSetup (0x48) ; wiringPiI2CWriteReg8(handle,0x01,0x00); wiringPiI2CWriteReg8(handle,0x02,taus); wiringPiI2CWriteReg8(handle,0x03,tein); while(1) { int temp=wiringPiI2CReadReg8(handle,0x00); printf("Temp:%d\n",temp); delay(500); } return 0 ; }
The data sheet [6] provides a detailed description of the functionality encoded by the individual bits.
Conclusion
Integrated logic allows the LM75 sensor to work independently. Once programmed, it can regulate a fan automatically according to the temperature measurements or just monitor values. If you experience a power interruption, you will have to reprogram the sensor. If necessary, you can reduce power consumption to 4µA via the shutdown function. All in all, the LM75 is a flexible sensor with lots of potential.
Infos
- "I2C Bus – Basics" by Martin Mohr, Raspberry Pi Geek, issue 9, 2015, pg. 80
- " I2C Bus – Clock Chip" by Martin Mohr, Raspberry Pi Geek, issue 11, 2015, pg. 34
- LM75: http://www.ti.com/product/lm75a
- wiringPi: http://wiringpi.com
- Code for this article: ftp://ftp.linux-magazine.com/pub/listings/raspberry-pi-geek.com/12
- LM75 data sheet: http://www.ti.com/lit/ds/symlink/lm75a.pdf
« Previous 1 2 Next »
Buy this article as PDF
Pages: 4
(incl. VAT)