How to measure temperature with Arduino and DS18B20 sensor

Are you planning to know how to measure temperature with Arduino and DS18B20?

Ok, so, if your answer is yes you need to read this article.

Information About The DS18B20 Sensor

DS18B20 is a digital temperature sensor from prodused by Maxim IC that uses a bus called 1-Wire. It measures the temperature on its own surface in degrees Celsius with 9 to 12-bit precision, from -55 to 125 (+/-0.5) Celsius degrees. Each sensor has a unique 64-Bit Serial number etched into it, allows for a huge number of sensors to be used on one data bus.

It can be powered from 3.0V to 5.5V and uses only one port pin to transmits data. It can be attached to a controller or used without any other components. And, finally, there is some library support for Arduino. It seems a classical transistor in a TO-92 package.

There is a special metal-jacket version that is waterproof also and can be used for liquids temperature measurements or could be inserted into a metallic coupling.

The color of the wires are:

Black – GND – Ground – pin 1 of the original DS18B20

Red – +VDD – Power Supply – pin 3 of the original DS18B20

Yellow – DQ – Data Pin – pin 2 of the original DS18B20

Information About Arduino

Arduino is an open-source electronics platform based on easy-to-use hardware and software. The idea, in the beginning, was to offer a development platform for who had no specific experience in electronic hardware or software. Nowadays you can buy a wide range of Arduino boards and program them with the same IDE (Integrated Development Environment) in C++.

The official Arduino site can be reached here and it contains everything you need to start.

How to measure temperature with Arduino and DS18B20 sensor

Now, we knew what DS18B20 and Arduino are. We need to connect together and write some software.

The simplest way to do this is to connect following the scheme:

If we take an Arduino Uno board we can provide power supply to the DS18B20 directly from the board and we can use PIN 2 of the Arduino to connect the data line. We need a 4.7kOhm resistor connected from the data line to +Vdd. The function of this resistor is called PULL-UP.

1-Wire library and DS18B20 library

Now we need the software library to enable Arduino to connect and manage DS18B20 data.

Download the 1-Wire library at the following link:

and you need the DS18B20 library. You can download this one:

After you are installed Arduino IDE (the last available version) and the two libraries, you are ready to test the reading features with Arduino and DS18B20 sensor.

Two articles will be available on this website on how to download and install Arduino IDE and how to install libraries under Arduino IDE.

The code

Now you need to start Arduino IDE.

After that, you need to select the following code and directly past into the Arduino IDE.

/********************************************************************/
// First we include the libraries
#include <OneWire.h> 
#include <DallasTemperature.h>
/********************************************************************/
// Data wire is plugged into pin 2 on the Arduino 
#define ONE_WIRE_BUS 2 
/********************************************************************/
// Setup a oneWire instance to communicate with any OneWire devices  
// (not just Maxim/Dallas temperature ICs) 
OneWire oneWire(ONE_WIRE_BUS); 
/********************************************************************/
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
/********************************************************************/ 
void setup(void) 
{ 
 // start serial port 
 Serial.begin(9600); 
 Serial.println("Dallas Temperature IC Control Library Demo"); 
 // Start up the library 
 sensors.begin(); 
} 
void loop(void) 
{ 
 // call sensors.requestTemperatures() to issue a global temperature 
 // request to all devices on the bus 
/********************************************************************/
 Serial.print(" Requesting temperatures..."); 
 sensors.requestTemperatures(); // Send the command to get temperature readings 
 Serial.println("DONE"); 
/********************************************************************/
 Serial.print("Temperature is: "); 
 Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"?  
   // You can have more than one DS18B20 on the same bus.  
   // 0 refers to the first IC on the wire 
   delay(1000); 
}

Now we are ready to download it into Arduino Uno.

Connect the board via USB cable and wait Windows connect it.

From the Arduino IDE menu select TOOLS -> BOARD -> select ARDUINO UNO.

From the Arduino IDE menu select TOOLS -> PORT -> select the COM port appeared.

Now you can UPLOAD the sketch using the second button in the main bar of the Arduino IDE or, alternatively, you can select SKETCH -> UPLOAD from the menu or you can press the keyboard shortcut “CTRL“+”U

Now you need to launch the Serial Monitor from TOOLS -> Serial Monitor or you can use “CTRL“+”SHIFT“+”M” shortcut.

In the serial monitor, you will see the temperature the DS18B20 sensor is reading.