The DS18B20 temperature sensor is a popular choice for projects requiring accurate temperature measurements. Its ability to communicate over a single data line and unique serial code for multiple sensor setups makes it ideal for advanced applications. In this guide, we’ll explore every detail about the DS18B20 temperature sensor and how to use it with Arduino, while also comparing it with other temperature sensors like the DHT11, DHT22, and LM35.
What is the DS18B20 Temperature Sensor?
The DS18B20 is a digital temperature sensor designed by Maxim Integrated. It offers high accuracy and supports multiple sensors on a single data line using the One-Wire protocol. Available in both waterproof and non-waterproof versions, this sensor is perfect for monitoring room, outdoor, and liquid temperatures.

Key Features:
- Temperature Range: -55°C to +125°C.
- Accuracy: ±0.5°C in the range of -10°C to +85°C.
- Resolution: Programmable from 9 to 12 bits.
- Power Supply: 3.0V to 5.5V.
- Unique 64-bit Serial Code: Enables multiple sensors to share the same data line.
- Waterproof Variant: Ideal for liquid temperature measurements.
DS18B20 Pinout
The DS18B20 sensor has three pins:
- GND: Ground connection.
- VCC: Power supply (3.3V or 5V).
- DATA: Digital signal pin used for communication.
Note: Use a 4.7k pull-up resistor between the DATA pin and VCC to ensure reliable communication.

How Does the DS18B20 Work?
The DS18B20 uses the One-Wire protocol to communicate with the microcontroller. This protocol requires only one data line for both data transmission and power (in parasitic mode). Each sensor has a unique 64-bit serial code, making it possible to connect multiple DS18B20 sensors to the same Arduino pin.
- Data Request: The Arduino sends a request to the DS18B20.
- Temperature Conversion: The sensor measures the temperature and converts it into a digital signal.
- Data Transmission: The DS18B20 sends the temperature data back to the Arduino.
Circuit Diagram
Here’s how to connect the DS18B20 to an Arduino Uno:
- VCC to 5V
- GND to GND
- DATA to Digital Pin 2
- Add a 4.7k pull-up resistor between the DATA and VCC pins.

Required Libraries for DS18B20 Temperature Sensor
To interface the DS18B20 with Arduino, you need the following libraries:
- OneWire Library: Facilitates communication with One-Wire devices.
- DallasTemperature Library: Specifically designed for DS18B20 sensors.
Installing the Libraries:
- Open the Arduino IDE and go to Sketch > Include Library > Manage Libraries.
- Search for “OneWire” and “DallasTemperature.”


3. Install both libraries.
Arduino Code Example
// DS18B20 Temperature Sensor with Arduino by ArduinoYard
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2 // Data pin connected to pin 2
OneWire oneWire(ONE_WIRE_BUS); // Setup OneWire instance
DallasTemperature sensors(&oneWire); // Pass OneWire instance to DallasTemperature
void setup() {
Serial.begin(9600); // Initialize serial communication
sensors.begin(); // Start the DallasTemperature library
}
void loop() {
sensors.requestTemperatures(); // Request temperature readings from the sensor
float temperature = sensors.getTempCByIndex(0); // Get temperature of the first sensor
// Print the temperature reading to the Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
delay(1000); // Wait for 1 second before taking the next reading
}
Demonstration
Here’s an example setup and the corresponding serial monitor output:
Hardware Setup:

Serial Monitor Output:

Using Multiple DS18B20 Sensors
Thanks to the unique 64-bit serial code of each DS18B20, you can connect multiple sensors to the same data pin. The DallasTemperature library allows easy identification and management of individual sensors.
Steps for Multiple Sensors:
- Connect all sensors’ DATA pins to the same Arduino pin.
- Modify the code to retrieve the serial address of each sensor.
Troubleshooting Common Issues
1. Sensor Not Detected:
- Ensure the pull-up resistor (4.7k) is properly connected between the DATA and VCC pins.
- Verify the wiring and check for loose connections.
- Confirm the correct pin number in the code.
2. Incorrect Temperature Readings:
- Check for stable power supply to the sensor.
- Avoid long cable lengths that may cause signal degradation.
- Ensure there are no sources of electrical interference near the sensor.
3. Data Loss in Long Cables:
- Use a lower resistance pull-up resistor (e.g., 2.2k) for improved signal strength.
- Use shielded cables or add decoupling capacitors for signal integrity.
4. Multiple Sensors Not Working:
- Verify each sensor’s unique address using the DallasTemperature library.
- Confirm all sensors share the same DATA pin but are correctly powered.
Comparing DS18B20 Temperature Sensor with Other Sensors
Feature | DS18B20 | DHT11 | DHT22 | LM35 |
---|---|---|---|---|
Output | Digital | Digital | Digital | Analog |
Accuracy | ±0.5°C | ±2°C | ±0.5°C | ±0.5°C |
Temperature Range | -55°C to 125°C | 0°C to 50°C | -40°C to 80°C | -55°C to 150°C |
Power | 3.0V-5.5V | 3.3V-5V | 3.3V-5V | 4V-30V |
Humidity Sensing | No | Yes | Yes | No |
Applications of DS18B20 Temperature Sensor
- Weather Stations: Measure outdoor temperatures.
- Liquid Temperature Monitoring: Use the waterproof variant for aquariums, water tanks, or industrial liquids.
- IoT Projects: Combine with Wi-Fi modules for remote temperature monitoring.
Advanced Topics
- Parasitic Mode: Power the sensor through the DATA line alone. Suitable for scenarios where wiring additional power is difficult.
- Long Cable Connections: Use capacitors and resistors for signal integrity.
Conclusion
The DS18B20 temperature sensor stands out for its accuracy, ease of use, and versatility in both beginner and advanced projects. Its ability to work in both waterproof and non-waterproof environments makes it a go-to sensor for a wide range of applications, from IoT to industrial systems. With its One-Wire protocol and unique addressing capability, it simplifies setups involving multiple sensors. Whether you are monitoring room temperature or creating a weather station, the DS18B20 is an excellent choice for precision and reliability.
This detailed guide should help you fully utilize the capabilities of the DS18B20. Be sure to experiment, innovate, and integrate this sensor into your projects to explore its full potential!