We offer a wide range of the best 1-Wire DS18B20 sensor connectors, including Nanoflex, DisplayPort, USB, Solar, SATA, HDMI, ATA IDE, SAS & many more. All cables are manufactured to the highest industry standards. Using Sensor Circuit Assembly for box builds allows you to focus on your design and marketing, reduce costs, and reap the benefits of our assembly lines, QA processes, and manufacturing expertise.
The DS18B20 sensor communicates using the “1-Dây điện” protocol, which means it uses a single data line for all communication with a microcontroller, allowing multiple sensors to be connected on the same line and identified by their unique 64-bit serial code; this single data line is pulled high with a resistor and the sensor transmits data by pulling the line low during specific time slots to send bits of information.
Cảm biến nhiệt độ DS18B20: The DS18B20 waterproof probe is designed for underwater use, capable of operating in wet or moist environments without being damaged by water or moisture.
Temperature sensor supply voltage: 3.0V ~ 5.25V;
Phạm vi nhiệt độ hoạt động:-55 ℃ to +125 oC (-67 ℉ to +257 ℉);
Provides from 9-bit to 12-bit Celsius temperature measurements;
Adapter module is equipped with a pull-up resistor, and directly connects to the GPIO of the Raspberry Pi without an external resistor;
Use this adapter module kit to simplify connecting the waterproof temperature sensor to your project.
1. Key points about the 1-Wire protocol:
Single data line:
Only one wire is needed for communication between the sensor and the microcontroller.
Half-duplex communication:
Data can be sent in both directions, but only one direction at a time.
Parasite power:
The DS18B20 can be powered directly from the data line during communication, eliminating the need for a separate power supply in some cases.
Unique device addresses:
Each DS18B20 sensor has a unique 64-bit serial code that allows the microcontroller to identify and address individual sensors on the bus.
Communication steps with a DS18B20:
1.1 Reset pulse:
The microcontroller initiates communication by pulling the data line low for a specific duration (reset pulse).
1.2 Presence pulse:
If a DS18B20 is present on the bus, it will respond with a short pulse, indicating its presence.
1.3 ROM command:
The microcontroller sends a ROM command to either read the unique 64-bit code of a specific sensor (“Match ROM”) or to address all sensors on the bus (“Skip ROM”).
1.4 Function command:
Depending on the desired operation (like reading temperature), the microcontroller sends a specific function command to the sensor.
1.5 Data transfer:
Data is transmitted bit-by-bit, with the sensor pulling the data line low to send a ‘0’ and letting the line go high to send a ‘1’.
2. Giải thích chi tiết về giao thức truyền thông 1 dây DS18B20
Lý do tại sao các cảm biến DS18B20 được sử dụng rộng rãi phần lớn là do giao thức truyền thông độc đáo của nó – 1-Giao thức giao tiếp dây. Giao thức này đơn giản hóa các yêu cầu cho các kết nối phần cứng và cung cấp một cách hiệu quả để truyền dữ liệu. Chương này sẽ phân tích sâu sắc cơ chế làm việc và quá trình trao đổi dữ liệu của giao thức truyền thông 1 dòng để đặt nền tảng vững chắc cho thực tiễn lập trình tiếp theo.
2.1 Khái niệm cơ bản về giao thức giao tiếp 1 dây
2.1.1 Các tính năng của giao thức giao tiếp 1 dây:
Giao thức giao tiếp DS18B20 1 dây cũng được gọi “Xe buýt đơn” công nghệ. Nó có các tính năng sau: – Giao tiếp xe buýt đơn: Chỉ có một dòng dữ liệu được sử dụng để truyền dữ liệu hai chiều, làm giảm đáng kể sự phức tạp của hệ thống dây điện so với phương pháp giao tiếp cảm biến đa dây truyền thống. – Kết nối nhiều thiết bị: Hỗ trợ kết nối nhiều thiết bị trên một bus dữ liệu, và xác định và giao tiếp thông qua mã nhận dạng thiết bị. – Tiêu thụ năng lượng thấp: Trong quá trình giao tiếp, Thiết bị có thể ở trạng thái chờ công suất thấp khi không tham gia giao tiếp. – Độ chính xác cao: Với thời gian truyền dữ liệu ngắn hơn, Nó có thể giảm nhiễu bên ngoài và cải thiện độ chính xác của dữ liệu.
2.1.2 Định dạng dữ liệu và phân tích thời gian của giao tiếp 1 dây
Định dạng dữ liệu của giao thức giao tiếp 1 dây tuân theo một quy tắc thời gian cụ thể. Nó bao gồm thời gian khởi tạo, Viết thời gian và đọc thời gian:
Thời gian khởi tạo: Máy chủ trước tiên bắt đầu thời gian phát hiện sự hiện diện (Xung hiện diện) bằng cách kéo xe buýt xuống trong một khoảng thời gian nhất định, và cảm biến sau đó gửi xung hiện diện để đáp ứng.
Viết thời gian: Khi máy chủ gửi thời gian ghi, đầu tiên nó kéo xe buýt xuống khoảng 1-15 micro giây, Sau đó phát hành xe buýt, và cảm biến kéo xe buýt xuống 60-120 micro giây để trả lời.
Đọc thời gian: Máy chủ thông báo cho cảm biến để gửi dữ liệu bằng cách kéo xe buýt xuống và phát hành nó, và cảm biến sẽ xuất bit dữ liệu trên xe buýt sau một sự chậm trễ nhất định.
2.2 Software implementation of data communication
2.2.1 Initialization and reset of 1-line communication
At the software level, initialization and reset of 1-Wire communication is the first step of communication. The following is the pseudo code to implement this process:
// OneWire communication initialization function
void OneWire_Init() {
// Set the bus to input mode and enable the pull-up resistor
SetPinMode(DS18B20_PIN, INPUT_PULLUP);
// Wait for the bus to be idle
DelayMicroseconds(1);
// Send a reset pulse
OneWire_Reset();
}
// OneWire communication reset function
void OneWire_Reset() {
// Pull down the bus
SetPinMode(DS18B20_PIN, OUTPUT_LOW);
DelayMicroseconds(480);
// Release the bus
SetPinMode(DS18B20_PIN, INPUT_PULLUP);
DelayMicroseconds(70);
// Wait for the presence of a pulse
nếu như (!WaitForOneWirePresence())
// No pulse was detected, maybe the sensor is not connected or the initialization failed
HandleError();
DelayMicroseconds(410);
}
// Waiting for the presence of a pulse
bool WaitForOneWirePresence() {
return ReadPin(DS18B20_PIN) == 0; // Assume low level is a signal presence
}
2.2.2 Data reading and writing operations
Data reading and writing operations are the core part of sensor communication. The following code shows how to write a byte to a one-wire bus:
// Write a byte to a one-wire bus
void OneWire_WriteByte(byte data) {
for (int i = 0; i < 8; i++) {
OneWire_WriteBit(data & 0x01);
data >>= 1;
}
}
// Write a bit to a one-wire bus
void OneWire_WriteBit(bit data) {
SetPinMode(DS18B20_PIN, OUTPUT_LOW);
nếu như (data) {
// Release the bus when writing 1
SetPinMode(DS18B20_PIN, INPUT_PULLUP);
DelayMicroseconds(1);
} khác {
// Continue to pull the bus low when writing 0
DelayMicroseconds(60);
}
SetPinMode(DS18B20_PIN, INPUT_PULLUP);
DelayMicroseconds(1);
}
Next is the function to read a byte:
// Read a byte from the one-wire bus
byte OneWire_ReadByte() {
byte data = 0;
for (int i = 0; i < 8; i++) {
data >>= 1;
nếu như (OneWire_ReadBit())
data |= 0x80;
}
return data;
}
// Read a bit from the one-wire bus
bit OneWire_ReadBit() {
SetPinMode(DS18B20_PIN, OUTPUT_LOW);
SetPinMode(DS18B20_PIN, INPUT_PULLUP);
DelayMicroseconds(3);
bool result = ReadPin(DS18B20_PIN);
DelayMicroseconds(57);
return result;
}
2.2.3 Verification mechanism of OneWire communication
The OneWire communication protocol uses a simple verification mechanism in the data exchange process, usually by reading back the written data to verify the correctness of the data. The following is a sample code for verifying the written data:
byte data = 0x55; // Assume that the data to be sent
OneWire_WriteByte(data); // Write data to the OneWire bus
byte readData = OneWire_ReadByte(); // Read back data from the OneWire bus
nếu như (readData != data) {
HandleError(); // If the read-back data does not match the written data, handle the error