Check the 34 other Arduino tutorials

I think it is important that Bas on Tech can be used by everyone free of charge.

Help me ensure the future of Bas on Tech. Your donation will be used for hosting, videos and maintenance, among other things.

Thank you in advance!

#6 · Reading 1-wire DS18B20 temperature sensor


In this Arduino tutorial for beginners I teach you how to read a 1-wire DS18B20 temperature sensor. You can use this sensor for a weather station, for example. Curious about the advantages of 1-wire? Then read on! 😃

Course material

At the bottom of this page you'll find the course material button. This button allows you to download the code, circuit diagram and other files relevant to this Arduino tutorial.

1-wire bus

The 1-wire bus is a bus system which is developed by the Dallas Semiconductor Corporation. A data bus is a way of connecting different electronic components. 1-wire uses only one wire for this communication. In our case, the temperature sensor uses 1-wire. However, there are other uses such as access keys.

1-wire key (source: Wikipedia)
1-wire key (source: Wikipedia)

DS18B20 1-wire temperature sensor

In this tutorial we will use the DS18B20 1-wire temperature sensor. This exists in different types. On the left of the image you see the bare sensor , on the right the waterproof variant .

DS18B20 1-wire temperatuursensor<
DS18B20 1-wire temperatuursensor

Check whether you actually see DS18B20 on the sensor. Below you see a transistor shown. This one looks very much like the DS18B20, but it is definitely not. A transistor serves a completely different function. If you use a transistor instead of the temperature sensor, it will become very hot and then burn out.

2N3904331 Transistor
2N3904331 Transistor

🎓 How do you recognize that you have the right sensor and no transistor?

The circuit

Connect the sensor according to the diagram below.

DS18B20 1-wire temperature sensor connected to the Arduino
DS18B20 1-wire temperature sensor connected to the ::KnSgtg:Arduino::

When connecting the sensor it is important to use the correct pins. The DS18B20 has a convex and a flat side.

Connect the following jumper wires with the flat side facing towards you:

  • The 5V on the Arduino -> left pin of the temperature sensor
  • The 12 on the Arduino -> middle pin of the temperature sensor (signal)
  • The GND on the Arduino -> right pin of the temperature sensor

Pull-up resistor

In this circuit we use a pull-up resistor. In this case we place the resistor between the middle pin on the sensor and the 5V. The pull-up resistor ensures that the signal pin of the sensor cannot enter a "floating" state. By connecting the resistor to the 5V we know that if 1-wire does nothing with that pin it will always be 5 volts. The 1-wire protocol states that the signal pin becomes 'LOW' when it is read.


Libraries take a lot of work off your hands. You can use the code that others have already developed for us. In this Arduino lesson we use two libraries to read the DS18B20 sensor:

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

If you don't have these yet, you can install them via the Arduino IDE:

Sketch ▸ Use Library ▸ Manage Libraries

Search here for DallasTemperature, this library already contains the OneWire library. You do not have to install these separately.

Arduino Code

The first lines speak for themselves: in the variable temp we store the measured temperature. We use oneWireBus to indicate to which pin the signal pin of the DS18B20 sensor is connected.

1 float temp = 0.0;
2 int oneWireBus = 12;
3 OneWire oneWire(oneWireBus);
4 DallasTemperature sensors(&oneWire);

Using Libraries

The last two lines are more complicated. Here we are going to address the 1-wire and DallasTemperature libraries.

1 OneWire oneWire(oneWireBus);

On this line we define the variable oneWire of type OneWire. The difference is that we're using a function here: the constructor. This is provided by the OneWire library. The only parameter for the constructor is the pin of the 1-wire bus. In our case this is the variable oneWireBus.

1 DallasTemperature sensors(&oneWire);

We do the same for the next line. We create the variable sensors of type DallasTemperature. This constructor expects as a parameter the address of the OneWire library instance. Because we want to pass the address and not the contents of the variable itself, we use &.


1 void setup() {
2     Serial.begin(9600);
3     Serial.println("Bas on Tech - 1-wire temperature sensor");
4     sensors.begin();
5 }

We know the first two lines from the other tutorials, but the last one is new. sensors.begin() initializes the 1-wire data bus so we can start reading the 1-wire sensor.


1 // repeat infinitely
2 void loop() {
4     sensors.requestTemperatures();
5     temp = sensors.getTempCByIndex(0);
7     Serial.print("Temperature is: ");
8     serial.println(temp);
10     delay(1000);
12 }

The loop() function consists of 3 parts:

  • Reading sensor
  • Print to serial monitor
  • Pause

Explanation code

1 sensors.requestTemperatures();
2 temp = sensors.getTempCByIndex(0);

We start with all sensors connected to 1-wire bus via requestTemperatures to request the temperature. After this we request the temperature in degrees Celsius with getTempCByIndex(0). We only use one sensor so index 0 is our sensor.

1 Serial.print("Temperature is: ");
2 serial.println(temp);

We then print this value to the serial monitor.

1 delay(1000);

Finally, we wait 1000ms until we start the next measurement.

Upload code to Arduino

Our program is now ready to be sent to the Arduino. Then open the serial monitor:

Tools ▸ Serial Monitor

Serial monitor with data from DS18B20 sensor
Serial monitor with data from DS18B20 sensor

If you do not see this data, check whether you have connected the jumper wires correctly.

The challenge 🚀

Are you ready for the challenge? Adjust the code so that the built-in LED turns on when a certain temperature is reached.

Good luck!

#6 &middot; Reading 1-wire DS18B20 temperature sensor schakelschema

Bas van Dijk

About Bas on Tech

My name is Bas van Dijk, entrepreneur, software developer and maker. With Bas on Tech I share video tutorials with a wide variety of tech subjects i.e. Arduino and 3D printing.

Years ago, I bought my first Arduino with one goal: show text on an LCD as soon as possible. It took me many Google searches and digging through various resources, but I finally managed to make it work. I was over the moon by something as simple as an LCD with some text.

With Bas on Tech I want to share my knowledge so others can experience this happiness as well. I've chosen to make short, yet powerful YouTube videos with a the same structure and one subject per video. Each video is accompanied by the source code and a shopping list.