ESP8266 ds18b20温度传感器

先到GitHub上下载库OneWire和DallasTemperature

下载好的例程,此程序是arduino上的例程,可以修改引脚为ESP8266的引脚

// Include the libraries we need
#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 (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

/*
 * The setup function. We only start the sensors here
 */
void setup(void)
{
  // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();
}

/*
 * Main function, get and show the temperature
 */
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.
  float tempC = sensors.getTempCByIndex(0);

  // Check if reading was successful
  if(tempC != DEVICE_DISCONNECTED_C) 
  {
    Serial.print("Temperature for the device 1 (index 0) is: ");
    Serial.println(tempC);
  } 
  else
  {
    Serial.println("Error: Could not read temperature data");
  }
}

版权声明:本文为CSDN博主「lcp0633」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lcp0633/article/details/122690550

生成海报
点赞 0

lcp0633

我还没有学会写个人说明!

暂无评论

发表评论

相关推荐

蓝桥杯之DS18B20

1、显示整数  #include #include //***************定义DS18B24引脚************* sbit DQP1^4; //*********

SQL585A是一款电子点烟器咪头专用芯片

概述 SQL585A是一款电子点烟器专用芯片。 SQL585A集成 4.0A的放电MOS开关,无需外部MOS,大大降低BOM成本。放电支持 10S超时保护, 以及微秒级快速响应的短路保护,

8x8LED点阵

点量这个只需要把9高电平,13低电平就可以了 共阳极点阵 行线是led的正极 列线是led的列线 左上角点亮 显示多个灯是动态扫描的,一个一个显示的,然后间隔速度要快就可以造成显示 点阵由两篇7