ESP8266-Arduino编程实例-TMP175数字温度传感器驱动

TMP175数字温度传感器驱动

1、TMP175介绍

TMP175 器件是一款数字温度传感器,非常适合 NTC 和 PTC 热敏电阻替代。 该器件提供 ±1°C 的典型精度,无需校准或外部组件信号调理。 IC 温度传感器具有高度线性,不需要复杂的计算或查找表即可得出温度。 片上 12 位 ADC 提供低至 0.0625°C 的分辨率。

TMP175 具有 SMBus、两线和 I2C 接口兼容性。 TMP175 器件允许在一条总线上连接多达 27 个器件。 . TMP175 具有 SMBus 警报功能。

在这里插入图片描述

TMP175 非常适合在各种通信、计算机、消费类、环境、工业和仪器仪表应用中进行扩展温度测量。

TMP175具有如下特性:

  • TMP175:27 个地址
  • 数字输出:SMBus™、Two-Wire™ 和 I2C
  • 接口兼容性
  • 分辨率:9 到 12 位,用户可选
    • 准确性:
      ±1°C(典型值),从 –40°C 到 125°C
      ±2°C(最大值),从 –40°C 到 125°C
  • 低静态电流:50µA、0.1µA 待机
  • 宽电源范围:2.7V 至 5.5V
  • 小型 8 引脚 MSOP 和 8 引脚 SOIC 封装

2、硬件准备

  • ESP8266 NodeMCU开发板一块
  • TMP175传感器模块一个
  • 面板板一个
  • 杜邦线若干
  • 数据线一条

硬件接线如下:

传感器引脚 ESP8266开发板引脚
Vin 5v
Gnd Gnd
SCL D1
SDA D2

3、软件准备

  • Arduino IDE或VSCode + PlatformIO

在前面的文章中,对如何搭建ESP8266开发环境做了详细的介绍,请参考:

ESP8266 NodeMCU的引脚介绍在前面的文章中做了详细的介绍,请参考:

4、代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <Wire.h> 

byte TempHi; // Variable hold data high byte
byte TempLo; // Variable hold data low byte
boolean P_N; // Bit flag for Positive and Negative
unsigned int Decimal; // Variable hold decimal value

void Cal\_Temp();

void setup()
{
Serial.begin(9600);
Wire.begin(); // join i2c bus (address optional for master)
delay(1000);
}

void loop()
{
const int I2C_address = 0x37; // I2C write address

delay(100);
Wire.beginTransmission(I2C_address);
Wire.write(1); // Setup configuration register
Wire.write(0x60); // 12-bit
Wire.endTransmission();

Wire.beginTransmission(I2C_address);
Wire.write(0); // Setup Pointer Register to 0
Wire.endTransmission();

while (1)
{
delay(1000);

// Read temperature value
Wire.requestFrom(I2C_address, 2);
while(Wire.available()) // Checkf for data from slave
{
TempHi = Wire.read(); // Read temperature high byte
TempLo = Wire.read(); // Read temperature low byte
}
Cal\_Temp ();

// Display temperature
Serial.print("The temperature is ");
if (P_N == 0)
Serial.print("-");
Serial.print(TempHi,DEC);
Serial.print(".");
Serial.print(Decimal,DEC);
Serial.println(" degree C");
}
}

void Cal\_Temp()
{
if (TempHi&0x80) // If bit7 of the TempHi is HIGH then the temperature is negative
P_N = 0;
else // Else the temperature is positive
P_N = 1;

TempHi = TempHi & 0x7F; // Remove sign
TempLo = TempLo & 0xF0; // Filter out last nibble
TempLo = TempLo >>4; // Shift right 4 times
Decimal = TempLo;
Decimal = Decimal \* 625; // Each bit = 0.0625 degree C

}

文章来源: https://iotsmart.blog.csdn.net/article/details/126555272