DS18B20單線數(shù)字溫度傳感器,“一線設備”具有獨特的優(yōu)點。
(1)使用單總線接口連接到微處理器時,只要有一個端口,微處理器和DS18B20之間就可以雙向通信。
單總線具有經(jīng)濟性好,抗干擾能力強,適合于惡劣環(huán)境的現(xiàn)場溫度測量,使用方便等優(yōu)點,使用戶可輕松地組建傳感器網(wǎng)絡,為測量系統(tǒng)的構(gòu)建引入全新概念。( 2 )測量溫度范圍寬,測量精度高 DS18B20 的測量范圍為 -55 ℃ ~+ 125 ℃ ; 在 -10~+ 85°C 范圍內(nèi),精度為 ± 0.5°C 。
( 3 )持多點組網(wǎng)功能 多個 DS18B20 可以并聯(lián)在惟一的單線上,實現(xiàn)多點測溫。
( 4)供電方式靈活 DS18B20 可以通過內(nèi)部寄生電路從數(shù)據(jù)線上獲取電源。因此,當數(shù)據(jù)線上的時序滿足一定的要求時,可以不接外部電源,從而 使系統(tǒng)結(jié)構(gòu)更趨簡單,可靠性更高。
( 5 )測量參數(shù)可配置 DS18B20 的測量分辨率可通過程序設定 9~12 位。
DS18B20 具有體積更小、適用電壓更寬、更經(jīng)濟、可選更小的封裝方式,更寬的電壓適用范圍,適合于構(gòu)建自己的經(jīng)濟的測溫系統(tǒng),因此也就被設計者們所青睞。
產(chǎn)品封裝
時序圖
典型應用電路
寄生供電方式
支持命令集
復位時序
讀寫時序
具體操作:
1、打開IDE,項目-加載庫-管理庫,搜索下載安裝相應的庫,不然程序?qū)懞昧?,編譯時會報錯;
搜索 18B20 ,看到18B20相關(guān)的庫,點擊 安裝好,
打開 文件-示例,第三方庫剛安裝好的庫,找到第一個例子Alarm;
看到除了Dalla,還需要另一個庫OneWire.h,再按之前步驟,搜索 OneWire 安裝即可;ONE_WIRE_BUS 2 意思是 數(shù)據(jù)口連接開發(fā)版引腳pin 2;
2、Arduino 開發(fā)版用USB連接電腦,選擇對應的開發(fā)版和端口,編譯上傳燒錄,
3、接線,必須加電阻,不加電阻檢測不到設備;
管腳定義:面朝印字面,左為GND,右為VCC,中間為數(shù)字輸出引腳(須接上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連接電腦,打開串口監(jiān)視器查看結(jié)果;
Alarm案例代碼,可根據(jù)需要自行修改:
#include <OneWire.h>//引用單總線頭文件
#include <Dalla>//引用18b20驅(qū)動文件
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2//定義2腳為數(shù)據(jù)腳
// 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的意思是串口數(shù)據(jù)以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);
}
*/
}
1.《【ds18b20為什么發(fā)燙】溫度傳感器DS18B20》援引自互聯(lián)網(wǎng),旨在傳遞更多網(wǎng)絡信息知識,僅代表作者本人觀點,與本網(wǎng)站無關(guān),侵刪請聯(lián)系頁腳下方聯(lián)系方式。
2.《【ds18b20為什么發(fā)燙】溫度傳感器DS18B20》僅供讀者參考,本網(wǎng)站未對該內(nèi)容進行證實,對其原創(chuàng)性、真實性、完整性、及時性不作任何保證。
3.文章轉(zhuǎn)載時請保留本站內(nèi)容來源地址,http://f99ss.com/auto/3129169.html