DS18B20: a temperature sensor for liquids


There are various humidity and temperature sensors available, such as the LM35. Some built into modules for specific use with the Arduino. But generally, they are to measure dry temperature, that is, air temperature. But there is a concrete model that works for measure temperature in liquids and is called DS18B20. A peculiarity that may be useful for some of your more exotic DIY projects where you also play with some type of liquid for which you need to know this parameter.

Actually the DS18B20 does not only measure temperature inside liquids, but it can be very useful to measure temperature in humid environments and also under some liquid. So you can also use it to measure the air temperature if the environment is quite loaded with humidity. And as I said, the feature of being able to immerse it in a liquid to measure its temperature is one of the features that make it incredibly practical.

What is the DS18B20?

Well, I think it has already become quite clear, it is an electronic sensor capable of measuring the temperature of gaseous or liquid media. In addition, there are different encapsulation or packaging of the DS18B20, such as the basic one you see in the main image, or it can also be integrated into some PCBs, submersible probes, etc. For your project you should choose the most suitable format according to what you want.

For example, apart from the typical TO-92, there is also the microSOP. Possibly to integrate with Arduino the most appropriate is the TO-92, since with its three pins it is very easy to insert into the breadboard for connection.


Ds18b20 pins

El DS18B20 pinout it is easy to identify. For example, taking the Dallas TO-92 package, which is one of the most popular, as a reference, you can see that it has three pins. If you put it from the front, that is, with the rounded section back and looking at the flat face where the inscriptions appear, the pin on your left is 1 and the one on your right is 3. Therefore, 1 would be for GND or ground, 2 is for data and 3 for supply voltage.

Here we have to say that, values ​​that you should know:

  • Pin 1: you must connect it to the GND pin of Arduino, that is, to 0v.
  • Pin 2: this pin is DQ or data, the one that will send the temperatures measured by the sensor to Arduino through a particular protocol known as 1-Wire and that will need a special library and functions for the Arduino IDE. That will allow using only one Arduino pin to connect multiple sensors with this protocol ...
  • Pin 3: It can be powered from 3 to 5,5v, so you can connect it to the 5v output of Arduino.

DS18B20 technical characteristics and datasheet

As always, it is interesting to know the technical characteristics of the sensor to know how it works, so as not to damage it, and above all so that we know where its measurement limits are, since if the values ​​we want to measure are not between them, it will not help us and you should look for another alternative.

To do this, it is best to download a manufacturer datasheet, like the one in Dallas that you can see here. There you will find all the necessary information. And remember that although all the DS18B20 may be similar, depending on the manufacturer or package you may find some changes ...

But regardless of the quirks, here are a few basic technical data:

  • Temperature range: -55 to 125ºC, therefore, it can measure in gas or liquid at very low and also high temperatures.
  • Errors: The DS18B20 is sensitive to external noise or disturbances that can give erroneous values ​​in measurements. The margin of error is plus minus 2ºC, although at temperatures between -10ºC and 85ºC, that is, when we are not close to the limits, it could be only half a degree.
  • Decision: You can work with several resolutions or minimal variations that you can detect with the Arduino analog pins. Supports 9-bit, 10-bit, 11-bit, and 12-bit (default). That is, it can measure from a half to a half degree, from a quarter to a quarter degree, from 0,125 to 0,125ºC, or from 0,0625ºC respectively. You can alter this programming through the programming code.
  • Supply voltage: 3 to 5,5v
  • Price: 1 to 3 €

Integration with Arduino

Arduino connection diagram - ds18b20

Although there are various ways to connect it, the most suitable is the one you see in this diagram. It is quite simple, with the GND pin in the corresponding connection of the Arduino board, the power supply the same and then the data to the Arduino analog that you have chosen in your programming code in Arduino IDE. But it is also good to set a 4,7k pull-up resistor (if the distance of the sensor probe cable is greater, the resistance should be lower, for example, for 5m of 3,3k, for 10 of 2,2, XNUMXk,…) for the data pin and thus keep it always high.

For programming in Arduino IDE and its good integration with the DS18B20 and that very particular protocol, it is recommended that you download the libraries Dallas Temperature y OneWire from the environment. And the base code, it could be something like this example that I show:

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

// Pin donde se conecta el bus 1-Wire (DQ)
const int pinDatosDQ = 9;

// Instancia a las clases OneWire y DallasTemperature
OneWire oneWireObjeto(pinDatosDQ);
DallasTemperature sensorDS18B20(&oneWireObjeto);
void setup() {
    // Iniciamos la comunicación serie a 9600 baudios
    // Iniciamos el bus 1-Wire del sensor
void loop() {
    // Indicamos que tome la temperatura
    Serial.println("Midiendo temperatura");
    // Lee y muestra la temperatura (recuerda que puedes conectar más de uno con 1-wire)
    Serial.print("La temperatura del sensor 0 es de: ");
    Serial.println(" C");
    Serial.print("La temperatura del sensor x es de: ");
    Serial.println(" ºC");

More info - Arduino Programming Manual (Free PDF)

The content of the article adheres to our principles of editorial ethics. To report an error click here!.

Be the first to comment

Leave a Comment

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



  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.