Monthly Archives: April 2013

Interfacing DS18B20 temperature sensor to Arduino


In this hands-on article, we will discuss DS18B20 temperature sensor from Maxim Integrated company. We will hook up this sensor to Arduino through amazing OneWire bus. Then we will write code that will bring our digital thermometer to life, and print temperature back to our computer. We will write generic code that will work on every Arduino platform.

DS18B20 Programmable Resolution 1-Wire Digital Thermometer

I choose to present you this sensor for several reasons. First of all, availability. You can buy this sensor online for 2-3$ (One Wire Digital Temperature Sensor – DS18B20).
Another good thing about this sensor is programmable resolution. That means, we can choose precision over refresh time, and vice versa. If wired correctly, this sensor can be powered from data line. More informations about this sensor can be found in its datasheet:


If we take a glance at DS18B20 datasheet, we can see that this sensor has three pins:

1. GND – Supply ground

2. DQ – Data line

3. Vdd – Supply 3.0V to 5.5V

So we just connect those pins to Arduino, and we’re ready to go, right? Wrong.
Generally, before we work with any analog or digital electronics, we should read it’s datasheet.

Quote from DS18B20 datasheet:

“The DS18B20 uses Maxim’s exclusive 1-Wire bus protocol that implements bus communication using one control signal. The control line requires a weak pullup resistor since all devices are linked to the bus via a 3-state or open-drain port (the DQ pin in the case of the DS18B20).”

So, we will have to add weak pullup resistor on DQ bus line. For 1-5 sensors, 4.7k ohm resistor.

When we hook-up everything, it should look like this:


Now, we’re ready to do some code!


Before we start coding, we must add OneWire library to our Arduino IDE. You can download lastest library here:
Extract OneWire folder to libraries folder found in Arduino IDE folder. Now, write this code to new Arduino sketch. Explanations are in comments:


int SensorPin = 10; //Our sensor is connected to digital pin 10

//Initiate OneWire communication
OneWire ds(SensorPin);

void setup(void) {
//Initiate serial communication with our computer

void loop(void) {

float T = readT(); // call function to read from sensor
Serial.print(“Temperature is: “); // print out results to us
Serial.println(” deg.C”); // print out results to us


float readT(){
//returns the temperature from our sensor

byte data[12];
byte addr[8];

if ( ! {
return -300; // if there is no sensor on OneWire Bus, return -300 value

ds.write(0×44,1); // tell sensor to start converting
ds.write(0xBE); // tell sensor to start sending data

for (int i = 0; i < 9; i++) { // we receive data in this loop
data[i] =;


byte MSB = data[1];
byte LSB = data[0];

float raw = ((MSB << 8) | LSB); // move MSB left for 8 spaces, join that with LSB
float realTempC = raw / 16; // move decimal point left for 4 spaces, result our temperature
return realTempC;


Now, let’s test our device! Open Serial Monitor at 9600bps, and you should have readings like this:

Temperature is: 28.31 deg.C
Temperature is: 29.25 deg.C
Temperature is: 29.56 deg.C

That’s it! Have fun extending it.

You can find more information and examples for this topic at site: