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

TMP102数字温度传感器驱动

1、TMP102介绍

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

在这里插入图片描述

TMP102 器件具有 SMBus™、两线和 I2C 接口兼容性,并允许在一条总线上最多四个器件。 该器件还具有 SMBus 警报功能。 该器件规定在 1.4 至 3.6 V 的电源电压下工作,在整个工作范围内最大静态电流为 10 µA。

TMP102 器件非常适合在各种通信、计算机、消费类、环境、工业和仪器仪表应用中进行扩展温度测量。 该器件的额定工作温度范围为 –40°C 至 125°C。

2、硬件准备

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

硬件接线如下:

传感器引脚 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
70
71
72
73
74
#include "Wire.h"
#define TMP102\_I2C\_ADDRESS 72 /\* This is the I2C address for our chip. This value is correct if you tie the ADD0 pin to ground. See the datasheet for some other values. \*/


void setup()
{
Wire.begin(); // start the I2C library
Serial.begin(115200); //Start serial communication at 115200 baud
}


void loop()
{
getTemp102();
delay(5000); //wait 5 seconds before printing our next set of readings.
}

void getTemp102()
{
byte firstbyte, secondbyte; //these are the bytes we read from the TMP102 temperature registers
int val; /\* an int is capable of storing two bytes, this is where we "chuck" the two bytes together. \*/
float convertedtemp; /\* We then need to multiply our two bytes by a scaling factor, mentioned in the datasheet. \*/
float correctedtemp;

/\* Reset the register pointer (by default it is ready to read temperatures)
You can alter it to a writeable register and alter some of the configuration -
the sensor is capable of alerting you if the temperature is above or below a specified threshold. \*/

Wire.beginTransmission(TMP102_I2C_ADDRESS); //Say hi to the sensor.
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(TMP102_I2C_ADDRESS, 2);
Wire.endTransmission();


firstbyte = (Wire.read());
/\*read the TMP102 datasheet - here we read one byte from
each of the temperature registers on the TMP102\*/
secondbyte = (Wire.read());
/\*The first byte contains the most significant bits, and
the second the less significant \*/
val = firstbyte;
if ((firstbyte & 0x80) > 0)
{
val |= 0x0F00;
}
val <<= 4;
/\* MSB \*/
val |= (secondbyte >> 4);
/\* LSB is ORed into the second 4 bits of our byte.
Bitwise maths is a bit funky, but there's a good tutorial on the playground\*/
convertedtemp = val\*0.0625;
correctedtemp = convertedtemp - 5;
/\* See the above note on overreading \*/


Serial.print("firstbyte is ");
Serial.print("\t");
Serial.println(firstbyte, BIN);
Serial.print("secondbyte is ");
Serial.print("\t");
Serial.println(secondbyte, BIN);
Serial.print("Concatenated byte is ");
Serial.print("\t");
Serial.println(val, BIN);
Serial.print("Converted temp is ");
Serial.print("\t");
Serial.println(val\*0.0625);
Serial.print("Corrected temp is ");
Serial.print("\t");
Serial.println(correctedtemp);
Serial.println();
}

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