Você está na página 1de 11

Tutorial digital temperature sensor DS18B20

Posted by Tutorials 20 Comments

Sensor DS18B20
The DS18B20 is a digital temperature sensor that uses the 1-Wire protocol to communicate,
this protocol needs only one data pin to communicate and allows to connect more than one
sensor on the same bus.
The DS18B20 sensor is manufactured by Maxim Integrated, the factory package is type TO-
92 similar to that used in small transistors. The most used commercial presentation for
convenience and robustness is the sensor inside a stainless steel tube resistant to water,
with which we work this tutorial.
With this sensor we can measure temperature from -55 ° C to 125 ° C and with a
programmable resolution from 9 bits up to 12 bits.
Each sensor has a unique 64-bit address set at the factory, this address serves to identify the
device with which it is communicating, since on a 1-wire bus there may be more than one
device.
The sensor has two feeding methods:

Power through the data pin:


In this way, the sensor internally gets energy from the data pin when it is in a high state and
stores charge in a capacitor for when the data line is in a low state, this way of obtaining
energy is called "Parasite" Power "and it is used when the sensor must be connected at
great distances or where space is limited, since in this way the VDD line is not needed. The
diagram for your connection should be as follows:
Note that the pin GND and VDD are both connected to GND, this is essential for the Parasite
Power to be activated. The MOSFET in the image is necessary for temperature conversions
or copying data from the EEPROM circuit memory, in these operations the operating current
increases and if only power is supplied through the resistor they can cause voltage drops in
the internal capacitor.

Power using an external source:


In this way the sensor is fed through the VDD pin, in this way the voltage is stable and
independent of the 1-wire bus traffic.
The connection diagram is as follows:

This form of feeding is the most recommended and is the one used in this tutorial.

Libraries for the DS18B20 in Arduino


In order to work the DS18B20 on Arduino we need two libraries:

- OneWire library , download: https://github.com/PaulStoffregen/OneWire


In this library, the entire 1-wire bus protocol is implemented. And it can be used for both the
DS18B20 and other 1-wire devices, for more information about the
library: http://www.pjrc.com/teensy/td_libs_OneWire.html
-Librery DallasTemperature , download: https://github.com/milesburton/Arduino-
Temperature-Control-Library
In this library the necessary functions are implemented to be able to perform readings or
configurations of the DS18B20, for more information about the library, check:
http://www.milesburton.com/Dallas_Temperature_Control_Library

Connections of the DS18B20 with Arduino:


Note that it is necessary to put a pull-up resistance of 4.7K, and can use a breadboard to
facilitate the connection.

Ex.1: Performing temperature readings with the


DS18B20
After installing the libraries and making the previous connection we can perform the
temperature readings, for that we use the following sketch:

#include < OneWire .h>


#include <DallasTemperature.h>

OneWire ourWire (2); // Pin 2 is set as OneWire bus

DallasTemperature sensors (& ourWire); // Declare a variable or object for our sensor

void setup () {
delay (1000);
Serial . begin (9600);
sensors begin (); // The sensor starts
}

void loop () {
sensors.requestTemperatures (); // The command is sent to read the temperature
float temp = sensors.getTempCByIndex (0); // The temperature is obtained in ° C

Serial . print ( "Temperature =" );


Serial . print (temp);
Serial . println ( "C" );
delay (100);
}

As it is observed measuring the temperature is simple, only two lines in the void loop () are
necessary to perform this task.
The result is as follows:

Connecting several temperature sensors:


We have two options or methods that we can use when we need to read more than one
temperature sensor.
The first method is to handle each sensor with a different pin from the Arduino. In this way we
do have 3 sensors, we will need to use 3 digital pins of the Arduino.
Another way is to use the same pin for all sensors, in other words all sensors are connected
to the same 1-Wire bus, and like any bus, each element or device has an identification or
address. In the case of the DS18B20 we need to find out your address that is unique and is
factory set.

Ex.2: Using several DS18B20 in different pins of the


Arduino:
For this case the connections are the following:
Each sensor works with a different pin and needs its own Pull-Up resistance of 4.7K.
The code to perform the readings is as follows:

#include < OneWire .h>


#include <DallasTemperature.h>

OneWire ourWire1 (2); // Pin 2 is set as OneWire


OneWire bus ourWire2 (3); // Pin 3 is set as OneWire bus

DallasTemperature sensors1 (& ourWire1); //


Declare a variable or object for our sensor1 DallasTemperature sensors2 (& ourWire2); // Declare a vari
able or object for our sensor2

void setup () {
delay (1000);
Serial . begin (9600);
sensors1. begin (); // Sensor 1
sensors2 is started. begin (); // Sensor 2 starts
}

void loop () {
sensors1.requestTemperatures (); // The command is sent to read the temperature
float temp1 = sensors1.getTempCByIndex (0); // The temperature in ° C of the sensor 1 is obtained

sensors2.requestTemperatures (); // The command is sent to read the temperature


float temp2 = sensors2.getTempCByIndex (0); // The temperature in ° C of the sensor 2 is obtained

Serial . print ( "Temperature 1 =" );


Serial . print (temp1);
Serial . print ( "C" );
Serial . print ( "Temperature 2 =" );
Serial . print (temp2);
Serial . println ( "C" );
delay (100);
}

This way of connecting two or more sensors is easy to understand and implement and is
useful when there are few sensors or we simply have pins available to connect more
DS18B20 like in an Arduino Mega.

Ex.3: Using several DS18B20 with a single pin of the


Arduino:
In this case we connect all the sensors to the same 1-Wire bus.
If necessary, it is possible to connect more sensors to the same data pin.

The difference here is that, being a bus, we need to find out the address of each sensor in
order to identify it.
The following sketch is only used to obtain the address of the devices connected to the 1-
wire bus:

#include < OneWire .h>

OneWire ourWire (2); // Pin 2 is set as OneWire bus

void setup ( void ) {


Serial . begin (9600);
}

void loop ( void ) {


byte addr [8];
Serial . println ( "Getting directions:" );
while (ourWire. search (addr))
{
Serial . print ( "Address =" );
for ( int i = 0; i <8; i ++) {
Serial . print ( "0x" );
Serial . print (addr [i], HEX );
}
Serial . println ();
}
Serial . println ();
ourWire reset_search ();
delay (2000);
}

The previous code helps us obtain the addresses of the sensors, in our case we have
obtained the addresses of the three sensors that we have connected, but can execute the
previous code for each sensor individually to know exactly the direction of their sensor.
Once the address is obtained, we can identify the sensor reading that we want to measure.

To make the readings we use the following sketch:

#include < OneWire .h>


#include <DallasTemperature.h>

OneWire ourWire (2); // Pin 2 is set as OneWire bus

DallasTemperature sensors (& ourWire); // Declare a variable or object for our sensor

DeviceAddress address1 = {0x28, 0xFF, 0xCA, 0x4A, 0x5, 0x16, 0x3, 0xBD}; // sensor address 1
DeviceAddress address2 = {0x28, 0xFF, 0x89, 0x3A, 0x1, 0x16, 0x4, 0xAF}; // sensor address 2
DeviceAddress address3 = {0x28, 0xFF, 0x23, 0x19, 0x1, 0x16, 0x4, 0xD9}; // sensor address 3

void setup () {
delay (1000);
Serial . begin (9600);
sensors begin (); // The sensor starts
}

void loop () {

sensors.requestTemperatures (); // send the command to obtain the temperatures


float temp1 = sensors.getTempC (address1); // The temperature in ° C of the sensor 1 is obtained
float temp2 = sensors.getTempC (address2); // The temperature in ° C of the sensor 2
float temp3 = sensors.getTempC (address3) is obtained; // The temperature in ° C of the sensor 3 is obt
ained

Serial . print ( "Temperature 1 =" );


Serial . print (temp1);
Serial . print ( "C" );
Serial . print ( "Temperature 2 =" );
Serial . print (temp2);
Serial . print ( "C" );
Serial . print ( "Temperature 3 =" );
Serial . print (temp3);
Serial . println ( "C" );
delay (100);
delay (100);
}

It must be taken into account that the addresses in the sketch must be replaced with the
addresses corresponding to the available sensors.
Here we show our results:

The sensed temperatures are similar since the sensors were in the same environment:
To change the sensor resolution to: 9, 10, 11 or 12 bits. only the function should be used:

sensors.setResolution (Address, 9); // 9-bit resolution

Normally the resolution is configured in the void setup () after initializing the sensors. The
lower the resolution, the shorter the reading time.
You can purchase the materials used in this tutorial in
our store:
- Arduino Uno R3
- DS18B20 Digital Temperature Sensor

Você também pode gostar