DS18B20温度传感器控制的机箱散热

DS18B20温度传感器控制的机箱散热

一、参考资料:
1、温度传感器及感温杯实验:http://www.cnblogs.com/xiaowuyi/p/3395326.html
2、DS18B20数字温度传感器实验(℃格式):https://www.arduino.cn/forum.php?mod=viewthread&tid=82106&highlight=18b20
3、Arduino教程——DS18B20温度传感器 :https://www.arduino.cn/forum.php?mod=viewthread&tid=1345&highlight=18b20
二、模块说明:
DS18B20数字温度传感器是美国DALLAS公司生产的单总线数字温度传感器。DSl820数字温度计提供 9 位(二进制)温度读数 指示器件的温度 信息经过单线接口送 入 DSl8B20 或从 DSl8B20 送出 因此从主机 CPU 到DSl820 仅需一条线(和地线) DSl820 的电源可以由数据线本身提供而不需要外部电源 因为每一个 DSl820 在出厂时已经给定了唯一的序号 因此任意多个 DSl820 可以存放在同一条单线总线上 这允许在许多不同的地方放置温度敏感器件 DSl820 的测量范围从-55 到+125 增量值为 0.5 可在 l s(典型值)内把温度变换成数字 。简单的理DS18B20测温原理就是说把芯片把感知到的温度换成数值放在数据寄存器里面,要想得到寄存器里面的数据,只有按照DALLAS规定的一种时序才能正确传出数据,这种时序被称为单总线,cpu就可通过单总线协议,取得DS18B20里面的温度值。
18b20传感器的引脚说明
与Arduino的接线方式
实际连接图

电阻连接

三、代码:
2019-2-21:打开IDE中的示例中的OneWire,中的例子载入,运行返回正常。实验成功,注意:电阻一定要10K的,正确连接,否则18B20是不能正常工作的。
四、连接图:
五、
源代码:注,复制IDE中的例程代码
#include <OneWire.h>

// OneWire DS18S20, DS18B20, DS1822 Temperature Example
//
// http://www.pjrc.com/teensy/td_libs_OneWire.html
//
// The DallasTemperature library can do all this work for you!
// http://milesburton.com/Dallas_Temperature_Control_Library

OneWire ds(10); // on pin 10

void setup(void) {
Serial.begin(9600);
}

void loop(void) {
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;

if ( !ds.search(addr)) {
Serial.println(“No more addresses.”);
Serial.println();
ds.reset_search();
delay(250);
return;
}

Serial.print(“ROM =”);
for( i = 0; i < 8; i++) {
Serial.write(’ ');
Serial.print(addr[i], HEX);
}

if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.println(“CRC is not valid!”);
return;
}
Serial.println();

// the first ROM byte indicates which chip
switch (addr[0]) {
case 0x10:
Serial.println(" Chip = DS18S20"); // or old DS1820
type_s = 1;
break;
case 0x28:
Serial.println(" Chip = DS18B20");
type_s = 0;
break;
case 0x22:
Serial.println(" Chip = DS1822");
type_s = 0;
break;
default:
Serial.println(“Device is not a DS18x20 family device.”);
return;
}

ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end

delay(1000); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.

present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad

Serial.print(" Data = “);
Serial.print(present,HEX);
Serial.print(” “);
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
Serial.print(data[i], HEX);
Serial.print(” “);
}
Serial.print(” CRC=");
Serial.print(OneWire::crc8(data, 8), HEX);
Serial.println();

// convert the data to actual temperature

unsigned int raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// count remain gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
if (cfg == 0x00) raw = raw << 3; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
fahrenheit = celsius * 1.8 + 32.0;
Serial.print(" Temperature = “);
Serial.print(celsius);
Serial.print(” Celsius, “);
Serial.print(fahrenheit);
Serial.println(” Fahrenheit");
}

if (celsius >22) //temperature变量为转换后得到的温度。 如果温度高于22度,则给继电器信号开。
{
digitalWrite(LED, HIGH);

}
  else if (celsius <20)    
           {
           digitalWrite(LED, LOW);
           }

四、完成图片,温度设置为22度时启动机箱散热风扇,低于20度时停止。中间有个2度的范围,是为了防止有时候温度读取有小范围波动而导致快速启停。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值