How to Use DS18B20 Temperature Sensor with Arduino Easily
To use the
DS18B20 temperature sensor with Arduino, connect the sensor's data pin to a digital pin with a 4.7k ohm pull-up resistor to 5V. Use the OneWire and DallasTemperature libraries to read temperature values easily in your Arduino sketch.Syntax
The DS18B20 sensor communicates using the OneWire protocol. You need to include the OneWire library to handle this communication and the DallasTemperature library to simplify reading temperature data.
Key parts of the code:
OneWire oneWire(pin);- Initializes OneWire on the chosen Arduino pin.DallasTemperature sensors(&oneWire);- Creates a sensor object to manage DS18B20.sensors.begin();- Starts communication with the sensor.sensors.requestTemperatures();- Tells the sensor to measure temperature.sensors.getTempCByIndex(0);- Reads the temperature in Celsius from the first sensor.
arduino
#include <OneWire.h> #include <DallasTemperature.h> // Data wire is plugged into pin 2 on the Arduino #define ONE_WIRE_BUS 2 // Setup a oneWire instance to communicate with any OneWire devices OneWire oneWire(ONE_WIRE_BUS); // Pass our oneWire reference to Dallas Temperature sensor DallasTemperature sensors(&oneWire); void setup() { Serial.begin(9600); sensors.begin(); } void loop() { sensors.requestTemperatures(); // Send the command to get temperatures float temperatureC = sensors.getTempCByIndex(0); // Read temperature Serial.print("Temperature: "); Serial.print(temperatureC); Serial.println(" °C"); delay(1000); }
Output
Temperature: 23.75 °C
Temperature: 23.75 °C
Temperature: 23.75 °C
...
Example
This example shows how to connect the DS18B20 sensor to Arduino pin 2 with a 4.7k ohm resistor between the data line and 5V. It reads and prints the temperature every second.
arduino
#include <OneWire.h> #include <DallasTemperature.h> #define ONE_WIRE_BUS 2 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); void setup() { Serial.begin(9600); sensors.begin(); } void loop() { sensors.requestTemperatures(); float tempC = sensors.getTempCByIndex(0); if (tempC == DEVICE_DISCONNECTED_C) { Serial.println("Error: Could not read temperature data"); } else { Serial.print("Current Temperature: "); Serial.print(tempC); Serial.println(" °C"); } delay(1000); }
Output
Current Temperature: 24.00 °C
Current Temperature: 24.00 °C
Current Temperature: 24.00 °C
...
Common Pitfalls
- Missing pull-up resistor: The DS18B20 data line needs a 4.7k ohm resistor between data and 5V to work properly.
- Wrong pin connection: Connect the sensor data pin to the Arduino digital pin defined in code.
- Not calling
sensors.begin(): This initializes the sensor and must be insetup(). - Reading temperature before requesting: Always call
sensors.requestTemperatures()before reading values. - Ignoring error values: Check if the temperature reading equals
DEVICE_DISCONNECTED_Cto detect sensor errors.
arduino
// Wrong way (missing pull-up resistor and no request): #include <OneWire.h> #include <DallasTemperature.h> #define ONE_WIRE_BUS 2 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); void setup() { Serial.begin(9600); // sensors.begin() missing } void loop() { float temp = sensors.getTempCByIndex(0); // No requestTemperatures() called Serial.println(temp); delay(1000); } // Right way: #include <OneWire.h> #include <DallasTemperature.h> #define ONE_WIRE_BUS 2 OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire); void setup() { Serial.begin(9600); sensors.begin(); } void loop() { sensors.requestTemperatures(); float temp = sensors.getTempCByIndex(0); Serial.println(temp); delay(1000); }
Quick Reference
| Step | Description |
|---|---|
| Connect sensor data pin to Arduino digital pin 2 | Use a 4.7k ohm resistor between data and 5V |
| Include | Manage sensor communication and data reading |
| Initialize OneWire and DallasTemperature objects | Set pin and sensor references |
| Call sensors.begin() in setup() | Start sensor communication |
| Call sensors.requestTemperatures() before reading | Trigger temperature measurement |
| Read temperature with sensors.getTempCByIndex(0) | Get temperature in Celsius |
| Check for DEVICE_DISCONNECTED_C | Detect sensor errors |
Key Takeaways
Always use a 4.7k ohm pull-up resistor on the DS18B20 data line to 5V.
Include and use the OneWire and DallasTemperature libraries for easy sensor communication.
Call sensors.requestTemperatures() before reading temperature values.
Check for DEVICE_DISCONNECTED_C to handle sensor read errors gracefully.
Connect the sensor data pin to the Arduino pin defined in your code.