Temperature With DS18B20




Introduction: Temperature With DS18B20

It's a small project about temperature monitoring. I used a DS18B20 digital device to get the temperature. The device uses OneWire protocol. You need only an Arduino board, a DS18B20 and a 4.7KOhm resistor and a breadboard. If you have a display shield for the Arduino that's a plus.

Step 1: What You Have to Know About DS18B20

The device is communicating on one common bus. It means that you can connect several devices. We use only two pins on the device to connect them to the Arduino board. We need two libraries in the sketch: OneWire and DallasTemperature .

So we must include these libraries.

Step 2: Setup

We define the communication IO port:
#define ONE_WIRE_BUS 3

We need two object to communicate with the device(s):
OneWire ds(ONE_WIRE_BUS);
DallasTemperature sensors(&ds);

ds object will handle the onewire protocol
sensors object will handle the temperature monitoring devices

I chained 3 devices in this example on a breadboard. On the left side I used a potmeter instead of a 4.7K resistor.

Step 3: Inventory

1st step is to start the sensors:

2nd step is to count the number of sensors on the bus. This function sets the global variable: maxsensors

3rd step is to request the temperatures from the devices:

4th step is to read the temperatures one by one:
for (int i=0;i {
float f = sensors.getTempCByIndex(i);
lcdprint(dtostrf(f, 5, 1, buffer));

lcdprint is a function to display the result on an LCDshield
dtostrf is a function defined in stdio library. it must be included.

Step 4: Source Code

Some link for downloading the source codes

Full source code
Dallas Temperature library by Miles Burton
LCD shield library, I used in this project

The LCD shield was ordered from here



    • Creative Misuse Contest

      Creative Misuse Contest
    • Fix It! Contest

      Fix It! Contest
    • Metalworking Contest

      Metalworking Contest

    23 Discussions


    1 year ago

    my ds18b29 sensor has only two wires so howto interface with it.

    please give me details of the pins

    1 reply

    I am facing some problem here.

    1) Where should those library files be actually placed? ../libraries or some home folder where I keep my source codes?

    2) Due to this the IDE does not compile it. I then placed the files in a folder src and now there is new error.

    #include Nested deeply

    I am at my wit's end. Please help!

    1 reply


    If you do not have a sketchbook folder (sometimes it iscalled "Arduino") the libraries folder must be in that one.
    You can find your Sketchbook location under "File-Preferences"


    2 years ago

    In parasitic mode, you need to connect VDD to Ground. Additionally,
    you may need to replace the resistor as with 4.7kΩ the sensor may not
    get enough power to run at all.
    On a 10 meter cable run with
    parasitic power, I get reliable results when resistor is less than
    2kΩ. With 4.7kΩ, I got over 95% read fails. With 2.3kΩ I got around 18%
    fails. With 1.9kΩ I get 1.7% fails.

    1 reply

    Thanks for the tip. It can be useful in the future for everyone.

    I have no problem with a 20 cm long cable for years.
    for those who have problem compiling it, I uploaded my Arduino 1.04 IDE with the integrated libraries and the project file. feel free to download and test it.



    This is very helpful, but IMHO there's an error in the Step 1 diagram showing the resistor; if you simply put the 4.7K resistor on the power line as shown you also drop the voltage to the device while pulling the data line high. Putting the resistor on the data line will keep voltage to the devices nominal and pull data high as well. Works great otherwise and thank you for putting it up!

    2 replies

    Actually MadScott, the schematic is correct. The OneWire devices can operate either with a power line, or in parasitic mode, where power is drawn from the data line. If you look at the schematic you will see pin 3 (VCC) on the devices is unconnected. This means they are using parasitic mode. The OneWire device will effectively "charge" itself from the data line, using VCC via the 4.7k resistor, and use use this charge to run during times when the data line is used to exchange data. Note that the charge is very small and only designed to last while transferring data. This places some constraints on the data transfers, such as how long the line must be idle before any communication to ensure all devices are charged. The reason the resistor must be so high is so that the data line can still be asserted low without drawing too much from the host power supply. The libraries take of this detail and in practice it works really well. Hope this helped.

    Here's a quote from the datasheet for the DS18B20: "When the DS18B20 is used in parasite power mode, the VDD pin must be connected to ground."



    This is my first Arduino project and so far have one DS18B20 working as a basic thermostat using a code I found that didn't required the Dallas Temperature Library. However I would love to use three sensors and the display as shown here, the only problem is that I don't seem to be able to find the actual variables that contain the temperature values for each sensor. I will need these to operate various hardware such as fans, actuators and heaters based on a temperature set point and the values for each of the sensors.

    Can anyone help with some code to create or access these variables.


    3 replies


    If you want to use more sensors, then I recommend using the library. ScanSensors will do a loop and read all the sensors on the same wire. It runs from the Setup function, so let's say it collects how many sensors we have. Then we read the individual sensor temperatures from the Loop function. First we send a command to measure the temperature: sensors.requestTemperatures(); After we can re the values by an index: float f = sensors.getTempCByIndex(i);


    Thanks Mark,

    I am very new to the C programming language but after some reading I think you have created an array that contains the sensor values and to access each one individually they would be called f[0], f[1] and f[2], is this right?

    Unfortunately I haven't been able to test this theory as I am getting multiple errors when I try and compile the sketch. In fact I can't even compile the examples in the onewire and Dallas Temperature library's that came from the link above. The errors from the OneWire library example are to do with "WConstants.h: Nosuch file or directory" and a bunch of variables that "was not declared in this scope" like digitalPinToBitMask and delayMicroseconds etc. The errors from your sketch are pointing to the LCD4bit_mod library file location and also mention the "WConstants.h: Nosuch file or directory" and a bunch of variables that "was not declared in this scope".

    Is there likely to be a simple fix for this?

    Well I can now see I was a little off the mark. I have found that the individual readings can be accessed by

    float temp1 = sensors.getTempCByIndex(0);

    float temp2 = sensors.getTempCByIndex(1); etc....

    The WConstants.h error can be resolved by removing the #include "WConstants.h" from the .cpp file and adding #include "Arduino.h" to the .h file as per https://sites.google.com/site/kingauswebpage/my-pr...


    That's cool. I found it in my project folder too :)
    if you need it for some reasons:


    an older version:


    Thanks Mark,

    Oddly enough I am having issues with OneWire library. Did you recieve a WConstants.h error when trying to compile? I attached an image of the error and my OneWire.h library.



    No I didn't get such error message when I used 1.0.4 version of IDE with this library: http://oscomputer.hu/download/arduino/DallasTemperature_OneWire_for_Arduino_1.0.4.zip



    Thanks for the code. I get another error now, see attached, and am using it for a sous vide application.

    arduino error screenshot.jpg

    I can still recommend you using Arduino 1.0.4.

    That version is working and can be compiled. See attached image.


    double checked, no compile errors at all. I used this simple source:

    #include <OneWire.h>

    #include <DallasTemperature.h>

    #include <stdio.h>

    #define ONE_WIRE_BUS 3

    char buffer[25];

    byte maxlines = 2;

    char line1[16];

    char line2[16];

    byte x = 0;

    byte y = 1;

    byte maxsensors = 0;

    /* DS18S20 Temperature chip i/o */

    OneWire ds(ONE_WIRE_BUS);

    DallasTemperature sensors(&ds);

    void flashled(void)


    digitalWrite(13, HIGH);


    digitalWrite(13, LOW);


    void scanSensors(void)


    byte i;

    byte present = 0;

    byte data[12];

    byte addr[8];

    while (ds.search(addr))



    for( i = 0; i < 8; i++) {

    Serial.print(addr[i], HEX);

    Serial.print(" ");


    if ( OneWire::crc8( addr, 7) != addr[7]) {

    Serial.print("CRC is not valid!\n");



    if ( addr[0] == 0x10) {

    Serial.print("Device is a DS18S20 family device.\n");



    else {

    if (addr[0] == 0x28) {

    Serial.print("Device is a DS18B20 family device.\n");



    else {

    Serial.print("Device is unknown!\n");

    Serial.print("Device ID: ");






    // The DallasTemperature library can do all this work for you!



    ds.write(0x44,1); // start conversion, with parasite power on at the end

    delay(1000); // maybe 750ms is enough, maybe not

    // we might do a ds.depower() here, but the reset will take care of it.

    present = ds.reset();


    ds.write(0xBE); // Read Scratchpad



    Serial.print(" ");

    for ( i = 0; i < 9; i++) { // we need 9 bytes

    data[i] = ds.read();

    Serial.print(data[i], HEX);

    Serial.print(" ");


    Serial.print(" CRC=");

    Serial.print( OneWire::crc8( data, 8), HEX);




    Serial.print("No more addresses.\n");




    void setup(void) {


    pinMode(13, OUTPUT);

    byte i, j;

    for (j=0;j<16;j++)


    line1[j] = 32;

    line2[j] = 32;


    line1[16] = 0;

    line2[16] = 0;

    x = 0;

    y = 1;




    void loop(void) {

    sensors.requestTemperatures(); // minden eszköztől kérjük a hőmérséklet mérést...

    for (int i=0;i<maxsensors;i++)



    float f = sensors.getTempCByIndex(i); // egyesével kiolvassuk a mért értéket

    Serial.print("Sensor ");


    Serial.println(dtostrf(f, 6, 2, buffer));