The DS18B20 is a precision digital temperature sensor that provides 9-bit to 12-bit Celsius temperature measurements. It communicates over a 1-Wire bus that by design requires only one data line (and ground) for communication with a central microprocessor. It has an operating temperature range of -55°C to +125°C and is accurate to ±0.5°C in the range of -10°C to +85°C. Additionally, the DS18B20 can derive power directly from the data line ("parasite power"), eliminating the need for an external power supply.
Common applications of the DS18B20 include HVAC environmental controls, temperature monitoring systems inside buildings, equipment or machinery, and process monitoring and control systems.
Pin Number | Name | Description |
---|---|---|
1 | GND | Ground |
2 | DQ | Data Line |
3 | VDD | Power Supply (3.0V to 5.5V) |
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is connected to Arduino digital pin 2
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass oneWire reference to DallasTemperature library
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(9600);
sensors.begin(); // Start up the library
}
void loop() {
sensors.requestTemperatures(); // Send command to get temperatures
Serial.print("Temperature is: ");
Serial.print(sensors.getTempCByIndex(0)); // Get temperature in Celsius
Serial.println("°C");
delay(1000); // Wait 1 second before next reading
}
sensors.getAddress()
function to verify the unique address of each DS18B20 sensor.Q: Can I connect multiple DS18B20 sensors to the same data line? A: Yes, each DS18B20 has a unique 64-bit serial code, which allows multiple sensors to function on the same 1-Wire bus.
Q: How do I set the resolution of the DS18B20?
A: The resolution can be set using the sensors.setResolution()
function in the DallasTemperature library.
Q: What is the maximum cable length for the DS18B20? A: The maximum cable length depends on several factors, including the quality of the cable and the pull-up resistor value. Generally, lengths up to 100 meters are possible with careful design.
Q: Is it necessary to use an external power supply for the DS18B20? A: No, the DS18B20 can operate in parasite power mode, where it draws power from the data line. However, an external power supply can be used if preferred.