El DS18B20 sensor It has gained popularity due to its reliability and versatility for temperature measurement. It is widely used in electronic projects with microcontrollers such as Arduino, PIC or ESP8266 due to its simplicity of use and its ability to connect multiple sensors on the same bus, making it an ideal choice for both amateurs and professionals.
One of the main attractions of this sensor is that it uses only one cable for data communication using the protocol 1 wire, making it easy to integrate into a wide variety of projects. In addition, the DS18B20 can operate in up to two different power modes, making it even more flexible. Throughout this article, we will explain in depth how it works, how you can connect multiple sensors, and how to optimize your temperature measurements.
Features of DS18B20
The DS18B20 is manufactured by Maxim Integrated, among others, and is presented in various encapsulations, the format being TO-92 (similar to that of many transistors) one of the most common. In addition, it can also be found in sealed and waterproof versions, making it ideal for measuring temperatures in difficult or humid environments.
Notable features of the DS18B20 include:
- The temperature range that can be measured covers from -55°C to 125°C, making it suitable for industrial and domestic applications.
- Su programmable resolution can vary between 9 and 12 bits, allowing for precise adjustment based on the needs of each project.
- Each sensor has a unique address 64 bits, facilitating the identification of multiple sensors connected to the same bus.
DS18B20 Power Modes
The sensor can operate in two power modes, providing flexibility when integrating it into different projects, each with its advantages.
Power supply via data pin (Parasite Power)
This mode is ideal when space is limited or when long distance connections are needed. The DS18B20 draws power directly from the data pin when it is high and stores the power in a small capacitor for when the data line is low. This type of power supply is called Parasite Power.
However, it is important to note that for it to work properly, it is necessary to connect the pins GND y VDD to ground. In addition, it is advisable to include a transistor MOSFET to help in situations where temperature conversions require more current.
Power supply using an external source
The most common and recommended way to power the DS18B20 is through an external source connected to the pin VDDThis method ensures a stable voltage independent of data traffic on the 1-Wire bus, which is advantageous for projects requiring constant precision.
How to use the DS18B20 with Arduino
To work with this sensor on the Arduino platform, it is necessary to use two fundamental libraries: OneWire y Dallas TemperatureThese libraries facilitate communication and allow for easy reading and configuration.
OneWire Library: Allows communication using the 1-Wire protocol. It can be downloaded from the GitHub repository.
DallasTemperature Bookstore: Contains the functions needed to read the temperature and configure the sensor. Download it from this link.
Once you have installed both libraries, you can start working with the sensor without any complications. Below we explain some examples of how to read the temperature and how to work with multiple sensors.
Example 1: Temperature reading with a single sensor
To take a temperature reading with a single DS18B20 connected to an Arduino, the basic circuit simply involves connecting the sensor's data pin to the digital pin 2 from the Arduino, along with a resistor Pull-Up of 4.7kΩ.
This is the basic code to read the temperature from the sensor:
#include <OneWire.h> #include <DallasTemperature.h> OneWire ds(2); DallasTemperature sensors(&ds); void setup() { Serial.begin(9600); sensors.begin(); } void loop() { sensors.requestTemperatures(); float tempC = sensors.getTempCByIndex(0); Serial.print("Temperatura= "); Serial.print(tempC); Serial.println(" °C"); delay(1000); }
The code is pretty simple. It only requires a couple of lines in the main loop to request the temperature and read it, making it very easy to implement and customize for different uses.
Example 2: Using multiple sensors on different pins
When working with more than one DS18B20, there are two ways to connect the sensors. The first is to assign a different Arduino digital pin to each sensor. In this case, a 4.7kΩ pull-up resistor will be needed for each sensor.
Here we show you an example of how to work with two sensors connected to different pins:
#include <OneWire.h> #include <DallasTemperature.h> OneWire ds1(2); OneWire ds2(3); DallasTemperature sensors1(&ds1); DallasTemperature sensors2(&ds2); void setup() { Serial.begin(9600); sensors1.begin(); sensors2.begin(); } void loop() { sensors1.requestTemperatures(); float temp1 = sensors1.getTempCByIndex(0); sensors2.requestTemperatures(); float temp2 = sensors2.getTempCByIndex(0); Serial.print("Temperatura 1 = "); Serial.print(temp1); Serial.print(" °C Temperatura 2 = "); Serial.println(temp2); delay(1000); }
Example 3: Multiple sensors connected to a single pin
The other possibility to connect several DS18B20 in a project is to do it using a single pin and the same 1-Wire bus for all of them. In this case, each sensor must have its own unique identification number, which is assigned at the factory. Here we explain how to obtain these addresses using the following code:
#include <OneWire.h> OneWire ds(2); void setup(void) { Serial.begin(9600); } void loop(void) { byte address[8]; if (!ds.search(address)) { Serial.println("No more addresses."); ds.reset_search(); delay(250); return; } Serial.print("Address: "); for (int i = 0; i < 8; i++) { Serial.print(address[i], HEX); if (i < 7) Serial.print(", "); } Serial.println(); delay(250); }
Once you have the addresses of all the sensors, you can read the specific sensor using its unique address. The following code shows you how to do this:
#include <DallasTemperature.h> OneWire ds(2); DallasTemperature sensors(&ds); DeviceAddress sensor1 = {0x28, 0xFF, 0xCA, 0x4A, 0x5, 0x16, 0x3, 0xBD}; DeviceAddress sensor2 = {0x28, 0xFF, 0x89, 0x3A, 0x1, 0x16, 0x4, 0xAF}; void setup() { Serial.begin(9600); sensors.begin(); } void loop() { sensors.requestTemperatures(); float temp1 = sensors.getTempC(sensor1); float temp2 = sensors.getTempC(sensor2); Serial.print("Temp sensor 1: "); Serial.println(temp1); Serial.print("Temp sensor 2: "); Serial.println(temp2); delay(1000); }
The advantages of this method are that you save pins on the Arduino and you can connect many sensors in parallel to the same data bus.
With these examples, you can see the versatility and power of the DS18B20 sensor. It is very easy to implement, and if you need to connect multiple sensors, you have full flexibility to do so in different ways. The DS18B20 is one of the most reliable sensors for measuring temperature, and thanks to its low power consumption and easy programming, it is perfect for all kinds of projects.
The DS18B20 sensor is not only easy to use with Arduino, but can also be used in a wide range of industrial and residential applications such as temperature monitoring in air conditioning systems, machinery or even greenhouses.