“SKU:RB-02S160 DS18B20 水温传感器”的版本间的差异
(以“右 ==产品概述== DS18B20是常用的数字温度传感器,其输出的是数字信号,具有体积小,硬件开销...”为内容创建页面) |
(→相关资料) |
||
(未显示1个用户的11个中间版本) | |||
第31行: | 第31行: | ||
===example1_Arduino=== | ===example1_Arduino=== | ||
* 主要硬件 | * 主要硬件 | ||
− | + | :Arduino UNO 控制器 | |
+ | :传感器扩展板 V5.0 | ||
+ | :DS18B20 温度传感器 | ||
+ | :单头防插反 3P 传感器连接线 | ||
+ | :USB 数据线 | ||
* 硬件连接 | * 硬件连接 | ||
+ | [[文件: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 控制器 | ||
+ | :传感器扩展板 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 控制器 | ||
+ | :传感器扩展板V5.0 | ||
+ | :LED发光模块-红/绿/黄 | ||
+ | :蜂鸣器发声模块 | ||
+ | :继电器模块 | ||
+ | :潜水泵 | ||
+ | :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> | ||
+ | |||
+ | * 程序效果 | ||
+ | 此例程可模拟控温系统<br/> | ||
+ | :若水温在大于40摄氏度时,LED 红灯亮起、蜂鸣器发出报警声、水泵开始抽水降温,直到温度降低40摄氏度以下后,停止抽水。 | ||
+ | :若水温在30摄氏度和40摄氏度之间时,LED 黄灯亮起、蜂鸣器停止报警、水泵停止抽水。 | ||
+ | :若水温在30摄氏度以下时,LED 绿灯亮起、蜂鸣器停止报警、水泵停止抽水。 | ||
+ | |||
+ | ===example_Raspberry Pi=== | ||
+ | * 使用软件 | ||
+ | :编程软件:Python 2.7.13 | ||
+ | :操作系统:Linux raspberrypi 4.14.50 | ||
+ | :前提:PC 端已通过 SSH 软件登陆到 RaspberryPi 控制器 | ||
+ | |||
+ | * 主要硬件 | ||
+ | :Raspberry Pi 3B+ | ||
+ | :16G SD卡 | ||
+ | :5V 2.5A 适配器 | ||
+ | :杜邦线-公母头 | ||
+ | :GPIO 扩展板 | ||
+ | :大面包板 | ||
* 硬件连接 | * 硬件连接 | ||
+ | [[文件:02S16008.png|500px|缩略图|居中]] | ||
* 示例程序 | * 示例程序 | ||
+ | 树莓派控制器配置步骤<br/> | ||
+ | 1.编辑 config.txt 文件,启用单线接口,默认为 BCM4 接口<br/> | ||
+ | sudo nano /boot/config.txt <br/> | ||
+ | 2.加入语句<br/> | ||
+ | dtoverlay=w1-gpio<br/> | ||
+ | 3.重新启动树莓派控制器<br/> | ||
+ | sudo reboot<br/> | ||
+ | 4.列出 Raspberry Pi 通过 1-Wire 总线(默认为BCM4)发现的设备<br/> | ||
+ | ls /sys/bus/w1/devices/<br/> | ||
+ | 如图所示,所列出的 28-xxx 为连接的 1-Wire 设备<br/> | ||
+ | [[文件:02S16009.png|500px|缩略图|居中]] | ||
+ | 5.进入设备文件夹,获取温度数值<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> | ||
* 程序效果 | * 程序效果 | ||
+ | 在登陆窗口会打印出当前的水温,分别为摄氏温度和华氏温度 | ||
+ | [[文件:02S16011.png|600px|缩略图|居中]] | ||
==相关资料== | ==相关资料== | ||
+ | [[文件:erweima.png|230px|无框|右]] | ||
* DS18B20 温度传感器 datasheet & 示例程序 | * DS18B20 温度传感器 datasheet & 示例程序 | ||
− | + | 下载链接: https://pan.baidu.com/s/1scZ2LKFiCDHJbrT2Se9keg | |
+ | 提取码:wp3v | ||
+ | * 相关资料 | ||
+ | [https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf DS18B20 datasheet] <br/> | ||
+ | [https://pinout.xyz/pinout/1_wire W1-GPIO - One-Wire Interface] <br/> | ||
+ | * 产品购买链接:http://www.alsrobot.cn/goods-870.html |
2021年6月22日 (二) 10:21的最后版本
目录 |
产品概述
DS18B20是常用的数字温度传感器,其输出的是数字信号,具有体积小,硬件开销低,抗干扰能力强,精度高的特点。 DS18B20数字温度传感器接线方便,封装成后可应用于多种场合,如管道式,螺纹式,磁铁吸附式,不锈钢封装式等。主要根据应用场合的不同而改变其外观。
DS18B20水温水温传感器采用的是不锈钢封装,防水防潮。封装后的DS18B20可用于电缆沟测温,高炉水循环测温,锅炉测温,机房测温,农业大棚测温,洁净室测温,弹药库测温等各种非极限温度场合,适用于各种狭小空间设备数字测温和控制领域。
产品参数
基本参数
- 品名:DS18B20水温传感器
- 货号:RB-02S160
- 品牌:奥松机器人
- 产地:哈尔滨
- 固定孔:M3*4
- 电缆长度:90cm
- 传感器:直径6mm,长30mm
电气参数
1.接口类型:3P防插反接口
2.信号类型:数字信号
3.工作电压:3V - 5.5V
4.引脚定义:
- +:电源正极
- -:电源负极
- S:信号端
5.传感器接口:KF2510-3P防插反接口
6.测试范围:-55℃~+125℃(-67°F至+ 257°F)
7.测量精度:在-10~+85℃范围内精确度为±5℃
8.测量分辨率:温度计分辨率可以被使用者选择为9~12位
使用方法
example1_Arduino
- 主要硬件
- Arduino UNO 控制器
- 传感器扩展板 V5.0
- DS18B20 温度传感器
- 单头防插反 3P 传感器连接线
- USB 数据线
- 硬件连接
- 示例程序
#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 控制器
- 传感器扩展板 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 控制器
- 传感器扩展板V5.0
- LED发光模块-红/绿/黄
- 蜂鸣器发声模块
- 继电器模块
- 潜水泵
- 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; }
- 程序效果
此例程可模拟控温系统
- 若水温在大于40摄氏度时,LED 红灯亮起、蜂鸣器发出报警声、水泵开始抽水降温,直到温度降低40摄氏度以下后,停止抽水。
- 若水温在30摄氏度和40摄氏度之间时,LED 黄灯亮起、蜂鸣器停止报警、水泵停止抽水。
- 若水温在30摄氏度以下时,LED 绿灯亮起、蜂鸣器停止报警、水泵停止抽水。
example_Raspberry Pi
- 使用软件
- 编程软件:Python 2.7.13
- 操作系统:Linux raspberrypi 4.14.50
- 前提:PC 端已通过 SSH 软件登陆到 RaspberryPi 控制器
- 主要硬件
- Raspberry Pi 3B+
- 16G SD卡
- 5V 2.5A 适配器
- 杜邦线-公母头
- GPIO 扩展板
- 大面包板
- 硬件连接
- 示例程序
树莓派控制器配置步骤
1.编辑 config.txt 文件,启用单线接口,默认为 BCM4 接口
sudo nano /boot/config.txt
2.加入语句
dtoverlay=w1-gpio
3.重新启动树莓派控制器
sudo reboot
4.列出 Raspberry Pi 通过 1-Wire 总线(默认为BCM4)发现的设备
ls /sys/bus/w1/devices/
如图所示,所列出的 28-xxx 为连接的 1-Wire 设备
5.进入设备文件夹,获取温度数值
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)
- 程序效果
在登陆窗口会打印出当前的水温,分别为摄氏温度和华氏温度
相关资料
- DS18B20 温度传感器 datasheet & 示例程序
下载链接: https://pan.baidu.com/s/1scZ2LKFiCDHJbrT2Se9keg 提取码:wp3v
* 相关资料