DS18B20 Temperature Sensor
The DS18B20 digital temperature sensor provides temperature measurements with precision ranging from 9 to 12 bits, and includes a non-volatile, user-programmable upper and lower trigger point alarm function. The DS18B20 communicates via a single-wire bus, which, as defined, only requires one data line (and ground) for communication with the microcontroller. Additionally, the DS18B20 can be powered directly from the data line (via "parasitic power"), eliminating the need for an additional power source. Each DS18B20 has a unique 64-bit serial code, allowing multiple DS18B20s to operate on the same bus. Therefore, it is simple to manage with a microprocessor.
This feature is beneficial for applications such as HVAC (Heating, Ventilation, and Air Conditioning) environmental control, temperature monitoring systems for buildings, equipment, or machinery, as well as process monitoring and control systems.
Module Source
Purchase Link:
https://detail.tmall.com/item.htm?abbucket=10&id=43339849601&ns=1&spm=a21n57.1.item.3.c81c523czOFqCH
Baidu Netdisk Download Link:
https://pan.baidu.com/s/1mNI1Uz58PFMRUx2QIjozwQ
Password:zyis
Specifications
Operating Voltage: 3-5.5V
Operating Current: 750nA~1.5mA
Measurement Resolution: Programmable resolution from 9 to 12 bits
Temperature Range: -55 ~ +125 ℃
Measurement Accuracy: ±0.5 ℃
Communication Protocol: Single-wire bus
Pin Count: 3 Pins (2.54mm pitch header)
Hardware Connection
Red Wire: Connect to the 5V of the development board.
Yellow Wire (Data Line): Connect to pin 2 of the development board. Black Wire: Connect to the GND of the development board.
Usage Method
Install Library
First, make sure you have downloaded and installed the OneWire and DallasTemperature libraries. In the Arduino IDE, you can install these libraries by going to Tools > Manage Libraries..., then search for and install both libraries.
Enter the code:
/******************************************************************************
* Test Hardware: LCSC ColorEasyDuino Development Board
* Version Number: V1.0
* Modified By: www.lckfb.com
* Modification Date: April 9, 2024
* Function Overview:
*****************************************************************************
* Open-source development board hardware and software information and related projects hardware and software information on official website
* Development board official website: www.lckfb.com
* Technical support resident forum, any technical problems are welcome at any time to exchange learning
* LCSC Forum: club.szlcsc.com
* Follow our Bilibili account: [立创开发板], stay toned to our latest news!
* We focus on cultivating Chinese engineers rather than profiting from board sales.
******************************************************************************/
#include <OneWire.h>
#include <DallasTemperature.h>
// 数据线接到开发板 2号引脚
#define ONE_WIRE_BUS 2
// 设置OneWire实例来通讯
OneWire oneWire(ONE_WIRE_BUS);
// 传递oneWire引用到DallasTemperature库
DallasTemperature sensors(&oneWire);
void setup(void) {
// 开启串行通信
Serial.begin(9600);
// 启动温度传感器
sensors.begin();
}
void loop(void) {
// 发送指令以获取所有传感器的温度
sensors.requestTemperatures();
// 打印温度信息,使用索引0,因为假定只有一个DS18B20在总线上
Serial.print("Temperature for the device 1 (index 0) is: ");
Serial.println(sensors.getTempCByIndex(0));
// 稍作延迟,不要太频繁读取
delay(1000);
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44