

The DS18B20 is a digital temperature sensor manufactured by DS18B20. It communicates over a 1-Wire interface, requiring only one data line (and ground) for communication. This sensor provides temperature readings with a resolution of 9 to 12 bits and operates within a temperature range of -55°C to +125°C. Its compact design and ease of use make it ideal for a wide range of applications, including HVAC systems, weather monitoring, industrial temperature control, and home automation.








The DS18B20 typically comes in a 3-pin TO-92 package. Below is the pin configuration:
| Pin Number | Pin Name | Description |
|---|---|---|
| 1 | GND | Ground (0V reference) |
| 2 | DQ | Data line for 1-Wire communication |
| 3 | VDD | Power supply (3.0V to 5.5V) |
Note: The DS18B20 can operate in "parasite power mode," where it draws power from the data line (DQ) instead of requiring a dedicated VDD connection.
Wiring the Sensor:
Communication Protocol:
The DS18B20 uses the 1-Wire protocol, which allows multiple devices to share the same data line. Each sensor has a unique 64-bit serial code, enabling individual addressing.
Reading Temperature:
Below is an example of how to interface the DS18B20 with an Arduino UNO using the popular OneWire and DallasTemperature libraries:
#include <OneWire.h>
#include <DallasTemperature.h>
// Pin connected to the DS18B20 data line
#define ONE_WIRE_BUS 2
// Create a OneWire instance to communicate with the sensor
OneWire oneWire(ONE_WIRE_BUS);
// Pass the OneWire reference to DallasTemperature library
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(9600); // Initialize serial communication
sensors.begin(); // Start the DS18B20 sensor
Serial.println("DS18B20 Temperature Sensor Example");
}
void loop() {
sensors.requestTemperatures(); // Request temperature measurement
float temperatureC = sensors.getTempCByIndex(0); // Get temperature in Celsius
// Check if the reading is valid
if (temperatureC != DEVICE_DISCONNECTED_C) {
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" °C");
} else {
Serial.println("Error: Sensor not detected!");
}
delay(1000); // Wait 1 second before the next reading
}
Explanation of the Code:
OneWire library handles communication with the DS18B20 over the 1-Wire protocol. DallasTemperature library simplifies temperature measurement and data retrieval. getTempCByIndex(0) function retrieves the temperature from the first sensor on the bus.Sensor Not Detected:
Incorrect Temperature Readings:
Multiple Sensors on the Same Bus Not Working:
Q1: Can the DS18B20 operate without a dedicated power supply?
Yes, the DS18B20 can operate in parasite power mode, drawing power from the data line. However, this mode may not work reliably in all setups, especially with long wires or multiple sensors.
Q2: What is the maximum cable length for the DS18B20?
The maximum cable length depends on factors such as wire quality, pull-up resistor value, and power supply. In general, lengths up to 30 meters are achievable with proper wiring and shielding.
Q3: How do I increase the resolution of the temperature readings?
The resolution (9 to 12 bits) can be configured by writing to the sensor's configuration register. Refer to the DS18B20 datasheet for detailed instructions.
Q4: Can the DS18B20 measure negative temperatures?
Yes, the DS18B20 can measure temperatures as low as -55°C. Negative temperatures are represented in two's complement format in the sensor's data output.