arduino ds18b20 temperature sensor

In this tutorial I will be looking at how to setup the Arduino DS18b20 temperature sensor and anything else you need to know about it.

This project is pretty cool if you want to setup a data logger or just something to monitor the temperatures of a certain room. You could combine this tutorial with another to create a pretty cool smart sensor. You could use something like a piezo buzzer to alert you if temperature falls outside a certain range for example.

In this tutorial I will just show you the basics of connecting the DS18B20 to the Arduino correctly. I will also be adding some LEDs to just demonstrate the circuit & code functionality. If you want to stay up to date with all my projects and much more then be sure to follow me on any of the major social networks.

If you want to see how it is all done visually then be sure to check out my video below. It goes through all the steps you need to take to getting this cool little project up and running.


In this tutorial I am going to use some LEDs to represent the current temperature of the temperature sensor. If you just wish to stick with purely software, then you don’t need to add these. You can find the full list of equipment right below!

Arduino Uno

1x DS18B20 temperature sensor

1x 4.7k-ohm Resistor


Breadboard wire


3x 100 ohm resistors


1 x Green LED

1 X Yellow LED

The Arduino DS18B20 Circuit

The circuit for the Arduino DS18B20 sensor is pretty simple and if you don’t want the LEDs just leave that part of the circuit out. I will just quickly describe the temperature sensor below however the rest of equipment is pretty straightforward and doesn’t really need explaining.

If you have read my guide on the DS18B20 with the Raspberry Pi over at Pi My Life Up, then you will know exactly what this device is. For anyone who hasn’t, the DS18B20 is a digital temperature sensor that is capable of reading temperatures within 0.05°C. It also has one wire support which means it’s capable of sharing a single wire with other devices that also support one wire.

In this tutorial I use a waterproof version of the DS18B20 so it simply just looks like a long thick wire with 3 wires sticking out of 1 end. If you get the device without any extras, it simply looks like a normal transistor.

Now let’s move on to putting the circuit together.

1. First connect the 3v3 pin from the Arduino to the positive rail & a ground pin to the ground rail on the breadboard.

2. Now place the DS18B20 sensor onto the breadboard.

3. Place a 4.7k resistor between the positive lead (Red Wire) and the output lead (White Wire) of the sensor.

4. Next, place a wire from the positive lead (Red Wire) to the positive 3v3 rail.

5. Place a wire from the output lead back to pin #5 on the Arduino. Place a wire from the ground lead (black wire) to the ground rail.

6. Now the next few steps are optional and are only required if you want the LEDS.

7. Place a wire from the ground rail to the ground rail on the opposite side of the breadboard.

8. Place the 3 LEDs onto the breadboard. (Red, Yellow and Green)

9. Connect a 100-ohm resistor to each LED and have this go to the ground rail.

10. Now have a wire come from the following Arduino pins: pin 2 to the green LED, pin 3 to a yellow LED and finally red to pin 4.

11. Now that’s the circuit all setup, it’s now time to move onto the code. If you come across any trouble with the circuit please refer to the diagram below or leave a comment at the bottom of this page.

arduino ds18b20 circuit diagram

Installing one wire support

Now by default the Arduino doesn’t have one wire support so we will need to download and install the library for this. If you want some extensive information on what is one wire, then be sure to check Arduinos official website. Now this is a pretty straightforward process that I will take you through now.

1. Firstly, download the latest version of the One Wire library, you can download it here. (Mirror)

2. Once downloaded open up sketch.

3. In here go up to sketch, include library and then add .zip library.

4. It should now say something like “Library added to libraries. Check “include library” menu”.

5. So now go back up to sketch, include library and then under contributed libraries you will find onewire, Click on it.
6. This will add a #include at the top of your file and will added to your Arduino at the next upload.

sketch include library

Installing Dallas Temperature Library

To make things a lot easier I am going to suggest installing a library to handle the data that comes the sensor. This will make it a lot easier than to write the data processing code yourself. If you want to do it yourself without an additional library, then be sure to check out this one wire page for more information. Alternatively, you can look at an analogue sensor such as the TMP36 which is super easy to get going on the Arduino.

If you’re happy using the dallas temperature sensor library then simply do the following.

1. First download the Dallas temperature sensor library, you can download it from here. (Mirror)

2. Now once that has downloaded go into sketch and repeat the steps we did before.

3. Go to sketch, include library and then add .zip library.

4. It should now say something like “Library added to libraries. Check "include library" menu“.

5. So now go back up to sketch, include library and then under contributed libraries you will find DallasTemperature. Click on it.

6. This will automatically add a #include at the top of the script. The library will be synced with your Arduino when you upload it.

Now let’s move onto writing the rest of the code to both get the temperature from the sensor and turn on the correct LED.

Arduino DS18B20 Code

This next part of the tutorial I will go through the code for reading the sensor. It’s pretty straight forward however if you’re new to coding it may be a little overwhelming.

If you want to just download the code, then you can find it available for download over at GitHub.

To begin we need to include both of the onewire and the dallasTemperature headers so we can use these within the code. These should already be there if you followed the steps above.

#include <OneWire.h>
#include <DallasTemperature.h>

Next we declare all the variables that we will need to use throughout the script.  The first of these represent the pin numbers that the devices are connected to.

The float temperature variable is where we will be storing our temperature value.

The lowerlimit & upperlimit variables represent our threshold temperatures. Anything below the lower limit will trigger the yellow LED to turn on. Anything above the upper limit will turn on the red LED. Anything between these will turn on the green LED.

int greenLedPin = 2;
int yellowLedPin = 3;
int redLedPin = 4;

int temp_sensor = 5;

float temperature = 0;
int lowerLimit = 15;
int higherLimit = 35;

Next we create a OneWire object by using our pin we defined earlier (5). If you have a large amount of 1 wire sensors, it’s suggested you split these up across multiple pins. For each just make a new Onewire object. For example, OneWire oneWirePin2(temp_sensor2);

Now we create our Dallas Temperature sensor object by passing our OneWire reference into the class.

OneWire oneWirePin(temp_sensor);
DallasTemperature sensors(&oneWirePin);

In our setup function we activate the Arduino serial interface so we can monitor the output lines in the code. This is also where we need to setup our LEDs to all act as outputs. Lastly we make a call to sensors.begin(), this will set up our sensor so we can start requesting data from it.

void setup(void){
  //Setup the LEDS to act as outputs

In this segment we have a few output lines to just let us know when it has successful requested all the temperatures of the sensors connected to our onewire pin (5).

We next store the value from the sensors.getTempCByIndex(0) into the temperature variable. If you want to get the fahrenheit temperature simply change C to F. For example sensors.getTempFByIndex(0). Also the 0 refers to which sensor we want information from. 0 = the first sensor, if you had a 2nd sensor then it would be 1.

void loop(){
  Serial.print("Requesting Temperatures from sensors: ");
  temperature = sensors.getTempCByIndex(0);

We then turn all the LEDS to low so that only the correct LED remains on when we run the next segment of code. We also print two statements the first being a simple string that says “Temperature is” and then the temperature itself. We finish off this text in the next segment of code described below.

digitalWrite(redLedPin, LOW);
  digitalWrite(greenLedPin, LOW);
  digitalWrite(yellowLedPin, LOW);
  Serial.print("Temperature is ");

This last section compares our temperature values with our predefined values. Depending on the result it will turn the relevant LED on. For example, the first if statement says if the temperature is lower or equal to the lowerlimit value then do this.

Lastly we also delay for 500ms before looping through the process again.

//Setup the LEDS to act as outputs
if(temperature <= lowerLimit){ 
    Serial.println(", Yellow LED is Activated"); 
    digitalWrite(yellowLedPin, HIGH); } 
else if(temperature > lowerLimit && temperature < higherLimit){ 
    Serial.println(", Green LED is Activated"); 
    digitalWrite(greenLedPin, HIGH);
else if(temperature >= higherLimit){
    Serial.println(", Red LED is Activated");
    digitalWrite(redLedPin, HIGH);


If you come across any troubles. you will find that there are typically a few things that can go wrong. I will just list some of the issues that you may come across.

If you’re finding odd results are being returned from the DS18B20 temperature sensor than it is possible it’s not receiving enough power or you’re requesting data too fast from it.  To fix the power issue I would suggest reducing the load on the rail supplying power to the sensor. To reduce the amount of times you request data from the sensor, simply increase the delay time to something a bit higher.

If your sensor is returning really consistent high temperatures all the time, then it’s possible it isn’t connected to the breadboard properly. Try making sure the connections are secure then restarting your Arduino.

These issues shouldn’t happen with the default circuit and code from this tutorial but they may happen if you change things.

I hope this Arduino DS18b20 temperature sensor tutorial has helped with setting up everything correctly. If have missed something, made a mistake or you just want to leave some feedback then please feel free to leave a comment below.


Subscribe & Get 21 Awesome Arduino Projects

Enter your email below to get instant access to 21 amazing Arduino projects.

Please check your inbox or junk mail for a confirmation email!