Arduino and DS18B20 temperature sensor example


The DS18B20 is a  temperature sensor that can be used in various simple projects.  This part uses the 1 wire (I2C) bus and you can connect multiple sensors up to your Arduino.

The part is also relatively low cost and only requires an additional 4k7 pull up resistor. In the example below we shall make a basic example that reads the temperature and outputs via serial and can be verified using the serial monitor in the Arduino IDE.


Parts List

Label Part Type
DS1 DS18B20 1-Wire Temperature Sensor
Part1 Arduino Uno (Rev3)
R1 4.7k Ω Resistor


As always be careful not to get the connections incorrect, you can refer to the pinout for the device below to help . The DS18B20 can be powered by between 3.0V and 5.5V so you can simply connect its GND pin to 0V and the VDD pin to +5V from the Arduino.

ds18b20 pinout
ds18b20 pinout

I personally find that using a module is the easiest way, the components are soldered for you and the connections come out to a header, here is a module that I have used Product – DS18B20 temperature module temperature sensor module


Breadboard layout

Here is the connection diagram showing how to connect your arduino to the resistor and sensor.

ds1820 breadboard layout
ds1820 breadboard layout


There are a couple of  libraries and copy them into your Arduino libraries folder.

Dallas 18b20 Temperature Library
“1-Wire” Library

Now copy and paste the following code into a new sketch

#include <OneWire.h>
#include <DallasTemperature.h>
//the pin you connect the ds18b20 to
#define DS18B20 2
OneWire ourWire(DS18B20);
DallasTemperature sensors(&ourWire);
void setup()
//start reading
void loop()
//read temperature and output via serial
Serial.println(" degrees C");



When you upload the sketch, go to Tools -> Serial Monitor and you should see something like the following.


ds18b20 serial output
ds18b20 serial output

DS18B20 Stainless Steel Waterproof Temperature Probe Sensor For Arduino

DS18B20 temperature module temperature sensor module