Example Code for a TMP102 I2c Thermometer————Arduino
参考: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.
- #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 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;
- // The sensor overreads (?)
- /* 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.send(0x00);
- Wire.endTransmission();
- Wire.requestFrom(TMP102_I2C_ADDRESS, 2);
- Wire.endTransmission();
- firstbyte = (Wire.receive());
- /*read the TMP102 datasheet - here we read one byte from
- each of the temperature registers on the TMP102*/
- secondbyte = (Wire.receive());
- /*The first byte contains the most significant bits, and
- the second the less significant */
- val = ((firstbyte) << 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();
- }
- void loop() {
- getTemp102();
- delay(5000); //wait 5 seconds before printing our next set of readings.
- }
Comments
val = firstbyte;
if ((firstbyte & 0x80) > 0) {
val |= 0x0F00;
}
val <<= 4;
Example Code for a TMP102 I2c Thermometer————Arduino的更多相关文章
- I2C总线的Arduino库函数
I2C总线的Arduino库函数 I2C即Inter-Integrated Circuit串行总线的缩写,是PHILIPS公司推出的芯片间串行传输总线.它以1根串行数据线(SDA)和1根串行时钟线(S ...
- I2C的库函数应用示例
I2C Arduino 简单应用举例 例1 多机通信 主机代码:(从编译器串口监视器发送数字1,2,3,4来控制从机的LED亮与灭) 1 #include <Wire.h> 2 v ...
- NXP LPC11xx I2C Slave 从机程序
/**************************************************************************** * $Id:: i2cslave.c 363 ...
- 1000多块整个插板,arduino + android 蓝牙插板的实现--屌丝版
需求描述 儿子有一堆充电玩具,基本上都是锂电池,经常插上去充电忘了到时拔下来,所以需要一块能设置接通时间的插板以保障电池的安全. 硬件设计: 首先需要一块插板,接着需要一个继电器,然后采用a ...
- (2)esp8266多国语言翻译系统
http://bbs.mydigit.cn/simple/?t2649513.html 这个想法不错 原来只是想用esp8266搞一个百度的多国语言翻译系统出来的,只是为了尝试如何调用各种web ap ...
- 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 ...
- 编写一个Open Live Writer的VSCode代码插件
起因 又是一年多没有更新过博客了,最近用Arduino做了一点有意思的东西,准备写一篇博客.打开尘封许久的博客园,发现因为Windows Live Writer停止更新,博客园推荐的客户端变为了Ope ...
- Arduino I2C + DS1307实时时钟
主要特性 DS1307是Maxim的串行.I2C实时时钟芯片.主要特性有: 工作电压:主电源电压4.5~5.5V,电池电压2.0~3.5V 功耗:电池供电.备份模式时<500nA 接口:I2C, ...
- Arduino I2C + 三轴加速度计LIS3DH
LIS3DH是ST公司生产的MEMS三轴加速度计芯片,实现运动传感的功能.主要特性有: 宽工作电压范围:1.71 ~ 3.6V 功耗:低功耗模式2μA:正常工作模式.ODR = 50Hz时功耗11μA ...
随机推荐
- Python 30道高频面试题及详细解答
开学啦,开学啦!周末坐地铁的时候看到很多同学推着行李箱,拎着大包小包的穿梭在人群中,哎新的一学期又开始啦,同时也意味着很多同学要准备毕业啦,尤其是准大四,准研三的同学. 今年的招聘行情并不乐观,小公司 ...
- CMOS设计手册—基础篇
模拟CMOS 衬底噪声:由于相邻的电阻互相注入电流而产生的衬底噪声.解决方法:在两个电阻之间加入一个P+注入区(作为P衬底晶圆的衬底接触).P+注入区保护电路免受载流子的影响,由于注入区是一个环形,所 ...
- 剑指 Offer 45. 把数组排成最小的数
题目描述 输入一个非负整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个. 示例 1: 输入: [10,2] 输出: "102" 示例 2: 输入: ...
- agumaster 分页方案
本文例程下载:https://files.cnblogs.com/files/xiandedanteng/agumaster20200430-1.zip 之前的分页方案有点小瑕疵,这回修正了一下. 控 ...
- 区块链Fabric 交易流程
1. 提交交易预案 1)应用端首先构建交易的预案,预案的作用是调用通道中的链码来读取或者写入账本的数据.应用端使用 Fabric 的 SDK 打包交易预案,并使用用户的私钥对预案进行签名. 应用打包完 ...
- Linux/Unix Terminal中文件/目录的颜色各代表什么意思?
注意:console/terminal中文件目录的颜色设置是可以更改的,故环境不同颜色就可能不一样. 下面是我所用终端的颜色示例: 颜色说明: 白色:普通文件 紫色:目录 红色:有问题的链接文件 蓝绿 ...
- 从架构到部署,全面了解K3s
Kubernetes无处不在--开发者的笔记本.树莓派.云.数据中心.混合云甚至多云上都有Kubernetes.它已然成为现代基础设施的基础,抽象了底层的计算.存储和网络服务.Kubernetes隐藏 ...
- 电子邮箱有哪些隐藏技能,读懂了效率提升N倍!
很多人将邮箱作为常见的通讯工具,然而,大部分职场人只了解其五分之一的功能.电子邮箱还有很多隐藏技能,身为商务精英的你,必须往下看看哦!今天跟随TOM邮箱小编导,来挖掘下邮箱的潜藏技能吧~ 作为经常外出 ...
- 【NOIP2013模拟】黑魔法师之门
题目描述 经过了16个工作日的紧张忙碌,未来的人类终于收集到了足够的能源.然而在与Violet星球的战争中,由于Z副官的愚蠢,地球的领袖applepi被邪恶的黑魔法师Vani囚禁在了Violet星球. ...
- 【深入理解Linux内核架构】3.2 (N)UMA模型中的内存组织
内核对一致和非一致内存访问系统使用相同的数据结构.在UMA系统上,只使用一个NUMA结点来管理整个系统内存.而内存管理的其他部分则相信他们是在处理一个伪NUMA系统. 3.2.1 概述 内存划分为结点 ...