Arduino DS18B20温度检测
一.实物图
注:电阻选取4.7k欧

二.事例代码
注:先下载Onewire库到arduino libraries目录下,然后就有例子
#include <OneWire.h> // OneWire DS18S20, DS18B20, DS1822 Temperature Example
//
// http://www.pjrc.com/teensy/td_libs_OneWire.html
//
// The DallasTemperature library can do all this work for you!
// http://milesburton.com/Dallas_Temperature_Control_Library OneWire ds(10); // on pin 10 (a 4.7K resistor is necessary) void setup(void) {
Serial.begin(9600);
} void loop(void) {
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit; if ( !ds.search(addr)) {
Serial.println("No more addresses.");
Serial.println();
ds.reset_search();
delay(250);
return;
} Serial.print("ROM =");
for( i = 0; i < 8; i++) {
Serial.write(' ');
Serial.print(addr[i], HEX);
} if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return;
}
Serial.println(); // the first ROM byte indicates which chip
switch (addr[0]) {
case 0x10:
Serial.println(" Chip = DS18S20"); // or old DS1820
type_s = 1;
break;
case 0x28:
Serial.println(" Chip = DS18B20");
type_s = 0;
break;
case 0x22:
Serial.println(" Chip = DS1822");
type_s = 0;
break;
default:
Serial.println("Device is not a DS18x20 family device.");
return;
} ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it. present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad Serial.print(" Data = ");
Serial.print(present, HEX);
Serial.print(" ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
Serial.print(data[i], HEX);
Serial.print(" ");
}
Serial.print(" CRC=");
Serial.print(OneWire::crc8(data, 8), HEX);
Serial.println(); // Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
// be stored to an "int16_t" type, which is always 16 bits
// even when compiled on a 32 bit processor.
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// "count remain" gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
fahrenheit = celsius * 1.8 + 32.0;
Serial.print(" Temperature = ");
Serial.print(celsius);
Serial.print(" Celsius, ");
Serial.print(fahrenheit);
Serial.println(" Fahrenheit");
}
三.串口读取数据

Arduino DS18B20温度检测的更多相关文章
- Arduino LM35温度检测
一. 接线原理图 二.实物图 三.代码例子
- 一种快速查询多点DS18B20温度的方法(转)
源:http://hi.baidu.com/james_xiao/item/79b961c90623093e45941623 一种快速查询多点DS18B20温度的方法 引言 为了满足实时性要 ...
- ZigBee学习五 无线温度检测
ZigBee学习五 无线温度检测 1)修改公用头文件GenericApp.h typedef union h{ uint8 TEMP[4]; struct RFRXBUF { unsigned cha ...
- 基于FS4412的DS18B20温度采集编程实现(1-时序分析)
作者:秦老师,华清远见嵌入式学院讲师. 一.DS18B20简介 DS18B20是常用的数字温度计.DS18B20数字温度计提供9至12位(可配置)温度读数,表明该设备的温度. 信息通过单总线接口被发送 ...
- Arduino+DS18b20+OLED Display
DS18b20获取到温度数值保存到变量中,然后和天气图标还有滚动字幕一起发送到OLED 屏幕上显示 需要用到的库均可在Arduino库管理器下载. 电路图: 图中屏幕接线已在代码中写出,温度传感器da ...
- 基于PLC-C#串口通讯,温度检测和转速监控的c#/.Net实现。
我司为五金加工企业,其中有一条喷涂车间和流水线,客户要求能实时监控炉温温度.流水线速,并设置上下限值,达到上下限时报警. 开始考虑过USB的温度采集器,但是却没有找到带USB的光电开关,并且线路长度受 ...
- DS18B20温度获取
https://detail.tmall.com/item.htm?id=40083203373&spm=a1z09.2.0.0.31cd2e8d1sb06V&_u=e1qf7bf56 ...
- 嵌入式 Web Server 温度检测系统
1. Web Server 服务器后台表单处理程序:使用 CGI 程序接口编写后台程序的 Web 服务器. 2. Boa 服务器
- 单片机C语言实现的采用DS18B20的温度检测装置
这几天老师布置了一个课程设计题目:采用51单片机控制的DS18B20温度检测系统.大概花了我一个礼拜的时间,幸好我的C语言学得还可以,最后还是让我搞出来了,真是高兴,我是采用STC-52单片机和DS1 ...
随机推荐
- python爬虫数据解析的四种不同选择器Xpath,Beautiful Soup,pyquery,re
这里主要是做一个关于数据爬取以后的数据解析功能的整合,方便查阅,以防混淆 主要讲到的技术有Xpath,BeautifulSoup,PyQuery,re(正则) 首先举出两个作示例的代码,方便后面举例 ...
- Maven学习总结(27)——Maven自定义打包插件maven-assembly-plugin详解
Assembly插件会生成 "assemblies", 此特性等同于的Maven 1 distribution plug-in..该插件不仅支持创建二进制归档文件,也支持创建源码归 ...
- OpenStack命令行工具与API
Openstack命令行工具 我们推荐Openstack命令行工具和Openstack的Dashboard两者结合使用.一些用户由于使用过其他云技术背景的,可能会使用EC2兼容的API,相对于我们需要 ...
- CDOJ 889 Battle for Silver
Battle for Silver Time Limit: 2999/999MS (Java/Others) Memory Limit: 65432/65432KB (Java/Others) ...
- Tensorflow MNIST浅层神经网络的解释和答复
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51416540 看到之前的一篇博文:深入 ...
- noip模拟赛 Chtholly Nota Seniorious
题目背景 大样例下发链接: https://pan.baidu.com/s/1nuVpRS1 密码: sfxg こんなにも.たくさんの幸せをあの人に分けてもらった だから.きっと 今の.私は 谁が何と ...
- /usr/bin/ld: cannot find -lltdl collect2: ld returned 1 exit status make: *** [sapi/cgi/php-cgi] Err
/usr/bin/ld: cannot find -lltdl collect2: ld returned 1 exit status make: *** [sapi/cgi/php-cgi] Err ...
- The Elder HDU - 5956
/* 树上斜率优化 一开始想的是构造出一个序列 转化成一般的dp但是可能被卡 扫把状的树的话可能变成n*n 其实可以直接在树上维护这个单调队列 dfs虽然搞得是一棵树,但是每次都是dfs到的都是一个序 ...
- [POJ 1745] Divisbility
[题目链接] http://poj.org/problem?id=1745 [算法] DP [代码] #include <algorithm> #include <bitset> ...
- Linux下查看操作系统的位数和系统名称版本信息
Linux下如何明确地查看操作系统的位数 如何知晓操作系统是32位还是64位?这里介绍一种简单的方式: [plain] [root@localhost mysql-5.1.57]# getconf L ...