参考:https://playground.arduino.cc/Code/TMP102/

Example Code for a TMP102 I2c Thermometer

I've fairly extensively commented the code, hopefully it makes sense.

Example code for talking to a TMP102 I2C thermometer, like those available from Sparkfun, for example. Rather heavily based on some code I found in the forums, some  samples, and a bit of personal investigation to make it all work.

Based on my testing, the sensor overreads by around 5 degrees C at room temperature. Not too sure if it is a percentage or a constant error. I know some of the analogue sensors are prone to incorrect readings if the reference voltages are off, but as we're using a digital output, this shouldn't affect it.  Any ideas? Mention them here.

For those using the Sparkfun logic converters with I2C, here are the pin connections:

  • Low voltage to the 3.3v pin of the Arduino
  • High voltage to the 5.0v pin
  • High voltage Channel 1 Txo to the SDA pin (analog 4)
  • High voltage Channel 2 Txo to the SCL pin (analog 5)
  • Low voltage Channel 1 Txi to the SDA pin of tmp102
  • Low voltage Channel 2 Txi to the SCL pin of tmp102
  • The RX pins of the logic converter are not used
  • V+ of tmp102 connected to 3.3v
  • ADD0 of tmp102 connected to ground
  • ALT of tmp102 not connected
  • All grounds connected together.

You can instead use resistors to voltage divide the signal lines, but I've not figured out how to do that yet.

  1. #include "Wire.h"
  2. #define TMP102_I2C_ADDRESS 72 /* This is the I2C address for our chip.
  3. This value is correct if you tie the ADD0 pin to ground. See the datasheet for some other values. */
  4.  
  5.  
  6. void setup() {
  7.   Wire.begin(); // start the I2C library
  8.   Serial.begin(115200); //Start serial communication at 115200 baud
  9. }
  10.  
  11.  
  12. void getTemp102(){
  13.   byte firstbyte, secondbyte; //these are the bytes we read from the TMP102 temperature registers
  14.   int val; /* an int is capable of storing two bytes, this is where we "chuck" the two bytes together. */
  15.   float convertedtemp; /* We then need to multiply our two bytes by a scaling factor, mentioned in the datasheet. */
  16.   float correctedtemp;
  17.   // The sensor overreads (?)
  18.  
  19.  
  20.   /* Reset the register pointer (by default it is ready to read temperatures)
  21. You can alter it to a writeable register and alter some of the configuration -
  22. the sensor is capable of alerting you if the temperature is above or below a specified threshold. */
  23.  
  24.   Wire.beginTransmission(TMP102_I2C_ADDRESS); //Say hi to the sensor.
  25.   Wire.send(0x00);
  26.   Wire.endTransmission();
  27.   Wire.requestFrom(TMP102_I2C_ADDRESS, 2);
  28.   Wire.endTransmission();
  29.  
  30.  
  31.   firstbyte      = (Wire.receive());
  32. /*read the TMP102 datasheet - here we read one byte from
  33.  each of the temperature registers on the TMP102*/
  34.   secondbyte     = (Wire.receive());
  35. /*The first byte contains the most significant bits, and
  36.  the second the less significant */
  37.     val = ((firstbyte) << 4);  
  38.  /* MSB */
  39.     val |= (secondbyte >> 4);    
  40. /* LSB is ORed into the second 4 bits of our byte.
  41. Bitwise maths is a bit funky, but there's a good tutorial on the playground*/
  42.     convertedtemp = val*0.0625;
  43.     correctedtemp = convertedtemp - 5;
  44.     /* See the above note on overreading */
  45.  
  46.  
  47.   Serial.print("firstbyte is ");
  48.   Serial.print("\t");
  49.   Serial.println(firstbyte, BIN);
  50.   Serial.print("secondbyte is ");
  51.   Serial.print("\t");
  52.   Serial.println(secondbyte, BIN);
  53.   Serial.print("Concatenated byte is ");
  54.   Serial.print("\t");
  55.   Serial.println(val, BIN);
  56.   Serial.print("Converted temp is ");
  57.   Serial.print("\t");
  58.   Serial.println(val*0.0625);
  59.   Serial.print("Corrected temp is ");
  60.   Serial.print("\t");
  61.   Serial.println(correctedtemp);
  62.   Serial.println();
  63. }
  64.  
  65. void loop() {
  66.   getTemp102();
  67.   delay(5000); //wait 5 seconds before printing our next set of readings.
  68. }

Comments

Hello, thanks too for this code. I noticed that it does not work for negative temperatures now it is getting colder outside ;-)  To my opinion the problem is in line 37. If the highest bit is 1 also the 4 highest bit of the integer have to be set. I replaced this line with:
    val = firstbyte;
if ((firstbyte & 0x80) > 0) {
val |= 0x0F00;
}
val <<= 4;

Example Code for a TMP102 I2c Thermometer————Arduino的更多相关文章

  1. I2C总线的Arduino库函数

    I2C总线的Arduino库函数 I2C即Inter-Integrated Circuit串行总线的缩写,是PHILIPS公司推出的芯片间串行传输总线.它以1根串行数据线(SDA)和1根串行时钟线(S ...

  2. I2C的库函数应用示例

    I2C  Arduino 简单应用举例   例1  多机通信 主机代码:(从编译器串口监视器发送数字1,2,3,4来控制从机的LED亮与灭) 1 #include <Wire.h> 2 v ...

  3. NXP LPC11xx I2C Slave 从机程序

    /**************************************************************************** * $Id:: i2cslave.c 363 ...

  4. 1000多块整个插板,arduino + android 蓝牙插板的实现--屌丝版

       需求描述 儿子有一堆充电玩具,基本上都是锂电池,经常插上去充电忘了到时拔下来,所以需要一块能设置接通时间的插板以保障电池的安全.   硬件设计: 首先需要一块插板,接着需要一个继电器,然后采用a ...

  5. (2)esp8266多国语言翻译系统

    http://bbs.mydigit.cn/simple/?t2649513.html 这个想法不错 原来只是想用esp8266搞一个百度的多国语言翻译系统出来的,只是为了尝试如何调用各种web ap ...

  6. JTAG 引脚自动识别 JTAG Finder, JTAG Pinout Tool, JTAG Pin Finder, JTAG pinout detector, JTAGULATOR, Easy-JTAG, JTAG Enumeration

    JTAG Finder Figuring out the JTAG Pinouts on a Device is usually the most time-consuming and frustra ...

  7. 编写一个Open Live Writer的VSCode代码插件

    起因 又是一年多没有更新过博客了,最近用Arduino做了一点有意思的东西,准备写一篇博客.打开尘封许久的博客园,发现因为Windows Live Writer停止更新,博客园推荐的客户端变为了Ope ...

  8. Arduino I2C + DS1307实时时钟

    主要特性 DS1307是Maxim的串行.I2C实时时钟芯片.主要特性有: 工作电压:主电源电压4.5~5.5V,电池电压2.0~3.5V 功耗:电池供电.备份模式时<500nA 接口:I2C, ...

  9. Arduino I2C + 三轴加速度计LIS3DH

    LIS3DH是ST公司生产的MEMS三轴加速度计芯片,实现运动传感的功能.主要特性有: 宽工作电压范围:1.71 ~ 3.6V 功耗:低功耗模式2μA:正常工作模式.ODR = 50Hz时功耗11μA ...

随机推荐

  1. 【jmespath】—2. 进阶 List and Slice Projections

    Projections翻译的话说实话我也不知道翻成什么比较准确,姑且就叫"投影"吧,不过带来的功能确实非常的强大.好用. 首先,我先说下我的理解: 通配符 * 通配符生成的是一个l ...

  2. 【HttpRunner v3.x】笔记 ——3. 录制生成测试用例

    在正式手动编写case之前,我们可以先来熟悉下httprunner的录制生成用例功能. 用postman的童鞋都知道,里面有个功能可以将接口转换成代码,可以直接copy过来使用,提升case编写效率. ...

  3. Canalv1.1.4版本搭建HA集群

    前提 Canal上一个正式版是于2019-9-2发布的v1.1.4,笔者几个月前把这个版本的Canal推上了生产环境,部署了HA集群.过程中虽然遇到不少的坑,但是在不出问题的前提下,Canal的作用还 ...

  4. 基于Celery在多台云服务器上实现分布式

    起源 最近参加公司里的一个比赛,比赛内容里有一项是尽量使用分布式实现项目.因为项目最终会跑在jetsonnano,一个贼卡的开发板,性能及其垃圾.而且要求使用python? 找了很多博客,讲的真的是模 ...

  5. nginx的gzip压缩

    随着nginx的发展,越来越多的网站使用nginx,因此nginx的优化变得越来越重要,今天我们来看看nginx的gzip压缩到底是怎么压缩的呢? gzip(GNU-ZIP)是一种压缩技术.经过gzi ...

  6. 一招教你如何用Word直接打开PDF进行编辑,无需下载转换软件

    我们都知道PDF是无法轻易修改的文档格式,市面上就出现了许多收费的如WPS等各种收费转换软件,免费的转换就只能转换几页,实属坑腰包,经过一番查找,终于发现Word可以直接打开PDF进行编辑,这一强大功 ...

  7. Repeater每行绑定事件代码

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { Repea ...

  8. App网络测试

    弱网测试作为健壮性测试的重要部分,对于移动端测试必不可少. 什么样的网络属于弱网? 小于或等于2G速率的都属于弱网,且随着5G的到来,3G网络速率的降低,未来3G也会逐渐划入弱网的行列. 对于弱网的数 ...

  9. liunx配置本地yum源和更新aliyun yum源

    1.挂载DVD光盘到/mnt   因为配置时候路径名里面不能有空格,否则不能识别 [root@ mnt]# mount   /dev/cdrom   /mnt [root@ mnt]# umount ...

  10. Centos 7 redis、tomcat、Spring Boot添加开机自启服务

    一.redis添加开机自启 1.添加服务配置文件 [root@test system]# vim /etc/systemd/system/redis-server.service 2.服务配置文件内容 ...