DS18B20
材料:NodeMcu
,DS18B20模块(三线),
杜邦线三根(母对母)`。
软件:Arduino(配置好ESP环境)
arduino库:DS18B20
,OneWire
连线:+ 3.3v
,- GND
,DAT D4
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2 // DS18B20 on NodeMCU pin D4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
long lastMsg = 0;
float temp_0;
float temp_1;
void setup()
{
Serial.begin(115200);
DS18B20.begin();
}
void loop() {
long now = millis();
if (now - lastMsg > 3000) {
lastMsg = now;
Serial.print("3s发送数据\n");
// Wait a few seconds between measurements
DS18B20.requestTemperatures();
temp_0 = DS18B20.getTempCByIndex(0); // Sensor 0 will capture Temp in Celcius
temp_1 = DS18B20.getTempFByIndex(1); // Sensor 0 will capture Temp in Fahrenheit
Serial.print("Temp_0: ");
Serial.print(temp_0);
Serial.print(" C . Temp_1: ");
Serial.print(temp_1);
Serial.println(" F\n");
}
}
串口效果展示
如果有用点个赞再走