ds18b20的显示代码
时间: 2024-01-05 15:04:19 浏览: 69
作为AI语言的模型,我无法编写实际代码。但是,以下是DS18B20温度传感器的示例代码,可以在Arduino等平台上运行:
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
void setup(void)
{
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
sensors.begin();
}
void loop(void)
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
// After we got the temperatures, we can print them here.
// We use the function ByIndex, and as an example get the temperature from the first sensor only.
Serial.print("Temperature for the device 1 (index 0) is: ");
Serial.println(sensors.getTempCByIndex(0));
delay(1000);
}
这段代码使用OneWire和DallasTemperature库来读取DS18B20传感器的温度值,并将其打印到串行监视器中。您需要将传感器连接到Arduino的2号引脚,并将串行监视器的波特率设置为9600。
阅读全文