DS18B20 Temperature Sensor – How to use with Arduino

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.

DS18B20 Sensor Pins

DS18B20 sensor pins.

Connection to Arduino UNO

DS18B20 connections to Arduino.

Click to enlarge

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.

Click to enlarge

Click to enlarge

DS18B20 Arduino Code

Code for Fahrenheit

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.

Run Arduino Code

Run the code above, whichever one you choose (Celsius or Fahrenheit), and then open the Serial Monitor.


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.

Testing DS18B20 sensor.

Components used in this example

*As an Amazon & Ebay Associate I earn from qualifying purchases.

Arduino UNOhttps://amzn.to/3uYVAMC
DS18B20 Module (KY-001)https://amzn.to/3ahco9G
Hand-held infrared thermometerhttps://amzn.to/3gf0GAo
Breadboard (Elenco 9440)https://amzn.to/3x23dnq

One Reply to “DS18B20 Temperature Sensor – How to use with Arduino”

Leave a Reply

Your email address will not be published. Required fields are marked *