Tutorial:Measuring Temperature using DS18B20 with Arduino Uno

DS18B20 in out marketplace is a stainless steel tube encapsulation waterproof moisture proof digital temperature sensor that is rust proof and readily submersible into the water to measure temperature. Typical application suitable for temperature measurement and control field freezer, silos, storage tanks, telecommunications room, power room, cable ducts, bearings, cylinder, textile machinery, air conditioning, industrial equipment and other small space temperature.

Theory of Operation

The DS18B20 Digital Temperature Sensor provides 9 to 12-bit (configurable) temperature readings which indicate the temperature of the device. It communicates over a 1-Wire bus that requires only one data line (and ground) for communication with a central microprocessor. In addition, it can derive power directly from the data line (“parasite power”), eliminating the need for an external power supply.

The core functionality of the DS18B20 is its direct-to-digital temperature sensor. The resolution of the temperature sensor is user-configurable to 9, 10, 11, or 12 bits, corresponding to increments of 0.5°C, 0.25°C, 0.125°C, and 0.0625°C, respectively. The default resolution at power-up is 12-bit.

Bit 3 to Bit 0 are the 4 decimal places. For example, the increment of 0.0625°C requires 4 bits for the values after the decimal point. Bit 10 to Bit 4 are the values before the decimal points.

Hardware Connection

  1. Connect the DS18B20 sensor to the Arduino pin as accoding to the following figure connection.
  2. Copy the following source code and paste into a new Arduino Sketch in the IDE.
  3. Upload the code to the Arduino UNO. If there is error in the sketch, it is due to your Arduino IDE does not have the required library such as Dallas Temperature library. If you have the library, you can skip to instruction number 6.
  4. Go to your top bar >> Sketch >> Import Library >> Manage Library. 
  5. Type on the search bar : Dallas Temperature , Install the library.
  6. Open the Serial monitor and look for the temperature results.


/**include the libraries**/
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 5 //Data pin 5
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
/**declare variables**/
float Celcius=0;
float Fahrenheit=0;
void setup(void)
sensors.begin(); // Start up the library
void loop(void)
sensors.requestTemperatures(); //send command to obtain temperature reading
// Why "byIndex"?
// You can have more than one DS18B20 on the same bus.
// 0 refers to the first IC on the wire
Fahrenheit=sensors.toFahrenheit(Celcius); //convert from celcius to fahrenheit
Serial.print(" C ");
Serial.print(" F ");