arduino 18b20传感器程序编写
时间: 2023-08-02 16:05:54 浏览: 51
下面是一个简单的Arduino代码,用于读取DS18B20温度传感器的值:
```C++
#include <OneWire.h> //引用OneWire库
#define ONE_WIRE_BUS 2 //设置数据引脚
OneWire oneWire(ONE_WIRE_BUS); //创建OneWire对象
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 ( !oneWire.search(addr)) {
Serial.println("No more addresses.");
oneWire.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();
// 根据芯片类型决定温度值所在的字节位置
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;
}
oneWire.reset();
oneWire.select(addr);
oneWire.write(0x44,1); // 开始转换温度值
delay(1000); // 等待转换完成
present = oneWire.reset();
oneWire.select(addr);
oneWire.write(0xBE); // 读取温度值
for ( i = 0; i < 9; i++) { // 将数据存入data数组
data[i] = oneWire.read();
}
int16_t 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);
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 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");
}
```
这个代码使用了OneWire库,所以你需要先下载并安装这个库。你可以在Arduino IDE中的"工具"-"库管理器"中搜索OneWire进行安装。
此代码使用的是DS18B20传感器,如果你使用的是DS18S20或DS1822,需要相应地修改代码中的芯片类型。此外,你还需要将数据引脚连接到Arduino板上。在上面的代码中,数据引脚被设置为2。如果你使用的是其他引脚,请相应地修改代码中的ONE_WIRE_BUS宏定义。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)