DS18B20单线数字温度传感器,“一线设备”具有独特的优点。
(1)使用单总线接口连接到微处理器时,只要有一个端口,微处理器和DS18B20之间就可以双向通信。
单总线具有经济性好,抗干扰能力强,适合于恶劣环境的现场温度测量,使用方便等优点,使用户可轻松地组建传感器网络,为测量系统的构建引入全新概念。( 2 )测量温度范围宽,测量精度高 DS18B20 的测量范围为 -55 ℃ ~+ 125 ℃ ; 在 -10~+ 85°C 范围内,精度为 ± 0.5°C 。
( 3 )持多点组网功能 多个 DS18B20 可以并联在惟一的单线上,实现多点测温。
( 4)供电方式灵活 DS18B20 可以通过内部寄生电路从数据线上获取电源。因此,当数据线上的时序满足一定的要求时,可以不接外部电源,从而 使系统结构更趋简单,可靠性更高。
( 5 )测量参数可配置 DS18B20 的测量分辨率可通过程序设定 9~12 位。
DS18B20 具有体积更小、适用电压更宽、更经济、可选更小的封装方式,更宽的电压适用范围,适合于构建自己的经济的测温系统,因此也就被设计者们所青睐。
产品封装
时序图
典型应用电路
寄生供电方式
支持命令集
复位时序
读写时序
具体操作:
1、打开IDE,项目-加载库-管理库,搜索下载安装相应的库,不然程序写好了,编译时会报错;
搜索 18B20 ,看到18B20相关的库,点击 安装好,
打开 文件-示例,第三方库刚安装好的库,找到第一个例子Alarm;
看到除了Dalla,还需要另一个库OneWire.h,再按之前步骤,搜索 OneWire 安装即可;ONE_WIRE_BUS 2 意思是 数据口连接开发版引脚pin 2;
2、Arduino 开发版用USB连接电脑,选择对应的开发版和端口,编译上传烧录,
3、接线,必须加电阻,不加电阻检测不到设备;
管脚定义:面朝印字面,左为GND,右为VCC,中间为数字输出引脚(须接上4.7K—10K的上拉电阻)本例4.7K电阻;
BOM表
Arduino Uno *1
18B20温度传感器 *1
4.7K电阻*1
接线
Arduino Uno <------> 18B20温度传感器 颜色
Pin 2 <------> DO 白色
5V <------> VCC 红色
GND <------> GND 黄色
Arduino接线图
4、接好线后,再用USB连接电脑,打开串口监视器查看结果;
Alarm案例代码,可根据需要自行修改:
#include <OneWire.h>//引用单总线头文件
#include <Dalla>//引用18b20驱动文件
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2//定义2脚为数据脚
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// arrays to hold device addresses
DeviceAddress insideThermometer, outsideThermometer;
void setup(void)
{
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
();//初始化器件
// locate devices on the bus
Serial.print("Found ");
Serial.prin(), DEC);//DEC的意思是串口数据以10进制的格式输出
Serial.println(" devices.");
// search for devices on the bus and assign based on an index.
if (!(insideThermometer, 0))
Serial.println("Unable to find address for Device 0");
if (!(outsideThermometer, 1))
Serial.println("Unable to find address for Device 1");
// show the addresses we found on the bus
Serial.print("Device 0 Address: ");
printAddress(insideThermometer);
Serial.println();
Serial.print("Device 0 Alarms: ");
printAlarms(insideThermometer);
Serial.println();
Serial.print("Device 1 Address: ");
printAddress(outsideThermometer);
Serial.println();
Serial.print("Device 1 Alarms: ");
printAlarms(outsideThermometer);
Serial.println();
Serial.println("Setting alarm temps...");
// alarm when temp is higher than 30C
(insideThermometer, 30);
// alarm when temp is lower than -10C
(insideThermometer, -10);
// alarm when temp is higher than 31C
(outsideThermometer, 31);
// alarn when temp is lower than 27C
(outsideThermometer, 27);
Serial.print("New Device 0 Alarms: ");
printAlarms(insideThermometer);
Serial.println();
Serial.print("New Device 1 Alarms: ");
printAlarms(outsideThermometer);
Serial.println();
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = (deviceAddress);
Serial.print("Temp C: ");
Serial.print(tempC);
Serial.print(" Temp F: ");
Serial.print(DallasTemperature::toFahrenheit(tempC));
}
void printAlarms(uint8_t deviceAddress[])
{
char temp;
temp = (deviceAddress);
Serial.print("High Alarm: ");
Serial.print(temp, DEC);
Serial.print("C/");
Serial.print(DallasTemperature::toFahrenheit(temp));
Serial.print("F | Low Alarm: ");
temp = (deviceAddress);
Serial.print(temp, DEC);
Serial.print("C/");
Serial.print(DallasTemperature::toFahrenheit(temp));
Serial.print("F");
}
// main function to print information about a device
void printData(DeviceAddress deviceAddress)
{
Serial.print("Device Address: ");
printAddress(deviceAddress);
Serial.print(" ");
printTemperature(deviceAddress);
Serial.println();
}
void checkAlarm(DeviceAddress deviceAddress)
{
if (deviceAddress))
{
Serial.print("ALARM: ");
printData(deviceAddress);
}
}
void loop(void)
{
// call () to issue a global temperature
// request to all devices on the bus
Serial.print("Requesting temperatures...");
();
Serial.println("DONE");
// Method 1:
// check each address individually for an alarm condition
checkAlarm(insideThermometer);
checkAlarm(outsideThermometer);
/*
// Alternate method:
// Search the bus and iterate through addresses of devices with alarms
// space for the alarm device's address
DeviceAddress alarmAddr;
Serial.println("Searching for alarms...");
// resetAlarmSearch() must be called before calling alarmSearch()
();
// alarmSearch() returns 0 when there are no devices with alarms
while (alarmAddr))
{
Serial.print("ALARM: ");
printData(alarmAddr);
}
*/
}