Tutorial Digital Temperature Sensor DS18B20
Tutorial Digital Temperature Sensor DS18B20
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:
This form of feeding is the most recommended and is the one used in this tutorial.
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
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:
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
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.
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:
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.
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 () {
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:
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