“SKU:RB-02S160 DS18B20 水溫傳感器”的版本間的差異
(以“右 ==產(chǎn)品概述== DS18B20是常用的數(shù)字溫度傳感器,其輸出的是數(shù)字信號,具有體積小,硬件開銷...”為內(nèi)容創(chuàng)建頁面) |
(→?相關(guān)資料) |
||
(未顯示1個用戶的11個中間版本) | |||
第31行: | 第31行: | ||
===example1_Arduino=== | ===example1_Arduino=== | ||
* 主要硬件 | * 主要硬件 | ||
? | + | :Arduino UNO 控制器 | |
+ | :傳感器擴(kuò)展板 V5.0 | ||
+ | :DS18B20 溫度傳感器 | ||
+ | :單頭防插反 3P 傳感器連接線 | ||
+ | :USB 數(shù)據(jù)線 | ||
* 硬件連接 | * 硬件連接 | ||
+ | [[文件:02S16004.png|500px|縮略圖|居中]] | ||
* 示例程序 | * 示例程序 | ||
+ | <pre style='color:blue'> | ||
+ | #define Skip_ROM 0xCC | ||
+ | #define Convert_T 0x44 | ||
+ | #define Read_scratchpad 0xBE | ||
+ | |||
+ | #define Port_18B20 7 | ||
+ | #define Tx_18B20 pinMode(Port_18B20, OUTPUT) | ||
+ | #define Rx_18B20 pinMode(Port_18B20, INPUT) | ||
+ | |||
+ | #define Set_Port_18B20_High digitalWrite(Port_18B20, HIGH) | ||
+ | #define Set_Port_18B20_Low digitalWrite(Port_18B20, LOW) | ||
+ | |||
+ | #define Read_Port_18B20 digitalRead(Port_18B20) | ||
+ | |||
+ | unsigned char DS18B20_reset(); | ||
+ | void DS18B20_write(char WRT); | ||
+ | unsigned char DS18B20_read(); | ||
+ | |||
+ | unsigned short tempL = 0; | ||
+ | unsigned short tempH = 0; | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | Serial.begin(9600); | ||
+ | } | ||
+ | |||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | unsigned int temp = 0; | ||
+ | float Temperature = 0.0; | ||
+ | if(!DS18B20_reset()) | ||
+ | { | ||
+ | DS18B20_write(Skip_ROM); | ||
+ | DS18B20_write(Convert_T); | ||
+ | delay(750); | ||
+ | DS18B20_reset(); | ||
+ | DS18B20_write(Skip_ROM); | ||
+ | DS18B20_write(Read_scratchpad); | ||
+ | tempL = DS18B20_read(); | ||
+ | tempH = DS18B20_read(); | ||
+ | temp = ((unsigned int)tempH << 8) + (unsigned int)tempL; | ||
+ | temp = (float)temp * 6.25; | ||
+ | Temperature = (float)temp/100; | ||
+ | Serial.print("Temperature: "); | ||
+ | Serial.println(Temperature); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | unsigned char DS18B20_reset() | ||
+ | { | ||
+ | Tx_18B20; | ||
+ | Set_Port_18B20_Low; | ||
+ | delayMicroseconds(480); | ||
+ | Rx_18B20; | ||
+ | delayMicroseconds(60); | ||
+ | |||
+ | if(Read_Port_18B20 == 0) | ||
+ | { | ||
+ | delayMicroseconds(480); | ||
+ | return 0; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | delayMicroseconds(480); | ||
+ | return 1; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void DS18B20_write(char WRT) | ||
+ | { | ||
+ | char i = 0, Cmd = 0; | ||
+ | Cmd = WRT; | ||
+ | Rx_18B20; | ||
+ | |||
+ | for(i = 0; i< 8; i++) | ||
+ | { | ||
+ | if((Cmd & (1<<i)) != 0) | ||
+ | { | ||
+ | Tx_18B20; | ||
+ | Set_Port_18B20_Low; | ||
+ | delayMicroseconds(1); | ||
+ | Rx_18B20; | ||
+ | delayMicroseconds(60); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | Tx_18B20; | ||
+ | Set_Port_18B20_Low; | ||
+ | delayMicroseconds(60); | ||
+ | Rx_18B20; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | unsigned char DS18B20_read() | ||
+ | { | ||
+ | char i = 0, result = 0; | ||
+ | Rx_18B20; | ||
+ | for(i = 0; i < 8; i++) | ||
+ | { | ||
+ | Tx_18B20; | ||
+ | Set_Port_18B20_Low; | ||
+ | delayMicroseconds(2); | ||
+ | Rx_18B20; | ||
+ | if(Read_Port_18B20 != 0) | ||
+ | result |= 1<<i; | ||
+ | delayMicroseconds(60); | ||
+ | } | ||
+ | return result; | ||
+ | }</pre> | ||
* 程序效果 | * 程序效果 | ||
+ | [[文件:02S16003.png|500px|縮略圖|居中]] | ||
===example2_Arduino=== | ===example2_Arduino=== | ||
* 主要硬件 | * 主要硬件 | ||
+ | :Arduino UNO 控制器 | ||
+ | :傳感器擴(kuò)展板 V5.0 | ||
+ | :DS18B20 水溫傳感器 | ||
+ | :IIC1602 LCD 液晶顯示器 | ||
* 硬件連接 | * 硬件連接 | ||
+ | [[文件:02S16005.png|500px|縮略圖|居中]] | ||
* 示例程序 | * 示例程序 | ||
+ | <pre style='color:blue'>#include <Wire.h> | ||
+ | #include <LiquidCrystal_I2C.h> | ||
+ | LiquidCrystal_I2C lcd(0x27,16,2); | ||
+ | |||
+ | #define Skip_ROM 0xCC | ||
+ | #define Convert_T 0x44 | ||
+ | #define Read_scratchpad 0xBE | ||
+ | |||
+ | #define Port_18B20 7 | ||
+ | #define Tx_18B20 pinMode(Port_18B20, OUTPUT) | ||
+ | #define Rx_18B20 pinMode(Port_18B20, INPUT) | ||
+ | |||
+ | #define Set_Port_18B20_High digitalWrite(Port_18B20, HIGH) | ||
+ | #define Set_Port_18B20_Low digitalWrite(Port_18B20, LOW) | ||
+ | |||
+ | #define Read_Port_18B20 digitalRead(Port_18B20) | ||
+ | |||
+ | unsigned char DS18B20_reset(); | ||
+ | void DS18B20_write(char WRT); | ||
+ | unsigned char DS18B20_read(); | ||
+ | |||
+ | unsigned short tempL = 0; | ||
+ | unsigned short tempH = 0; | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | Serial.begin(9600); | ||
+ | lcd.init(); | ||
+ | lcd.backlight(); | ||
+ | } | ||
+ | |||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | unsigned int temp = 0; | ||
+ | float Temperature = 0.0; | ||
+ | |||
+ | if(!DS18B20_reset()) | ||
+ | { | ||
+ | DS18B20_write(Skip_ROM); | ||
+ | DS18B20_write(Convert_T); | ||
+ | delay(750); | ||
+ | |||
+ | DS18B20_reset(); | ||
+ | DS18B20_write(Skip_ROM); | ||
+ | DS18B20_write(Read_scratchpad); | ||
+ | |||
+ | tempL = DS18B20_read(); | ||
+ | tempH = DS18B20_read(); | ||
+ | |||
+ | temp = ((unsigned int)tempH << 8) + (unsigned int)tempL; | ||
+ | |||
+ | temp = (float)temp * 6.25; | ||
+ | |||
+ | Temperature = (float)temp/100; | ||
+ | |||
+ | Serial.println(Temperature); | ||
+ | |||
+ | lcd.setCursor(0, 0); | ||
+ | lcd.print("Temperature ="); | ||
+ | lcd.setCursor(0,1); | ||
+ | lcd.print(Temperature); | ||
+ | lcd.setCursor(6,1); | ||
+ | lcd.print("Celsius"); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | unsigned char DS18B20_reset() | ||
+ | { | ||
+ | Tx_18B20; | ||
+ | Set_Port_18B20_Low; | ||
+ | delayMicroseconds(480); | ||
+ | Rx_18B20; | ||
+ | delayMicroseconds(60); | ||
+ | |||
+ | if(Read_Port_18B20 == 0) | ||
+ | { | ||
+ | delayMicroseconds(480); | ||
+ | return 0; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | delayMicroseconds(480); | ||
+ | return 1; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void DS18B20_write(char WRT) | ||
+ | { | ||
+ | char i = 0, Cmd = 0; | ||
+ | Cmd = WRT; | ||
+ | Rx_18B20; | ||
+ | |||
+ | for(i = 0; i< 8; i++) | ||
+ | { | ||
+ | if((Cmd & (1<<i)) != 0) | ||
+ | { | ||
+ | Tx_18B20; | ||
+ | Set_Port_18B20_Low; | ||
+ | delayMicroseconds(1); | ||
+ | Rx_18B20; | ||
+ | delayMicroseconds(60); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | Tx_18B20; | ||
+ | Set_Port_18B20_Low; | ||
+ | delayMicroseconds(60); | ||
+ | Rx_18B20; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | unsigned char DS18B20_read() | ||
+ | { | ||
+ | char i = 0, result = 0; | ||
+ | Rx_18B20; | ||
+ | |||
+ | for(i = 0; i < 8; i++) | ||
+ | { | ||
+ | Tx_18B20; | ||
+ | Set_Port_18B20_Low; | ||
+ | delayMicroseconds(2); | ||
+ | Rx_18B20; | ||
+ | |||
+ | if(Read_Port_18B20 != 0) | ||
+ | result |= 1<<i; | ||
+ | |||
+ | delayMicroseconds(60); | ||
+ | } | ||
+ | |||
+ | return result; | ||
+ | }</pre> | ||
* 程序效果 | * 程序效果 | ||
+ | [[文件:02S16006.png|500px|縮略圖|居中]] | ||
? | === | + | ===example3_arduino=== |
* 主要硬件 | * 主要硬件 | ||
+ | :Arduino UNO 控制器 | ||
+ | :傳感器擴(kuò)展板V5.0 | ||
+ | :LED發(fā)光模塊-紅/綠/黃 | ||
+ | :蜂鳴器發(fā)聲模塊 | ||
+ | :繼電器模塊 | ||
+ | :潛水泵 | ||
+ | :DC2.1 電源接線端子 | ||
+ | :7.4V 1300mAh 鋰電池 | ||
+ | * 硬件連接 | ||
+ | [[文件:02S16007.png|900px|縮略圖|居中]] | ||
+ | * 示例程序 | ||
+ | <pre style='color:blue'> | ||
+ | #define Skip_ROM 0xCC | ||
+ | #define Convert_T 0x44 | ||
+ | #define Read_scratchpad 0xBE | ||
+ | |||
+ | #define Port_18B20 12 //溫度傳感器 | ||
+ | #define Tx_18B20 pinMode(Port_18B20, OUTPUT) | ||
+ | #define Rx_18B20 pinMode(Port_18B20, INPUT) | ||
+ | |||
+ | #define Set_Port_18B20_High digitalWrite(Port_18B20, HIGH) | ||
+ | #define Set_Port_18B20_Low digitalWrite(Port_18B20, LOW) | ||
+ | |||
+ | #define Read_Port_18B20 digitalRead(Port_18B20) | ||
+ | |||
+ | #define LED1 9 //G | ||
+ | #define LED2 10 //Y | ||
+ | #define LED3 11 //R | ||
+ | #define buzzer 7 //蜂鳴器 | ||
+ | #define relay 8 //繼電器 | ||
+ | |||
+ | unsigned char DS18B20_reset(); | ||
+ | void DS18B20_write(char WRT); | ||
+ | unsigned char DS18B20_read(); | ||
+ | |||
+ | unsigned short tempL = 0; | ||
+ | unsigned short tempH = 0; | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | Serial.begin(9600); | ||
+ | pinMode(LED1,OUTPUT); | ||
+ | pinMode(LED2,OUTPUT); | ||
+ | pinMode(LED3,OUTPUT); | ||
+ | pinMode(buzzer,OUTPUT); | ||
+ | pinMode(relay,OUTPUT); | ||
+ | |||
+ | digitalWrite(LED1,LOW); | ||
+ | digitalWrite(LED2,LOW); | ||
+ | digitalWrite(LED3,LOW); | ||
+ | digitalWrite(buzzer,HIGH); | ||
+ | digitalWrite(relay,LOW); | ||
+ | } | ||
+ | |||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | unsigned int temp = 0; | ||
+ | float Temperature = 0.0; | ||
+ | |||
+ | if(!DS18B20_reset()) | ||
+ | { | ||
+ | DS18B20_write(Skip_ROM); | ||
+ | DS18B20_write(Convert_T); | ||
+ | delay(50); | ||
+ | |||
+ | DS18B20_reset(); | ||
+ | DS18B20_write(Skip_ROM); | ||
+ | DS18B20_write(Read_scratchpad); | ||
+ | |||
+ | tempL = DS18B20_read(); | ||
+ | tempH = DS18B20_read(); | ||
+ | |||
+ | temp = ((unsigned int)tempH << 8) + (unsigned int)tempL; | ||
+ | |||
+ | temp = (float)temp * 6.25; | ||
+ | |||
+ | Temperature = (float)temp/100; | ||
+ | |||
+ | Serial.println(Temperature); | ||
+ | int val = int(Temperature); | ||
+ | if(val>= 30 && val <= 40) | ||
+ | { | ||
+ | digitalWrite(LED1,LOW); | ||
+ | digitalWrite(LED2,HIGH); | ||
+ | digitalWrite(LED3,LOW); | ||
+ | digitalWrite(buzzer,HIGH); | ||
+ | digitalWrite(relay,LOW); | ||
+ | } | ||
+ | else if(val > 40) | ||
+ | { | ||
+ | digitalWrite(LED1,LOW); | ||
+ | digitalWrite(LED2,LOW); | ||
+ | digitalWrite(LED3,HIGH); | ||
+ | digitalWrite(buzzer,LOW); | ||
+ | digitalWrite(relay,HIGH); | ||
+ | } | ||
+ | else if(val < 30) | ||
+ | { | ||
+ | digitalWrite(LED1,HIGH); | ||
+ | digitalWrite(LED2,LOW); | ||
+ | digitalWrite(LED3,LOW); | ||
+ | digitalWrite(buzzer,HIGH); | ||
+ | digitalWrite(relay,LOW); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | digitalWrite(LED1,LOW); | ||
+ | digitalWrite(LED2,LOW); | ||
+ | digitalWrite(LED3,LOW); | ||
+ | digitalWrite(buzzer,HIGH); | ||
+ | digitalWrite(relay,LOW); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | unsigned char DS18B20_reset() | ||
+ | { | ||
+ | Tx_18B20; | ||
+ | Set_Port_18B20_Low; | ||
+ | delayMicroseconds(480); | ||
+ | Rx_18B20; | ||
+ | delayMicroseconds(60); | ||
+ | |||
+ | if(Read_Port_18B20 == 0) | ||
+ | { | ||
+ | delayMicroseconds(480); | ||
+ | return 0; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | delayMicroseconds(480); | ||
+ | return 1; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void DS18B20_write(char WRT) | ||
+ | { | ||
+ | char i = 0, Cmd = 0; | ||
+ | Cmd = WRT; | ||
+ | Rx_18B20; | ||
+ | for(i = 0; i< 8; i++) | ||
+ | { | ||
+ | if((Cmd & (1<<i)) != 0) | ||
+ | { | ||
+ | Tx_18B20; | ||
+ | Set_Port_18B20_Low; | ||
+ | delayMicroseconds(1); | ||
+ | Rx_18B20; | ||
+ | delayMicroseconds(60); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | Tx_18B20; | ||
+ | Set_Port_18B20_Low; | ||
+ | delayMicroseconds(60); | ||
+ | Rx_18B20; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | unsigned char DS18B20_read() | ||
+ | { | ||
+ | char i = 0, result = 0; | ||
+ | Rx_18B20; | ||
+ | for(i = 0; i < 8; i++) | ||
+ | { | ||
+ | Tx_18B20; | ||
+ | Set_Port_18B20_Low; | ||
+ | delayMicroseconds(2); | ||
+ | Rx_18B20; | ||
+ | if(Read_Port_18B20 != 0) | ||
+ | result |= 1<<i; | ||
+ | delayMicroseconds(60); | ||
+ | } | ||
+ | |||
+ | return result; | ||
+ | }</pre> | ||
+ | |||
+ | * 程序效果 | ||
+ | 此例程可模擬控溫系統(tǒng)<br/> | ||
+ | :若水溫在大于40攝氏度時,LED 紅燈亮起、蜂鳴器發(fā)出報警聲、水泵開始抽水降溫,直到溫度降低40攝氏度以下后,停止抽水。 | ||
+ | :若水溫在30攝氏度和40攝氏度之間時,LED 黃燈亮起、蜂鳴器停止報警、水泵停止抽水。 | ||
+ | :若水溫在30攝氏度以下時,LED 綠燈亮起、蜂鳴器停止報警、水泵停止抽水。 | ||
+ | |||
+ | ===example_Raspberry Pi=== | ||
+ | * 使用軟件 | ||
+ | :編程軟件:Python 2.7.13 | ||
+ | :操作系統(tǒng):Linux raspberrypi 4.14.50 | ||
+ | :前提:PC 端已通過 SSH 軟件登陸到 RaspberryPi 控制器 | ||
+ | |||
+ | * 主要硬件 | ||
+ | :Raspberry Pi 3B+ | ||
+ | :16G SD卡 | ||
+ | :5V 2.5A 適配器 | ||
+ | :杜邦線-公母頭 | ||
+ | :GPIO 擴(kuò)展板 | ||
+ | :大面包板 | ||
* 硬件連接 | * 硬件連接 | ||
+ | [[文件:02S16008.png|500px|縮略圖|居中]] | ||
* 示例程序 | * 示例程序 | ||
+ | 樹莓派控制器配置步驟<br/> | ||
+ | 1.編輯 config.txt 文件,啟用單線接口,默認(rèn)為 BCM4 接口<br/> | ||
+ | sudo nano /boot/config.txt <br/> | ||
+ | 2.加入語句<br/> | ||
+ | dtoverlay=w1-gpio<br/> | ||
+ | 3.重新啟動樹莓派控制器<br/> | ||
+ | sudo reboot<br/> | ||
+ | 4.列出 Raspberry Pi 通過 1-Wire 總線(默認(rèn)為BCM4)發(fā)現(xiàn)的設(shè)備<br/> | ||
+ | ls /sys/bus/w1/devices/<br/> | ||
+ | 如圖所示,所列出的 28-xxx 為連接的 1-Wire 設(shè)備<br/> | ||
+ | [[文件:02S16009.png|500px|縮略圖|居中]] | ||
+ | 5.進(jìn)入設(shè)備文件夾,獲取溫度數(shù)值<br/> | ||
+ | cd /sys/bus/w1/devices/28-0213169dceaa<br/> | ||
+ | cat w1_slave<br/> | ||
+ | [[文件:02S16010.png|500px|縮略圖|居中]]<br/> | ||
+ | |||
+ | Python 代碼<br/> | ||
+ | <pre style='color:blue'>import os | ||
+ | import glob | ||
+ | import time | ||
+ | |||
+ | os.system('modprobe w1-gpio') | ||
+ | os.system('modprobe w1-therm') | ||
+ | |||
+ | base_dir = '/sys/bus/w1/devices/' | ||
+ | device_folder = glob.glob(base_dir + '28*')[0] | ||
+ | device_file = device_folder + '/w1_slave' | ||
+ | |||
+ | def read_temp_raw(): | ||
+ | f = open(device_file, 'r') | ||
+ | lines = f.readlines() | ||
+ | f.close() | ||
+ | return lines | ||
+ | |||
+ | def read_temp(): | ||
+ | lines = read_temp_raw() | ||
+ | while lines[0].strip()[-3:] != 'YES': | ||
+ | time.sleep(0.2) | ||
+ | lines = read_temp_raw() | ||
+ | equals_pos = lines[1].find('t=') | ||
+ | if equals_pos != -1: | ||
+ | temp_string = lines[1][equals_pos+2:] | ||
+ | temp_c = float(temp_string) / 1000.0 | ||
+ | temp_f = temp_c * 9.0 / 5.0 + 32.0 | ||
+ | return temp_c, temp_f | ||
+ | |||
+ | while True: | ||
+ | print(read_temp()) | ||
+ | time.sleep(1) | ||
+ | |||
+ | </pre> | ||
* 程序效果 | * 程序效果 | ||
+ | 在登陸窗口會打印出當(dāng)前的水溫,分別為攝氏溫度和華氏溫度 | ||
+ | [[文件:02S16011.png|600px|縮略圖|居中]] | ||
==相關(guān)資料== | ==相關(guān)資料== | ||
+ | [[文件:erweima.png|230px|無框|右]] | ||
* DS18B20 溫度傳感器 datasheet & 示例程序 | * DS18B20 溫度傳感器 datasheet & 示例程序 | ||
? | + | 下載鏈接: https://pan.baidu.com/s/1scZ2LKFiCDHJbrT2Se9keg | |
+ | 提取碼:wp3v | ||
+ | * 相關(guān)資料 | ||
+ | [https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf DS18B20 datasheet] <br/> | ||
+ | [https://pinout.xyz/pinout/1_wire W1-GPIO - One-Wire Interface] <br/> | ||
+ | * 產(chǎn)品購買鏈接:http://lifestyle201.com/goods-870.html |
2021年6月22日 (二) 10:21的最后版本
目錄 |
產(chǎn)品概述
DS18B20是常用的數(shù)字溫度傳感器,其輸出的是數(shù)字信號,具有體積小,硬件開銷低,抗干擾能力強(qiáng),精度高的特點。 DS18B20數(shù)字溫度傳感器接線方便,封裝成后可應(yīng)用于多種場合,如管道式,螺紋式,磁鐵吸附式,不銹鋼封裝式等。主要根據(jù)應(yīng)用場合的不同而改變其外觀。
DS18B20水溫水溫傳感器采用的是不銹鋼封裝,防水防潮。封裝后的DS18B20可用于電纜溝測溫,高爐水循環(huán)測溫,鍋爐測溫,機(jī)房測溫,農(nóng)業(yè)大棚測溫,潔凈室測溫,彈藥庫測溫等各種非極限溫度場合,適用于各種狹小空間設(shè)備數(shù)字測溫和控制領(lǐng)域。
產(chǎn)品參數(shù)
基本參數(shù)
- 品名:DS18B20水溫傳感器
- 貨號:RB-02S160
- 品牌:奧松機(jī)器人
- 產(chǎn)地:哈爾濱
- 固定孔:M3*4
- 電纜長度:90cm
- 傳感器:直徑6mm,長30mm
電氣參數(shù)
1.接口類型:3P防插反接口
2.信號類型:數(shù)字信號
3.工作電壓:3V - 5.5V
4.引腳定義:
- +:電源正極
- -:電源負(fù)極
- S:信號端
5.傳感器接口:KF2510-3P防插反接口
6.測試范圍:-55℃~+125℃(-67°F至+ 257°F)
7.測量精度:在-10~+85℃范圍內(nèi)精確度為±5℃
8.測量分辨率:溫度計分辨率可以被使用者選擇為9~12位
使用方法
example1_Arduino
- 主要硬件
- Arduino UNO 控制器
- 傳感器擴(kuò)展板 V5.0
- DS18B20 溫度傳感器
- 單頭防插反 3P 傳感器連接線
- USB 數(shù)據(jù)線
- 硬件連接
- 示例程序
#define Skip_ROM 0xCC #define Convert_T 0x44 #define Read_scratchpad 0xBE #define Port_18B20 7 #define Tx_18B20 pinMode(Port_18B20, OUTPUT) #define Rx_18B20 pinMode(Port_18B20, INPUT) #define Set_Port_18B20_High digitalWrite(Port_18B20, HIGH) #define Set_Port_18B20_Low digitalWrite(Port_18B20, LOW) #define Read_Port_18B20 digitalRead(Port_18B20) unsigned char DS18B20_reset(); void DS18B20_write(char WRT); unsigned char DS18B20_read(); unsigned short tempL = 0; unsigned short tempH = 0; void setup() { Serial.begin(9600); } void loop() { unsigned int temp = 0; float Temperature = 0.0; if(!DS18B20_reset()) { DS18B20_write(Skip_ROM); DS18B20_write(Convert_T); delay(750); DS18B20_reset(); DS18B20_write(Skip_ROM); DS18B20_write(Read_scratchpad); tempL = DS18B20_read(); tempH = DS18B20_read(); temp = ((unsigned int)tempH << 8) + (unsigned int)tempL; temp = (float)temp * 6.25; Temperature = (float)temp/100; Serial.print("Temperature: "); Serial.println(Temperature); } } unsigned char DS18B20_reset() { Tx_18B20; Set_Port_18B20_Low; delayMicroseconds(480); Rx_18B20; delayMicroseconds(60); if(Read_Port_18B20 == 0) { delayMicroseconds(480); return 0; } else { delayMicroseconds(480); return 1; } } void DS18B20_write(char WRT) { char i = 0, Cmd = 0; Cmd = WRT; Rx_18B20; for(i = 0; i< 8; i++) { if((Cmd & (1<<i)) != 0) { Tx_18B20; Set_Port_18B20_Low; delayMicroseconds(1); Rx_18B20; delayMicroseconds(60); } else { Tx_18B20; Set_Port_18B20_Low; delayMicroseconds(60); Rx_18B20; } } } unsigned char DS18B20_read() { char i = 0, result = 0; Rx_18B20; for(i = 0; i < 8; i++) { Tx_18B20; Set_Port_18B20_Low; delayMicroseconds(2); Rx_18B20; if(Read_Port_18B20 != 0) result |= 1<<i; delayMicroseconds(60); } return result; }
- 程序效果
example2_Arduino
- 主要硬件
- Arduino UNO 控制器
- 傳感器擴(kuò)展板 V5.0
- DS18B20 水溫傳感器
- IIC1602 LCD 液晶顯示器
- 硬件連接
- 示例程序
#include <Wire.h> #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27,16,2); #define Skip_ROM 0xCC #define Convert_T 0x44 #define Read_scratchpad 0xBE #define Port_18B20 7 #define Tx_18B20 pinMode(Port_18B20, OUTPUT) #define Rx_18B20 pinMode(Port_18B20, INPUT) #define Set_Port_18B20_High digitalWrite(Port_18B20, HIGH) #define Set_Port_18B20_Low digitalWrite(Port_18B20, LOW) #define Read_Port_18B20 digitalRead(Port_18B20) unsigned char DS18B20_reset(); void DS18B20_write(char WRT); unsigned char DS18B20_read(); unsigned short tempL = 0; unsigned short tempH = 0; void setup() { Serial.begin(9600); lcd.init(); lcd.backlight(); } void loop() { unsigned int temp = 0; float Temperature = 0.0; if(!DS18B20_reset()) { DS18B20_write(Skip_ROM); DS18B20_write(Convert_T); delay(750); DS18B20_reset(); DS18B20_write(Skip_ROM); DS18B20_write(Read_scratchpad); tempL = DS18B20_read(); tempH = DS18B20_read(); temp = ((unsigned int)tempH << 8) + (unsigned int)tempL; temp = (float)temp * 6.25; Temperature = (float)temp/100; Serial.println(Temperature); lcd.setCursor(0, 0); lcd.print("Temperature ="); lcd.setCursor(0,1); lcd.print(Temperature); lcd.setCursor(6,1); lcd.print("Celsius"); } } unsigned char DS18B20_reset() { Tx_18B20; Set_Port_18B20_Low; delayMicroseconds(480); Rx_18B20; delayMicroseconds(60); if(Read_Port_18B20 == 0) { delayMicroseconds(480); return 0; } else { delayMicroseconds(480); return 1; } } void DS18B20_write(char WRT) { char i = 0, Cmd = 0; Cmd = WRT; Rx_18B20; for(i = 0; i< 8; i++) { if((Cmd & (1<<i)) != 0) { Tx_18B20; Set_Port_18B20_Low; delayMicroseconds(1); Rx_18B20; delayMicroseconds(60); } else { Tx_18B20; Set_Port_18B20_Low; delayMicroseconds(60); Rx_18B20; } } } unsigned char DS18B20_read() { char i = 0, result = 0; Rx_18B20; for(i = 0; i < 8; i++) { Tx_18B20; Set_Port_18B20_Low; delayMicroseconds(2); Rx_18B20; if(Read_Port_18B20 != 0) result |= 1<<i; delayMicroseconds(60); } return result; }
- 程序效果
example3_arduino
- 主要硬件
- Arduino UNO 控制器
- 傳感器擴(kuò)展板V5.0
- LED發(fā)光模塊-紅/綠/黃
- 蜂鳴器發(fā)聲模塊
- 繼電器模塊
- 潛水泵
- DC2.1 電源接線端子
- 7.4V 1300mAh 鋰電池
- 硬件連接
- 示例程序
#define Skip_ROM 0xCC #define Convert_T 0x44 #define Read_scratchpad 0xBE #define Port_18B20 12 //溫度傳感器 #define Tx_18B20 pinMode(Port_18B20, OUTPUT) #define Rx_18B20 pinMode(Port_18B20, INPUT) #define Set_Port_18B20_High digitalWrite(Port_18B20, HIGH) #define Set_Port_18B20_Low digitalWrite(Port_18B20, LOW) #define Read_Port_18B20 digitalRead(Port_18B20) #define LED1 9 //G #define LED2 10 //Y #define LED3 11 //R #define buzzer 7 //蜂鳴器 #define relay 8 //繼電器 unsigned char DS18B20_reset(); void DS18B20_write(char WRT); unsigned char DS18B20_read(); unsigned short tempL = 0; unsigned short tempH = 0; void setup() { Serial.begin(9600); pinMode(LED1,OUTPUT); pinMode(LED2,OUTPUT); pinMode(LED3,OUTPUT); pinMode(buzzer,OUTPUT); pinMode(relay,OUTPUT); digitalWrite(LED1,LOW); digitalWrite(LED2,LOW); digitalWrite(LED3,LOW); digitalWrite(buzzer,HIGH); digitalWrite(relay,LOW); } void loop() { unsigned int temp = 0; float Temperature = 0.0; if(!DS18B20_reset()) { DS18B20_write(Skip_ROM); DS18B20_write(Convert_T); delay(50); DS18B20_reset(); DS18B20_write(Skip_ROM); DS18B20_write(Read_scratchpad); tempL = DS18B20_read(); tempH = DS18B20_read(); temp = ((unsigned int)tempH << 8) + (unsigned int)tempL; temp = (float)temp * 6.25; Temperature = (float)temp/100; Serial.println(Temperature); int val = int(Temperature); if(val>= 30 && val <= 40) { digitalWrite(LED1,LOW); digitalWrite(LED2,HIGH); digitalWrite(LED3,LOW); digitalWrite(buzzer,HIGH); digitalWrite(relay,LOW); } else if(val > 40) { digitalWrite(LED1,LOW); digitalWrite(LED2,LOW); digitalWrite(LED3,HIGH); digitalWrite(buzzer,LOW); digitalWrite(relay,HIGH); } else if(val < 30) { digitalWrite(LED1,HIGH); digitalWrite(LED2,LOW); digitalWrite(LED3,LOW); digitalWrite(buzzer,HIGH); digitalWrite(relay,LOW); } else { digitalWrite(LED1,LOW); digitalWrite(LED2,LOW); digitalWrite(LED3,LOW); digitalWrite(buzzer,HIGH); digitalWrite(relay,LOW); } } } unsigned char DS18B20_reset() { Tx_18B20; Set_Port_18B20_Low; delayMicroseconds(480); Rx_18B20; delayMicroseconds(60); if(Read_Port_18B20 == 0) { delayMicroseconds(480); return 0; } else { delayMicroseconds(480); return 1; } } void DS18B20_write(char WRT) { char i = 0, Cmd = 0; Cmd = WRT; Rx_18B20; for(i = 0; i< 8; i++) { if((Cmd & (1<<i)) != 0) { Tx_18B20; Set_Port_18B20_Low; delayMicroseconds(1); Rx_18B20; delayMicroseconds(60); } else { Tx_18B20; Set_Port_18B20_Low; delayMicroseconds(60); Rx_18B20; } } } unsigned char DS18B20_read() { char i = 0, result = 0; Rx_18B20; for(i = 0; i < 8; i++) { Tx_18B20; Set_Port_18B20_Low; delayMicroseconds(2); Rx_18B20; if(Read_Port_18B20 != 0) result |= 1<<i; delayMicroseconds(60); } return result; }
- 程序效果
此例程可模擬控溫系統(tǒng)
- 若水溫在大于40攝氏度時,LED 紅燈亮起、蜂鳴器發(fā)出報警聲、水泵開始抽水降溫,直到溫度降低40攝氏度以下后,停止抽水。
- 若水溫在30攝氏度和40攝氏度之間時,LED 黃燈亮起、蜂鳴器停止報警、水泵停止抽水。
- 若水溫在30攝氏度以下時,LED 綠燈亮起、蜂鳴器停止報警、水泵停止抽水。
example_Raspberry Pi
- 使用軟件
- 編程軟件:Python 2.7.13
- 操作系統(tǒng):Linux raspberrypi 4.14.50
- 前提:PC 端已通過 SSH 軟件登陸到 RaspberryPi 控制器
- 主要硬件
- Raspberry Pi 3B+
- 16G SD卡
- 5V 2.5A 適配器
- 杜邦線-公母頭
- GPIO 擴(kuò)展板
- 大面包板
- 硬件連接
- 示例程序
樹莓派控制器配置步驟
1.編輯 config.txt 文件,啟用單線接口,默認(rèn)為 BCM4 接口
sudo nano /boot/config.txt
2.加入語句
dtoverlay=w1-gpio
3.重新啟動樹莓派控制器
sudo reboot
4.列出 Raspberry Pi 通過 1-Wire 總線(默認(rèn)為BCM4)發(fā)現(xiàn)的設(shè)備
ls /sys/bus/w1/devices/
如圖所示,所列出的 28-xxx 為連接的 1-Wire 設(shè)備
5.進(jìn)入設(shè)備文件夾,獲取溫度數(shù)值
cd /sys/bus/w1/devices/28-0213169dceaa
cat w1_slave
Python 代碼
import os import glob import time os.system('modprobe w1-gpio') os.system('modprobe w1-therm') base_dir = '/sys/bus/w1/devices/' device_folder = glob.glob(base_dir + '28*')[0] device_file = device_folder + '/w1_slave' def read_temp_raw(): f = open(device_file, 'r') lines = f.readlines() f.close() return lines def read_temp(): lines = read_temp_raw() while lines[0].strip()[-3:] != 'YES': time.sleep(0.2) lines = read_temp_raw() equals_pos = lines[1].find('t=') if equals_pos != -1: temp_string = lines[1][equals_pos+2:] temp_c = float(temp_string) / 1000.0 temp_f = temp_c * 9.0 / 5.0 + 32.0 return temp_c, temp_f while True: print(read_temp()) time.sleep(1)
- 程序效果
在登陸窗口會打印出當(dāng)前的水溫,分別為攝氏溫度和華氏溫度
相關(guān)資料
- DS18B20 溫度傳感器 datasheet & 示例程序
下載鏈接: https://pan.baidu.com/s/1scZ2LKFiCDHJbrT2Se9keg 提取碼:wp3v
* 相關(guān)資料
DS18B20 datasheet
W1-GPIO - One-Wire Interface
- 產(chǎn)品購買鏈接:http://lifestyle201.com/goods-870.html