Skip to Content

The complete guide for DS18B20 digital temperature sensors with Arduino

The complete guide for DS18B20 digital temperature sensors with Arduino

This tutorial includes everything you need to know about using DS18B20 1-Wire digital temperature sensors with Arduino. I have included wiring diagrams and several example codes to get you started.

For this tutorial, we will be using the DallasTemperature in combination with the OneWire Arduino library. These libraries make communicating with one or multiple sensors super easy. In the first part of this article, you can find the specifications and information about the different types of DS18B20 sensors. Next, we will look at how to connect the sensor to the Arduino.

In the first code example, I will show you how to take temperature readings from a single sensor and display the result in the Serial Monitor. The subsequent examples explain how to read multiple sensors with a single Arduino pin. Lastly, I will show you how to display the temperature on an I2C LCD.

If you would like to learn more about other temperature sensors, check out the articles below.

Recommended articles

If you have any questions, please leave a comment below.

Supplies

Hardware components

DS18B20 digital temperature sensor (TO-92)× 3Amazon
Waterproof DS18B20 (alternative)× 1Amazon
DS18B20 breakout board (alternative)× 1Amazon
Arduino Uno× 1Amazon
Breadboard× 1Amazon
Jumper wires~ 15Amazon
4.7 kΩ pull-up resistor× 1Amazon
16×2 character I2C LCD× 1Amazon
USB cable type A/B× 1Amazon

Software

Arduino IDEArduino IDE

Makerguides.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on Amazon.com. As an Amazon Associate we earn from qualifying purchases.

About the DS18B20 1-Wire temperature sensor

The DS18B20 is a digital temperature sensor manufactured by Maxim Integrated (formerly Dallas Semiconductor). It is one of the most popular temperature sensors on the market and provides fairly high accuracy (±0.5 °C) over a large temperature range (-55 °C to + 125 °C). Because the operating voltage of the sensor is 3.0 to 5.5 V, you can use it with both the Arduino (which operates at 5 V), as well as with devices like the ESP32 and Raspberry Pi which have 3.3 V GPIO pins.

One of the main advantages of this sensor is that it requires only one digital pin of the Arduino for communication. The sensor communicates using the Dallas Semiconductor 1-Wire® protocol. This protocol works in a similar way as I2C, but with lower data rates and longer range.

Another advantage is that each DS18B20 sensor has a unique 64-bit serial code, which allows multiple sensors to function on the same 1-Wire bus. So you can read data from multiple sensors that are connected together with just one Arduino pin (see code examples below).

The resolution of the sensor can be set programmatically to 9, 10, 11, or 12 bits. This corresponds to temperature increments of 0.5 °C, 0.25 °C, 0.125 °C, and 0.0635 °C, respectively. The default resolution at power-up is 12-bit.

You can find more specifications in the table below.

DS18B20 digital temperature sensor specifications

Supply voltage3.0 to 5.5 V
Standby current1 μA
Active current1.5 mA
Measuring range-55 °C to + 125 °C (-67 °F to +257 °F)
Accuracy
(Thermometer Error)
±0.5 °C from -10 °C to +85 °C
±1 °C from -30 °C to +100 °C
±2 °C from -55 °C to +125 °C
Resolution 9-bit to 12-bit (programmable)
Conversion time< 750 ms (12-bit resolution)
Communication protocol1-Wire® bus protocol
Package3-pin TO-92
ManufacturerMaxim Integrated
CostCheck price

For more information, you can also check out the datasheet here:

Types of DS18B20 sensors

The sensor usually comes in three form factors. The most common type is the 3-pin TO-92 package, which looks just like a transistor.

DS18B20 as TO-92 package (source)

This type of sensor is sometimes mounted on a breakout board that can include a power LED and the required 4.7 kΩ pull-up resistor. Always make sure to check the markings on the PCB as the order of the pins can be different depending on the manufacturer.

DS18B20 Breakout board (source)

Lastly, you can buy the sensor in a waterproof probe style with a cable already attached. This style can be useful if you want to measure something far away, underwater, or under the ground.

DS18B20 as waterproof probe (source)

Note that the cable of the waterproof sensor is usually jacketed in PVC so it is recommended to keep it under 100 °C.

Wiring – Connecting the DS18B20 to an Arduino

Connecting a DS18B20 to the Arduino is fairly easy as you only need to connect 3 pins. Start by connecting the GND pin to ground and the VDD pin to the 5 V output of the Arduino.

Next, connect the middle pin (DQ) to any of the digital pins of the Arduino. In this case, I used digital pin 2. You also have to add a 4.7 kΩ pull-up resistor between the DQ pin and 5 V. This will keep the idle state for the 1-Wire bus high.

DS18B20-digital-temperature-sensor-with-Arduino-connections-wiring-diagram-schematic-circuit-tutorial
DS18B20 digital temperature sensor with Arduino Uno wiring diagram

The connections are also given in the table below.

DS18B20-digital-temperature-sensor-pinout
Pinout of DS18B20

Note that pin 1 (GND) is the leftmost pin when the flat side of the sensor (with the text printed on it) is facing towards you.

DS18B20 digital temperature sensor connections

DS18B20Arduino
Pin 1 (GND)GND
Pin 2 (DQ)Pin 2 Arduino and via 4.7 kΩ resistor to 5V
PIN 3 (VDD)5V

A waterproof DS18B20 sensor gets connected in the same way. However, the color of the wires can be different depending on the manufacturer. The ground (GND) wire is typically black or blue, the power (VDD) wire is usually red, and the signal wire (DQ) is often yellow or white. I highly recommend to always check the datasheet of your sensor if you are not sure.

Waterproof DS18B20 digital temperature sensor connections

Waterproof DS18B20Arduino
Black wire (GND)GND
Yellow wire (DQ)Pin 2 Arduino and via 4.7 kΩ resistor to 5V
Red wire (VDD)5V

Installing the DallasTemperature and OneWire Arduino libraries

The 1-Wire communication protocol is somewhat complex and requires a bunch of code to parse out the communication. Therefore, we will be using the DallasTemperature Arduino library by Miles Burton to make the programming of these sensors a lot easier.

This library allows you to issue simple commands to read out the temperature data from the sensors. You can find the source code for this library here on GitHub.

To install the library, go to Tools > Manage Libraries (Ctrl + Shift + I on Windows) in the Arduino IDE. The Library Manager will open and update the list of installed libraries.

Installing an Arduino library step 1 open Library Manager
Library Manager

You can search for ‘ds18b20’ and look for the DallasTemperature library by Miles Burton. Select the latest version and then click Install.

DallasTemperature library in Library Manager
DallasTemperature library in Library Manager

The DallasTemperature library relies on the OneWire Arduino library which you need to install as well. This library takes care of the 1-Wire communication protocol.

Search for ‘onewire’ and look for the OneWire library by Jim Studt.

OneWire library in Library Manager
OneWire library in Library Manager

DS18B20 temperature sensor with Arduino example code

With the following example code, you can read the temperature from a DS18B20 sensor and display it in the Serial Monitor.

You can upload the example code to your Arduino using the Arduino IDE.

To copy the code, click on the button in the top right corner of the code field.

/* DS18B20 1-Wire digital temperature sensor with Arduino example code. More info: https://www.makerguides.com */

// Include the required Arduino libraries:
#include "OneWire.h"
#include "DallasTemperature.h"

// Define to which pin of the Arduino the 1-Wire bus is connected:
#define ONE_WIRE_BUS 2

// Create a new instance of the oneWire class to communicate with any OneWire device:
OneWire oneWire(ONE_WIRE_BUS);

// Pass the oneWire reference to DallasTemperature library:
DallasTemperature sensors(&oneWire);

void setup() {
  // Begin serial communication at a baud rate of 9600:
  Serial.begin(9600);
  // Start up the library:
  sensors.begin();
}

void loop() {
  // Send the command for all devices on the bus to perform a temperature conversion:
  sensors.requestTemperatures();

  // Fetch the temperature in degrees Celsius for device index:
  float tempC = sensors.getTempCByIndex(0); // the index 0 refers to the first device
  // Fetch the temperature in degrees Fahrenheit for device index:
  float tempF = sensors.getTempFByIndex(0);

  // Print the temperature in Celsius in the Serial Monitor:
  Serial.print("Temperature: ");
  Serial.print(tempC);
  Serial.print(" \xC2\xB0"); // shows degree symbol
  Serial.print("C  |  ");

  // Print the temperature in Fahrenheit
  Serial.print(tempF);
  Serial.print(" \xC2\xB0"); // shows degree symbol
  Serial.println("F");

  // Wait 1 second:
  delay(1000);
}

You should see the following output in the Serial Monitor (Ctrl + Shift + M).

Serial Monitor output

Make sure that the baud rate of the Serial Monitor is also set to 9600.

How the code works

The first step is to include the required Arduino libraries:

// Include the required Arduino libraries:
#include "OneWire.h"
#include "DallasTemperature.h"

Next, I defined to which pin of the Arduino the DQ pin of the sensor is connected. The statement #define can be used to give a name to a constant value. The compiler will replace all references to this constant with the defined value when the program is compiled. So everywhere you mention ONE_WIRE_BUS, the compiler will replace it with the value 2 when the program is compiled.

// Define to which pin of the Arduino the 1-Wire bus is connected:
#define ONE_WIRE_BUS 2

After this, I created a new object of the OneWire class and passed the DQ pin to its constructor. You also need to create an object of the DallasTemperature class and pass the oneWire object as a parameter.

Note that I called the DallasTemperature object ‘sensors’ but you can use other names as well.

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

In the setup section of the code, we begin serial communication at a baud rate of 9600. Next, I initialized the bus with the function begin().

void setup() {
  // Begin serial communication at a baud rate of 9600:
  Serial.begin(9600);
  // Start up the library:
  sensors.begin();
}

In the loop section of the code, we start with the command for all the sensors on the bus to start a temperature conversion.

  // Send the command for all devices on the bus to perform a temperature conversion:
  sensors.requestTemperatures();

Next, I used the functions getTempCByIndex(deviceIndex) and getTempFByIndex(deviceIndex) to fetch the temperature in degrees Celsius and Fahrenheit respectively. In this case, we have only 1 sensor connected to the bus. Because counting starts at zero, I set the index for our sensor to 0.

  // Fetch the temperature in degrees Celsius for device index:
  float tempC = sensors.getTempCByIndex(0); // the index 0 refers to the first device
  // Fetch the temperature in degrees Fahrenheit for device index:
  float tempF = sensors.getTempFByIndex(0);

Lastly, the temperatures are printed in the Serial Monitor:

  // Print the temperature in Celsius in the Serial Monitor:
  Serial.print("Temperature: ");
  Serial.print(tempC);
  Serial.print(" \xC2\xB0"); // shows degree symbol
  Serial.print("C  |  ");
  // Print the temperature in Fahrenheit
  Serial.print(tempF);
  Serial.print(" \xC2\xB0"); // shows degree symbol
  Serial.println("F");

Because temperature conversion in 12-bit mode can take up to 750 ms, I added a one second delay between each measurement.

Using multiple DS18B20 sensors with Arduino

As I mentioned in the introduction, you can read the temperature from multiple DS18B20 sensors with just one pin of the Arduino. Below you can find two example codes. With the first example, you can read the temperature from the connected sensors by their index. Because all the sensors are connected to the same 1-Wire bus, the first sensor has index 0, the second index 1, and so on.

In the second example, I will show you how to read the unique 64-bit address of each sensor. This address can then be used to read each sensor individually.

Wiring – Connecting multiple DS18B20 sensors to the Arduino

Connecting multiple DS18B20 sensors to the Arduino is just as straightforward as connecting just one. All the sensors are connected in parallel, i.e. all the same pins are connected together. Just like before, the GND pins are connected to ground, the VDD pins to 5 V, and the DQ pins to pin 2 of the Arduino. Don’t forget the 4.7 kΩ pull-up resistor between the DQ pin and 5 V.

Note that I used a breadboard to connect all the pins together. If you are unfamiliar with how a breadboard works, you can find an explanation here.

Multiple DS18B20 1-Wire digital temperature sensors connected to an Arduino

Multiple DS18B20 sensors with Arduino example code

With the example below, you can read the temperature of each sensor by its index and display it in the Serial Monitor.

/* Multiple DS18B20 1-Wire digital temperature sensors with Arduino example code. More info: https://www.makerguides.com */

// Include the required Arduino libraries:
#include "OneWire.h"
#include "DallasTemperature.h"


// Define to which pin of the Arduino the 1-Wire bus is connected:
#define ONE_WIRE_BUS 2

// Create a new instance of the oneWire class to communicate with any OneWire device:
OneWire oneWire(ONE_WIRE_BUS);

// Pass the oneWire reference to DallasTemperature library:
DallasTemperature sensors(&oneWire);

int deviceCount = 0;
float tempC;
float tempF;

void setup() {
  // Begin serial communication at a baud rate of 9600:
  Serial.begin(9600);
  // Start up the library:
  sensors.begin();

  // Locate the devices on the bus:
  Serial.println("Locating devices...");
  Serial.print("Found ");
  deviceCount = sensors.getDeviceCount();
  Serial.print(deviceCount);
  Serial.println(" devices");
}

void loop() {
  // Send the command for all devices on the bus to perform a temperature conversion:
  sensors.requestTemperatures();

  // Display temperature from each sensor
  for (int i = 0;  i < deviceCount;  i++) {
    Serial.print("Sensor ");
    Serial.print(i + 1);
    Serial.print(" : ");
    tempC = sensors.getTempCByIndex(i);
    tempF = sensors.getTempFByIndex(i);
    Serial.print(tempC);
    Serial.print(" \xC2\xB0"); // shows degree symbol
    Serial.print("C  |  ");
    Serial.print(tempF);
    Serial.print(" \xC2\xB0"); // shows degree symbol
    Serial.println("F");
  }

  Serial.println();
  delay(1000);
}

The output in the Serial Monitor should look like this:

Multiple-DS18B20-temperature-sensors-Serial-Monitor-output
Multiple DS18B20 sensors Serial Monitor output

How the code works

The code in this example is mostly the same as before.

In the setup section, I added an extra function that counts the number of devices that are connected to the 1-Wire bus.

  // Locate the devices on the bus:
  Serial.println("Locating devices...");
  Serial.print("Found ");
  deviceCount = sensors.getDeviceCount();
  Serial.print(deviceCount);
  Serial.println(" devices");

In the loop section of the code, I used a for loop to loop through a section of code that fetches the temperature for each sensor connected to the bus by their index.

  // Display temperature from each sensor
  for (int i = 0;  i < deviceCount;  i++) {
    Serial.print("Sensor ");
    Serial.print(i + 1);
    Serial.print(" : ");
    tempC = sensors.getTempCByIndex(i);
    tempF = sensors.getTempFByIndex(i);
    Serial.print(tempC);
    Serial.print(" \xC2\xB0"); // shows degree symbol
    Serial.print("C  |  ");
    Serial.print(tempF);
    Serial.print(" \xC2\xB0"); // shows degree symbol
    Serial.println("F");
  }

Read sensors by address

In order to read the sensors by their individual address, we first need to know what the address of each sensor is. For this, you can use the example sketch below.

The sketch prints the addresses of the connected sensors in the Serial Monitor. In order to know which sensor is which, you can wire just one sensor at a time or successively add a new sensor. Then, you can label each sensor that is connected to the 1-Wire bus.

DS18B20 address finder

/* Multiple DS18B20 1-Wire digital temperature sensors with Arduino example code. More info: https://www.makerguides.com */

// Include the required Arduino libraries:
#include "OneWire.h"
#include "DallasTemperature.h"

// Define to which pin of the Arduino the 1-Wire bus is connected:
#define ONE_WIRE_BUS 2

// Create a new instance of the oneWire class to communicate with any OneWire device:
OneWire oneWire(ONE_WIRE_BUS);

// Pass the oneWire reference to DallasTemperature library:
DallasTemperature sensors(&oneWire);

// Create variables:
int deviceCount = 0; // variable to store the number of devices connected
DeviceAddress deviceAddress; // variable to store the device address

void setup() {
  // Begin serial communication at a baud rate of 9600:
  Serial.begin(9600);
  // Start up the library:
  sensors.begin();

  // Locate the devices on the bus:
  Serial.println("Locating devices...");
  Serial.print("Found ");
  deviceCount = sensors.getDeviceCount();
  Serial.print(deviceCount);
  Serial.println(" devices");

  Serial.println("Printing addresses...");
  for (int i = 0;  i < deviceCount;  i++) {
    Serial.print("Sensor ");
    Serial.print(i + 1);
    Serial.print(" : ");
    sensors.getAddress(deviceAddress, i);
    printAddress(deviceAddress);
  }
}

void loop() {
}

void printAddress(DeviceAddress deviceAddress) {
  for (uint8_t i = 0; i < 8; i++) {
    Serial.print("0x");
    if (deviceAddress[i] < 0x10) {
      Serial.print("0");
    }
    Serial.print(deviceAddress[i], HEX);
    if (i < 7) {
      Serial.print(", ");
    }
  }
  Serial.println();
}

The output in the Serial Monitor should look something like this:

Serial Monitor Output
Serial Monitor Output

Now write down the addresses of all the sensors, because you will need them in the next example.

Read sensors by address Arduino example code

With the following example, you can read the temperature from each sensor by specifying their unique address.

/* Read multiple DS18B20 1-Wire digital temperature sensors by address. 
   More info: https://www.makerguides.com */

// Include the required Arduino libraries:
#include "OneWire.h"
#include "DallasTemperature.h"

// Define to which pin of the Arduino the 1-Wire bus is connected:
#define ONE_WIRE_BUS 2

// Create a new instance of the oneWire class to communicate with any OneWire device:
OneWire oneWire(ONE_WIRE_BUS);

// Pass the oneWire reference to DallasTemperature library:
DallasTemperature sensors(&oneWire);

// Addresses of DS18B20 sensors connected to the 1-Wire bus
byte sensor1[8] = {0x28, 0x18, 0xB4, 0x49, 0x0C, 0x00, 0x00, 0x7C};
byte sensor2[8] = {0x28, 0xCC, 0x19, 0x49, 0x0C, 0x00, 0x00, 0xBB};
byte sensor3[8] = {0x28, 0x19, 0xEF, 0x48, 0x0C, 0x00, 0x00, 0x21};

void setup() {
  // Begin serial communication at a baud rate of 9600:
  Serial.begin(9600);
  // Start up the library:
  sensors.begin();
}

void loop() {
  // Send the command for all devices on the bus to perform a temperature conversion:
  sensors.requestTemperatures();

  Serial.print("Sensor 1: ");
  printTemperature(sensor1); // call the printTemperature function with the address of sensor1 as input
  Serial.print("Sensor 2: ");
  printTemperature(sensor2);
  Serial.print("Sensor 3: ");
  printTemperature(sensor3);

  Serial.println(); // prints an empty line
  delay(1000);
}

void printTemperature(DeviceAddress address) {
  // Fetch the temperature in degrees Celsius for device address:
  float tempC = sensors.getTempC(address);
  // Fetch the temperature in degrees Fahrenheit for device address:
  float tempF = sensors.getTempF(address);
  Serial.print(tempC);
  Serial.print(" \xC2\xB0"); // shows degree symbol
  Serial.print("C  |  ");

  // Print the temperature in Fahrenheit
  Serial.print(tempF);
  Serial.print(" \xC2\xB0"); // shows degree symbol
  Serial.println("F");
}

Note that you have to replace the addresses in lines 17 to 19 with the addresses that you found using the previous example code.

// Addresses of DS18B20 sensors connected to the 1-Wire bus
byte sensor1[8] = {0x28, 0x18, 0xB4, 0x49, 0x0C, 0x00, 0x00, 0x7C};
byte sensor2[8] = {0x28, 0xCC, 0x19, 0x49, 0x0C, 0x00, 0x00, 0xBB};
byte sensor3[8] = {0x28, 0x19, 0xEF, 0x48, 0x0C, 0x00, 0x00, 0x21};

You should see the following output in the Serial Monitor.

Serial Monitor Output
Serial Monitor Output

Code explanation

The address of each sensor consists of 64-bits. In the code, we specify the address as an array of eight 8-bit bytes.

// Addresses of DS18B20 sensors connected to the 1-Wire bus
byte sensor1[8] = {0x28, 0x18, 0xB4, 0x49, 0x0C, 0x00, 0x00, 0x7C};
byte sensor2[8] = {0x28, 0xCC, 0x19, 0x49, 0x0C, 0x00, 0x00, 0xBB};
byte sensor3[8] = {0x28, 0x19, 0xEF, 0x48, 0x0C, 0x00, 0x00, 0x21};

The setup section of the code is the same as the previous examples.

In the loop, we call the printTemperature(DeviceAddress address) function. In this custom function, we use getTempC(address) and getTempF(address) to fetch the temperature from a sensor whose address is passed as a parameter.

void printTemperature(DeviceAddress address) {
  // Fetch the temperature in degrees Celsius for device address:
  float tempC = sensors.getTempC(address);
  // Fetch the temperature in degrees Fahrenheit for device address:
  float tempF = sensors.getTempF(tempC);
  Serial.print(tempC);
  Serial.print(" \xC2\xB0"); // shows degree symbol
  Serial.print("C  |  ");

  // Print the temperature in Fahrenheit
  Serial.print(tempF);
  Serial.print(" \xC2\xB0"); // shows degree symbol
  Serial.println("F");
}

Other functions of the DallasTemperature Arduino library

The DallasTemperature library has some other useful functions built-in that I have not yet covered in the examples above. I have therefore listed a few of them below:

setResolution()

This function can be used to set the resolution of the temperature-to-digital conversion. As I mentioned in the introduction, this can be set to 9, 10, 11, or 12 bits, corresponding to increments of 0.5 °C, 0.25 °C, 0.125 °C, and 0.0625 °C, respectively.

You might wonder why you would want to change the resolution, isn’t higher always better? One advantage of selecting a lower resolution is that the temperature-to-digital conversion takes a lot less time. This means that you can take more temperature readings in the same amount of time.

From the datasheet I got the following information:

ResolutionTemperature incrementMax conversion time
9-bit0.5 °C93.75 ms
10-bit0.25 °C187.5 ms
11-bit0.125 °C375 ms
12-bit0.0625 °C750 ms

The DallasTemperature library allows you to set the resolution with the function setResolution(). This function can be added to the setup or loop section of your code.

You can set the resolution for all the connected sensors as follows:

// Set the resolution for all devices to 9, 10, 11, or 12 bits:
sensors.setResolution(9);

Or you can set it individually for a specific sensor by specifying its address:

// Addresses of DS18B20 sensors connected to the 1-Wire bus
byte sensor1[8] = {0x28, 0x18, 0xB4, 0x49, 0x0C, 0x00, 0x00, 0x7C};

// Set the resolution of a specific device to 9, 10, 11, or 12 bits:
sensors.setResolution(sensor1, 9);

toFahrenheit()

This function can be used to convert the temperature in degrees Celsius to Fahrenheit.

float tempC = sensors.getTempCbyIndex(0);
float tempF = DallasTemperature::toFahrenheit(tempC);

setHighAlarmTemp() and setLowAlarmTemp()

This function sets the internal high and low temperature alarms for a device in degrees Celsius.

bool hasAlarm()

This function returns true when a device has an alarm condition. You can find an example of these functions here.

Display the DS18B20 temperature readings on an I2C LCD

If you want to make a standalone project that doesn’t need a computer, it can be nice to know how to display the temperature readings on an LCD display.

With the example code below, you can display the temperature readings on a 16×2 character I2C LCD.

Connecting the I2C LCD is very easy as you can see in the wiring diagram below. You can check out my detailed tutorial below for more information. How to control a character I2C LCD with Arduino. If you want to use a standard non-I2C LCD instead, take a look at this article: How to use a 16×2 character LCD with Arduino

DS18B20 with 16×2 character I2C LCD and Arduino

The connections are also given in the table below:

I2C LCD Connections

I2C Character LCDArduino
GNDGND
VCC5 V
SDAA4
SCLA5

To use an I2C LCD, you will need to install the LiquidCrystal_I2C Arduino library.

Open the library manager in the Arduino IDE and search for ‘liquidcrystal_i2c’. Now scroll down and look for the library by Frank de Brabander. Select the latest version and then click Install.

Installing the LiquidCrystal_I2C Arduino library

DS18B20 with I2C LCD example code

/* DS18B20 1-Wire digital temperature sensor with 16x2 I2C LCD and Arduino example code. More info: https://www.makerguides.com */

// Include the required Arduino libraries:
#include "OneWire.h"
#include "DallasTemperature.h"
#include "LiquidCrystal_I2C.h"

// Define to which pin of the Arduino the 1-Wire bus is connected:
#define ONE_WIRE_BUS 2

// Create a new instance of the oneWire class to communicate with any OneWire device:
OneWire oneWire(ONE_WIRE_BUS);

// Pass the oneWire reference to DallasTemperature library:
DallasTemperature sensors(&oneWire);

LiquidCrystal_I2C lcd(0x27, 16, 2);

// Degree symbol:
byte Degree[] = {
  B00111,
  B00101,
  B00111,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000
};

void setup() {
  // Start up the library:
  sensors.begin();
  // Start the LCD and turn on the backlight:
  lcd.init();
  lcd.backlight();
  // Create a custom character:
  lcd.createChar(0, Degree);
}

void loop() {
  // Send the command for all devices on the bus to perform a temperature conversion:
  sensors.requestTemperatures();

  // Fetch the temperature in degrees Celsius for device index:
  float tempC = sensors.getTempCByIndex(0); // the index 0 refers to the first device

  // Print the temperature on the LCD;
  lcd.setCursor(0,0);
  lcd.print("Temperature:");
  lcd.setCursor(0,1);
  lcd.print(tempC);
  lcd.write(0); // print the custom character
  lcd.print("C");

  // Wait 1 second:
  delay(1000);
}

You should see the following output on the LCD:

DS18B20-16x2-I2C-LCD-output
LCD Display Output

Conclusion

In this tutorial, I have shown you how to use DS18B20 1-Wire digital temperature sensors with Arduino. I hope you found it useful and informative. If you did, please share this article with a friend who also likes electronics and making things.

I would love to know what projects you plan on building (or have already built) with these sensors. If you have any questions, suggestions, or if you think that things are missing in this tutorial, please leave a comment below.

Creative Commons License

Cliff

Monday 1st of May 2023

I have been struggling with DS18B20 sensors when employing them at different distances from my UNO for testing. After struggling for some time with random errors I tried your code for multiple sensors on one bus and come up with many good reads but still interlaced with bad ones. The distances I'm speaking of are three sensors on individual lines 30-50 ft placed in parallel and then two sensors on the test breadboard near the UNO. If I only record the values for three far sensors I get good reading, but when I add in the two close sensors the problem returns. I have tried pull-up resistor values from 2K to 4.7k.

Also should not: sensors.requestTemperatures(); delay(750); be added before the second read of the same sensor?

All Good Things, Cliff

Andrew

Friday 24th of February 2023

This is wonderful but, how do how would I get the multiple sensors to display on a 12C LCD? If you can help me with this that would be great. Thanks

Nat

Friday 26th of March 2021

Hello,

Thanks for the upload, I am using the DS18B20 on my project with an MKR1010 but have one question: How do you recommend powering this system?

I am stumbling across a weird problem where my measurements are working as long as the system is powered by my computer, but: Any other, adapter or battery system won't make it work. By that I mean, the arduino fires up no problem, but can't get any data (temp) sent to my platform.

I tried out simple blink examples to make sure my adapter wasn't out of range etc. Then tried reducing the program to the strict minimum, having a single sensor light up the onboard LED: that would again only work when connected to the computer on the serial.

Thanks for reading, if you have any tips, they would be super welcome! Best regards, N.

James

Friday 15th of January 2021

I've followed these instructions with an Arduino Nano, but I'm only getting the temperature reported as -127C (which is also what is reported if there is nothing connected at all)...

I've also tried other tutorials where they said to wire the GND and Vdd both to GND then resistor between 5V of the board to signal (along with signal) to D2. I used their code and got the same result too.

Is it something wrong with my resistor? Must the sensor be dead?

Surely I'm just missing something simple?

Gareth

Tuesday 8th of March 2022

I struggled with this same issue for a while and nearly threw away my sensor assuming it was faulty, but the answer turned out to be simple; my Arduino board is an Uno R3 and the analog input pins are numbered A0 to A5 rather than 0 to 5 as in the code examples. Check your board and if it's like mine, just "#define ONE_WIRE_BUS A2" instead of "#define ONE_WIRE_BUS 2" and you should be good.

František Pospíšil

Sunday 14th of March 2021

I had the same problem, I thought of connecting the temperature sensors to the D13 terminal, where the LED on the Arduino board is connected, to see if there is any communication, and lo and behold, it has already started working as it should. I have a PullUp resistor of 10kOhm. Then I will sometimes try to take an oscilloscope and see what the problem is. Of course, you need to set OneWire to pin 13, so change this line: #define ONE_WIRE_BUS 13

František Pospíšil

Sunday 14th of March 2021

měl jsem ten samý problém, napadlo mě připojit teplotní senzory na vývod D13, kde je na desce Arduina připojena LEDka, abych se podíval, jestli tam probíhá nějaká komunikace, a ejhle, už to začalo pracovat tak jak má. Odpor PullUp mám 10kOhm. Zkusím pak někdy vzít osciloskop a podívám se, v čem je problém. Samozřejmě je potřeba nastavit OnaWire na pin 13, tedy změnit tento řádek: #define ONE_WIRE_BUS 13

Bill Frick

Monday 9th of November 2020

Great tutorial ! New to Arduino and the code explanations are very helpful. My project uses 15 DS18B20 sensors to control circulation fans in 6 Solar furnaces. The plan is to use the DELTA temperature of the air in each to PWM the power to the fans. 3 sernsors are to monitor the outside, inside, and collector surface temperatures. Overkill but earlier version only measured output temperature.

Benne de Bakker

Monday 9th of November 2020

Awesome, sounds like an interesting project!