Learn how to use DS18B20 digital temperature sensor with Arduino. In this post we’ll go over how to connect the DS18B20 to an Arduino UNO, the sensor specs, how to program for use with Arduino, the required libraries, and then we’ll do some testing.
The DS18B20 is a temperature sensor that can be used for electronics projects with Arduino or other microcontrollers.
Supply Voltage: 3.0V – 5.5V
Sensor range: -55oC (-67oF) to 125oC (257oF)
Accuracy (error) in Celsius
Range (oC) | Error (oC) |
-10 to 85 | ±0.5 |
-30 to 100 | ±1.0 |
-55 to 125 | ±2.0 |
Accuracy (error) in Fahrenheit
Range (oF) | Error (oF) |
14 to 185 | ±0.9 |
-22 to 212 | ±1.8 |
-67 to 257 | ±3.6 |

For more details on the sensor specs, see the datasheet.
Downloading the Libraries for DS18B20
Dallas Temperature Control Library (by Miles Burton) – LINK
OneWire Library (by www.pjrc.com) – LINK
Both libraries are in GitHub as downloadable Zip files.

Installing the Libraries for DS18B20
Once the Zip libraries have been downloaded, you need to import them within the Arduino IDE. To do this, go to the top menu, then click Sketch, Include Library, Add .Zip Library… Then, find the Zip files in the folder in which you downloaded them to, then for each (one at a time) select the Zip file and click open to import into Arduino IDE.
DS18B20 Arduino Code
Code for Fahrenheit
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
//Arduino Code - DS18B20 #include <OneWire.h> #include <DallasTemperature.h> #define ONE_WIRE_BUS 2 // Data wire is plugged into port 2 on the Arduino // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) OneWire oneWire(ONE_WIRE_BUS); // Pass our oneWire reference to Dallas Temperature. DallasTemperature sensors(&oneWire); void setup(void) { // start serial port Serial.begin(9600); // Start up the library sensors.begin(); } void loop(void) { // call sensors.requestTemperatures() to issue a global temperature // request to all devices on the bus sensors.requestTemperatures(); // Send the command to get temperatures Serial.print("Temperature: "); Serial.print(sensors.getTempFByIndex(0)); Serial.println(" degF"); } |
Code for Celsius
If you want to get the temperature in Celsius copy the code and replace the last few lines of the code above.
1 2 3 4 5 |
//Arduino Code - DS18B20 (Celsius) Serial.print(sensors.getTempCByIndex(0)); Serial.println(" degC"); } |
Run Arduino Code
Run the code above, whichever one you choose (Celsius or Fahrenheit), and then open the Serial Monitor.


Testing
After I started running the code, I went ahead and used a hand-held infrared thermometer to compare the readings. The results were not exactly equal, but they were close enough considering the potential error in both the DS18B20 and the hand held infrared thermometer.

Components used in this example
*As an Amazon & Ebay Associate I earn from qualifying purchases.
Component | Link |
Arduino UNO | https://amzn.to/3uYVAMC https://ebay.us/veZdKX |
DS18B20 Module (KY-001) | https://amzn.to/3ahco9G https://ebay.us/3x6bzl |
Hand-held infrared thermometer | https://amzn.to/3gf0GAo https://ebay.us/kcLkfO |
Breadboard (Elenco 9440) | https://amzn.to/3x23dnq https://ebay.us/FcwSdb |
How do I change the bit resolution of the thermistor, what is the default?