arduino水温度传感器数字显示_温度传感器DS18B20

DS18B20 单线数字温度传感器,即“一线器件”,其具有独特的优点:

( 1 )采用单总线的接口方式 与微处理器连接时 仅需要一条口线即可实现微处理器与 DS18B20 的双向通讯。 单总线具有经济性好,抗干扰能力强,适合于恶劣环境的现场温度测量,使用方便等优点,使用户可轻松地组建传感器网络,为测量系统的构建引入全新概念。

( 2 )测量温度范围宽,测量精度高 DS18B20 的测量范围为 -55 ℃ ~+ 125 ℃ ; 在 -10~+ 85°C 范围内,精度为 ± 0.5°C 。

( 3 )持多点组网功能 多个 DS18B20 可以并联在惟一的单线上,实现多点测温。

( 4)供电方式灵活 DS18B20 可以通过内部寄生电路从数据线上获取电源。因此,当数据线上的时序满足一定的要求时,可以不接外部电源,从而 使系统结构更趋简单,可靠性更高。

( 5 )测量参数可配置 DS18B20 的测量分辨率可通过程序设定 9~12 位。

DS18B20 具有体积更小、适用电压更宽、更经济、可选更小的封装方式,更宽的电压适用范围,适合于构建自己的经济的测温系统,因此也就被设计者们所青睐。

产品封装

19241cb53e797af2875f8a756c97c473.png

时序图

41d238c5cce6ce3ccea0868f45c0aae0.png

典型应用电路

75a84a6c978230808e11d2be7241aa31.png

寄生供电方式

5a28b609b5a093dfb2813ed28dc9c2b8.png

支持命令集

43c35485cf9b6950de3e19eb213d5fc8.png

复位时序

6bf7c857e04253124944d2091b07c02d.png

读写时序

a4c2f8c4f97a134a2936e4386d12d7c0.png

具体操作:

1、打开IDE,项目-加载库-管理库,搜索下载安装相应的库,不然程序写好了,编译时会报错;

ae894b464ff338dcc43ae8d0e92668fe.png
93bddc701c3d62818e86376580ad804a.png

搜索 18B20 ,看到18B20相关的库,点击 安装好,

打开 文件-示例,第三方库刚安装好的库,找到第一个例子Alarm;

fb059ee892e17ffef586b16704148db6.png

看到除了DallasTemperature.h,还需要另一个库OneWire.h,再按之前步骤,搜索 OneWire 安装即可;ONE_WIRE_BUS 2 意思是 数据口连接开发版引脚pin 2;

37ed42acf9636ea98484e4ab142044f6.png

2、Arduino 开发版用USB连接电脑,选择对应的开发版和端口,编译上传烧录,

9de6882d67d12011d42112cce4aeeb91.png
06f2fcfd71e80598f49df9e9d1f9840f.png

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接线图

84baaaae84c6719a12cc472645687ae7.png
184290a8ff95915ece3621a57254e796.png

4、接好线后,再用USB连接电脑,打开串口监视器查看结果;

8ee87e9d2538a79c5c1e7b17a4ee0de4.png
5e5677eb4f1c120067b4ccd0f71b29e7.png

Alarm案例代码,可根据需要自行修改:

#include //引用单总线头文件

#include //引用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

sensors.begin();//初始化器件

// locate devices on the bus

Serial.print("Found ");

Serial.print(sensors.getDeviceCount(), DEC);//DEC的意思是串口数据以10进制的格式输出

Serial.println(" devices.");

// search for devices on the bus and assign based on an index.

if (!sensors.getAddress(insideThermometer, 0))

Serial.println("Unable to find address for Device 0");

if (!sensors.getAddress(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

sensors.setHighAlarmTemp(insideThermometer, 30);

// alarm when temp is lower than -10C

sensors.setLowAlarmTemp(insideThermometer, -10);

// alarm when temp is higher than 31C

sensors.setHighAlarmTemp(outsideThermometer, 31);

// alarn when temp is lower than 27C

sensors.setLowAlarmTemp(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 = sensors.getTempC(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 = sensors.getHighAlarmTemp(deviceAddress);

Serial.print("High Alarm: ");

Serial.print(temp, DEC);

Serial.print("C/");

Serial.print(DallasTemperature::toFahrenheit(temp));

Serial.print("F | Low Alarm: ");

temp = sensors.getLowAlarmTemp(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 (sensors.hasAlarm(deviceAddress))

{

Serial.print("ALARM: ");

printData(deviceAddress);

}

}

void loop(void)

{

// call sensors.requestTemperatures() to issue a global temperature

// request to all devices on the bus

Serial.print("Requesting temperatures...");

sensors.requestTemperatures();

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()

sensors.resetAlarmSearch();

// alarmSearch() returns 0 when there are no devices with alarms

while (sensors.alarmSearch(alarmAddr))

{

Serial.print("ALARM: ");

printData(alarmAddr);

}

*/

}

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值