DS18B20 Temperature Sensor with Arduino: A Comprehensive Guide

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:

  1. GND: Ground connection.
  2. VCC: Power supply (3.3V or 5V).
  3. 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.

DS18B20-Pinout
DS18B20 Pinout

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.

  1. Data Request: The Arduino sends a request to the DS18B20.
  2. Temperature Conversion: The sensor measures the temperature and converts it into a digital signal.
  3. 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.
DS18B20-Temperature-Sensor-with-Arduino-Wiring
Arduino DS18B20 Wiring Diagram

Required Libraries for DS18B20 Temperature Sensor

To interface the DS18B20 with Arduino, you need the following libraries:

  1. OneWire Library: Facilitates communication with One-Wire devices.
  2. DallasTemperature Library: Specifically designed for DS18B20 sensors.

Installing the Libraries:

  1. Open the Arduino IDE and go to Sketch > Include Library > Manage Libraries.
  2. 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:

DS18B20-Demonstration

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:

  1. Connect all sensors’ DATA pins to the same Arduino pin.
  2. 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

FeatureDS18B20DHT11DHT22LM35
OutputDigitalDigitalDigitalAnalog
Accuracy±0.5°C±2°C±0.5°C±0.5°C
Temperature Range-55°C to 125°C0°C to 50°C-40°C to 80°C-55°C to 150°C
Power3.0V-5.5V3.3V-5V3.3V-5V4V-30V
Humidity SensingNoYesYesNo

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

  1. Parasitic Mode: Power the sensor through the DATA line alone. Suitable for scenarios where wiring additional power is difficult.
  2. 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!


External References


Recommended Links

Leave a Comment