Description
The DS18B20 Digital Temperature Sensor is a popular option for adding temperature sensing over the range of -55 to 85°C to a project.
PACKAGE INCLUDES:
- DS18B20 Digital Temperature Sensor
- 4.7K resistor (for pull-up)
KEY FEATURES OF DS18B20 DIGITAL TEMPERATURE SENSOR:
- 1-Wire interface only requires one uC pin
- Multiple devices can be connected to the same 1-Wire bus
- Digital readout is less sensitive to electrical noise than analog sensors
- -55 to 85°C measurement range with 0.5°C accuracy°
- 3.3 and 5V compatible
If you have a uC like an Arduino and you want to measure temperature, the DS18B20 is one of the most popular of the temperature measurement ICs to use. Being a digital device, it can avoid some of the pitfalls of analog sensors which are more sensitive electrical noise pickup, especially if the device is remote from the uC.
The DS18B20 is packaged in a 3-pin TO-92 package which is the size of a small transistor which makes it easy to locate in tight PCB areas or it can be soldered to a cable and remotely mounted, such as on a heat sink.
An alternative for temperature measurement is to use an analog temperature measurement IC like the LM35. The LM35 analog output can be read directly from an A/D input port on the microprocessor which makes it very easy to use without using a library. The trade off is that the LM35 is more sensitive to electrical noise and additional care may be needed to ensure that the readings are accurate, especially if the temperature sensor is remotely mounted. The LM35 can also only have one device connected to each A/D pin, while the DS18B20 can have multiple devices connected in series to one digital pin as each device has a unique serial number for addressing.
Powering the DS18B20
Input power for the device can range from 3.0V to 5.5V. This power is normally tapped off the uC power or it can use a separate power supply. This requires three wires to be run to the sensor (power, ground, 1-Wire bus).
As an alternative option for powering the device, it can be powered off the 1-Wire bus line so that only two wires are required (ground , 1-Wire bus). In this parasitic power mode, it runs off the power available through pull-up resistor when the 1-wire bus is high. This mode requires some special considerations that are fully documented in the datasheet. Generally it is best just to stick with the 3-wire hookup.
Communicating to the DS18B20
The DQ pin on the device is connected to a digital pin on the uC to establish the 1-Wire communication bus.
A pull-up is needed on this line as it is open-collector, so we throw a 4.7K resistor in the bag which is the specified value to use. Typically, the internal pull-ups available on uCs such as Arduino will suffice for this pull-up. It is a weaker pull-up but will generally work if you are not using a bunch of sensors on the same bus or are using very long wires. If communications are spotty using this approach, try installing the 4.7K pull-up.
The 1-Wire serial bus communication protocol can look daunting, but fortunately most poplar uC have software libraries which make these devices very straightforward and easy to use.
There are some options which can be programmed such as choosing between number of bits of resolution vs conversion time, but those can all be safely ignored for establishing basic operation.
Our Evaluation Results:
The DS18B20 can be used to experiment with a number of different applications where temperature measurement is useful such as determining when a cooling fan should be turned on and it is perfectly suitable for integration into a final product.
The software below implements basic communications with the sensor using the 1-Wire bus protocol and provides a read-out of the sensor output.
We are hooking it up to pin 4, but this can be changed to any digital pin. We are using the OneWire.h and DallasTemperature.h libraries.
DS18B20 Digital Temperature Sensor Example Program
/* DS18B20 Digital Temperature Sensor Test Basic code to establish communication with the DS18B20 and retrieve temperature measurement data. Requires OneWire and DallasTemperature Libraries */ #include <OneWire.h> #include <DallasTemperature.h> const int ONE_WIRE_BUS = 4; // Define a pin for communicating to the DS18B20 device via the oneWire bus. OneWire oneWireLocal(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with the DS18B20 device DallasTemperature sensorsLocal(&oneWireLocal); // Pass this oneWire reference to DallasTemperature float tempLocal = 0.0; // Variable for holding the temperature returned from the sensor //=============================================================================== // Initialization //=============================================================================== void setup() { Serial.begin (9600); // Set output window comm rate sensorsLocal.begin(); } //=============================================================================== // Main //=============================================================================== void loop() { CheckTemps(); // Call the routine that actually does the work Serial.print("Current Temp: "); // Printout the results Serial.println(tempLocal); delay(1000); } //=============================================================================== // Subroutines //=============================================================================== void CheckTemps() { sensorsLocal.requestTemperatures(); // Send command to get temperature from the DS18B20 // The sensor will return reading from previous request unless a delay is used to give it time to // complete the reading request. If polling every second like we are doing here, the delay can be ignored. delay(100); tempLocal = sensorsLocal.getTempCByIndex(0); // There can be more than one device on this same bus // so we need to use the first index of (0) }
Notes:
- None
TECHNICAL SPECIFICATIONS
Interface | Communication bus | 1-Wire |
Input | Logic High | > 2.2V |
Logic Low | < 0.8V | |
Supply Voltage | Maximum | 5.5V |
Minimum | 3V | |
Temperature | Max Measurement Range | -55°C to +85°C |
Accuracy (-10°C to 85°C) | +/- 0.5°C | |
Package | TO-92 | |
Package Type | Plastic, thru-hole | |
Mfr | Dallas / Maxim | |
Datasheet | DS18B20 |