SkyzcStack|正材棧區

  • 首页
  • 文章归档
  • 默认分类
  • 关于页面

  • 搜索
Docker Algorithm Ag Golang LeetCode Golnag 传感器 ESP8266 Resume SkyzcYou 软考 C/C++ Interview Python 面试 Java Eclipse IDE Android MySQL Ubuntu Linux

ESP8266-NodeMCU 获取 DS18B20 温度传感器数据

发表于 2021-04-13 | 分类于 Notes | 0 | 阅读次数 2059

ESP8266-NodeMCU 获取 DS18B20 温度传感器数据

1. 电路连接图

ds18b20_esp8266_single_parasite.png

2. 安装所需库

  1. 打开 Arduino IDE,工具>管理库

  2. 安装 OneWire 库(确定作者)

image20210130224029624.png

  1. 安装 DallasTemperature库

image20210130224150570.png

3. 实现代码

#include <OneWire.h>
#include <DallasTemperature.h>

// 连接 DS18B20 的 GPIO 引脚编号
const int oneWireBus = 2;     

// 设置一个OneWire 实例以与任何OneWire设备通信
OneWire oneWire(oneWireBus);

// 将oneWire传递给Dallas
DallasTemperature sensors(&oneWire);

void setup() {
  pinMode(D3, OUTPUT);     // 初始化D3引脚,控制 LED小灯闪烁
  // 启动串行监视器
  Serial.begin(9600);
  // 启动DS18B20传感器
  sensors.begin();
}

void loop() {
  // 控制 LED 闪烁
  digitalWrite(D3, LOW);
  delay(1000);                      
  digitalWrite(D3, HIGH);  

  // 获取并输出温度到串口
  sensors.requestTemperatures(); 
  float temperatureC = sensors.getTempCByIndex(0);
  float temperatureF = sensors.getTempFByIndex(0);
  Serial.print(temperatureC);
  Serial.println("ºC");
  Serial.print(temperatureF);
  Serial.println("ºF");
  delay(5000);
}

4. 实现效果

image20210130224622719.png

参考:ESP8266 DS18B20 Temperature Sensor with Arduino IDE (Single, Multiple, Web Server)

  • 本文作者: SkyzcYou
  • 本文链接: /archives/esp8266-nodemcuhuo-qu-ds18b20wen-du-chuan-gan-qi-shu-ju
  • 版权声明: 本博客所有文章除特别声明外,均采用CC BY-NC-SA 3.0 许可协议。转载请注明出处!
# Docker # Algorithm # Ag # Golang # LeetCode # Golnag # 传感器 # ESP8266 # Resume # SkyzcYou # 软考 # C/C++ # Interview # Python # 面试 # Java # Eclipse # IDE # Android # MySQL # Ubuntu # Linux
利用 Clion 打造你的 C/C++ 一站式开发环境-01
Docker 学习之 image 镜像原理
  • 文章目录
  • 站点概览
SkyzcYou

SkyzcYou

27 日志
4 分类
22 标签
RSS
Github E-mail
Creative Commons
© 2022 SkyzcYou
由 Halo 强力驱动
|
主题 - NexT.Gemini v5.1.4

黔ICP备17002491号 - 2