How to use the DS18S20 and DS18B20 temperature sensors with Arduino

I bought from FunGizmos few DS18S20 temperature sensors to test for my small robot project and last week they finally got into my hands but only today I had the time to try them out.

To connect the sensors to the Arduino board I choose the parasite mode which derive power directly from the data line ( no need for an external power supply anymore 🙂 ), below are the pinouts, the electronic scheme and a photo of the circuit (though, really simple as I used a breadboard) :

pinouts ds18s20

pinouts ds18s20

Arduino connection for Dallas DS18S20

Arduino connection for Dallas DS18S20

real scheme ds18s20

After a little bit of research of Dallas’s one-wire interface and downloading the onewire library from I came out with a nice function to detect all the DS18S20 and DS18B20 sensors from a one-wire bus.

Here is the detection and temperature reading code : tutorialpedia.

A sample of their output :

–Search started–
Device is a DS18S20 : 10-1D-30-F9-01-08-00-51
–Search ended–

for the detection code and

Temp = 79.25 F or 26.25 C
Temp = 79.14 F or 26.19 C
Temp = 79.14 F or 26.19 C
Temp = 79.25 F or 26.25 C

for the temperature retrieving code.

Other helpful links :
* onewire.h library
* Dallas DS18S20 official document

Categorized: Arduino

29 comments on “How to use the DS18S20 and DS18B20 temperature sensors with Arduino

  1. The link on tutorialpedia doesn’t appear to work. Is this code posted some place else? Thanks!

  2. Thank you for the example. I was not getting realistic results however and traced it to the getTemperature routine. I do not know why for sure I was getting wildly high temps from your code as is, but this is how I changed it:

    float getTemperature(byte* address){
    int tr;
    byte data[12];
    float temp;



    //put in temp all the 8 bits of MSB
    tr = data[1];

    //check for negative temperature
    if (data[1] > 0x80){
    tr = !tr + 1; //two’s complement adjustment
    tr = tr * -1; //flip value negative.

    temp = tr * 256 + data[0]; //MSB + LSB
    temp = temp / 16.0; // think this was cpc, but not sure about reading reserved byte

    return temp;

  3. Hello dear friends.

    Mi problem is that I can’t read temperatures over 52 °C and I put the sensor (ds18b20) very near of a welder!!.
    The CRC is good.
    When I energize the circuit the first data is 85°C (LSB=50h, MSB=05h, TH=75, TL=70, CF=127, Reser1=255, Reser2= (change any times from 2 to 16, but many times is only 12), Reser3=16, and CRC= change).
    And with ice the lowest temperature is 20°C arghhh 🙁

    Is the ds18b20 not working? any idea?


  4. Hi Sorin,

    The tutorial appears to have meen mangled… is there any chance you could put it back up?

    Thanks heaps for a great resource!


  5. Thansk for that – it’s great & works a treat.
    I did however come across an issue – below freezing it doesn’t go below -1.25 C. As soon as it hit that, the next reading is -0.31 C. I tried it in the freezer and it kept jumping back up, but less & less frequently as time goes by as it tends towards its final temperature. It’s almost as though the integer part of the temperature is wrong. I’m afraid I’m not smart enough to fix it. I graphed it in excel so I can show you what’s happening, but can’t paste it in here I guess, but can email or whatever to anyone.
    Thanks, Simon.

  6. Hi Simon,

    Try to ground pin 3 as tinux suggested. I’ll run some more tests tomorrow because I’m off work 🙂 and will get back and modify the post if it is the case.

    Best wishes


  7. Hi Sorin, Hope the exams went well. Did you get a chance to try it? I confirmed it’s not a grounding issue – almost certain it’s coding.

  8. Pingback: using a DS18B20 temp probe

  9. Hi,

    i can confirm the problem with the negative temperatures. I also think the measured temperature is +-3° above the real temp.

    Simon, did you ever get this working?

  10. Pingback: Temperaturna postaja « gregag

  11. Thanks a lot!!

    This was exactly the code i was looking for! Al the other code’s on the internet are very large, en they don’t give you the right info you need!

    Thanks again!

  12. Andreas, I think you’re correct. The resolution is different between the 18S20 and the 18B20, so some bit twiddling is required.

  13. in parasite mode you should still connect the third pin to ground so says the datasheet…. I’m interested in Vdd wiring, I’m hoping this will give me faster temp returns does anyone know where i could find this before i venture into the unknown…

  14. I think you should make clear that this code does not work for DS18B20.
    I doubt that it works for negative temperatures even for DS18S20, !tr is logical inversion, while you need bitwose inversion ~tr to negate a 2’s complement integer number.
    The code for DS18B20 is actually very simple:
    float celsius=(short int)((data[1]<<8)+data[0])/16.;
    return celsius;

  15. Pingback: Arduino, LED-s, LM35 and DS18B20 or temperature sensors. » Ogalik

  16. Pingback: First meeting in Pu’unene space | Maui Makers

  17. Pingback: First meeting in Pu’unene space – Maui Makers

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.