Page 1 of 1

How to measure temperature with your Arduino and a DS18B20

How to measure temperature with your Arduino and a DS18B20
   82

In this example project we will be combining an Arduino and a DS18B20 temperature sensor.

The DS18B20 is a so called 1-wire digital temperature sensor. The words “digital” and “1-wire” make this sensor really cool and allows you, with a super simple setup, to read the temperature of one or more sensors. You can even connect multiple devices together, utilizing only one pin on your Arduino.

The more I play with the Arduino, the more fun it gets 




Arduino and DS18B20 Temperature Sensor

The DS18B20 comes in different forms and shapes, so you have plenty of choice when deciding which one works best for you. There are 3 variations available: 8-Pin SO (150 mils), 8-Pin µSOP, and 3-Pin TO-92. If you look at eBay or Amazon, some of our Chinese friends have packaged the TO-92 version in a waterproof housing so you can use it directly in your fish-tank, water boiler, outside your house or even your freezer, without having to worry about water/moisture shortcutting your electronics (these have 3 wires: Black (GND), Red (Vdd) and white or yellow (Data)).

DS18b20 - Available Packages

DS18b20 – Available Packages

DS18B20 in a waterproof casing

DS18B20 in a waterproof casing

The DS18B20 is quite versatile. It can be powered through the data line (so called “parasite” mode, which requires only 2 wires versus 3 in normal mode), it operates in a 3.0V to 5.5V range, measures Temperatures from -55°C to +125°C (-67°F to +257°F) with and ±0.5°C Accuracy (from -10°C to +85°C). It converts a temperature in 750ms or less to a up to 12 bits value.

Another cool feature is that you can connect up to 127 of these sensors in parallel, and read each individual temperature. Not sure what I’d do with that, but the ability to combine one or two, for example for the temperature of your fridge and freezer, is a nice option, specially when other pins of your Arduino are being used for other things …

What we need to get the Arduino and DS18B20 to work

Software
  • Of course we will need the Arduino IDE software
  • OneWire library for the Arduino and DS18B20 (will save you a lot of work)
  • Our own sketch …

The Arduino IDE can be downloaded from the Arduino website (highly recommended), or from Tweaking4All (this version might be outdated by the time you read this article).

The OneWire Library can be downloaded from the OneWire Project Page (highly recommended to get the latest version) or you can download v2.2 from Tweaking4All.

DOWNLOAD - Arduino Windows 

Platform: Windows, 32 bits
Filename: arduino-ide-windows.exe
Version: 1.6.7
Size: 80.7 MiB
Date: February 8, 2016
 Download Now 

DOWNLOAD - Arduino MacOS X 

Platform: Mac OS X
Filename: arduino-ide-macosx.zip
Version: 1.6.7
Size: 136.4 MiB
Date: February 8, 2016
 Download Now 

DOWNLOAD - Arduino Linux 32 bit 

Platform: Linux, 32 bits
Filename: arduino-ide-linux32.tar.xz
Version: 1.6.7
Size: 92.1 MiB
Date: February 8, 2016
 Download Now 

DOWNLOAD - Arduino Linux 64 bit 

Platform: Linux, 64 bits
Filename: arduino-ide-linux64.tar.xz
Version: 1.6.7
Size: 91.0 MiB
Date: February 8, 2016
 Download Now 

DOWNLOAD - OneWire 

Filename: OneWire.zip
Version: 2.2
Size: 14.7 KiB
Date: March 19, 2014
 Download Now 

Harware

The USB cable is of course only needed to program and power the Arduino during development.
When we run the Arduino computer independent you might want to look for a suitable power supply.

Wiring Arduino and DS18B20

The wiring, this is the beauty of a 1-wire interface, is super simple.

The GND pin of the DS18B20 goes to GND on the Arduino.
The Vdd pin of the DS18B20 goes to +5V on the Arduino.
The Data pin of the DS18B20 goes to a (digital) pin of your choice on the Arduino, in this example I used Pin 2.

The only thing we need to add is a pull-up resistor of 4.7 KΩ. I have seen ther values, but it seems this is the most commonly used value.

The wiring presented below is the regular way of wiring the DS18B20 with your Arduino.

  Make sure that you very line 10 and line 65 in the Sketch below to match your data pin and power mode!

DS18B20 and Arduino wiring

DS18B20 and Arduino wiring

Below a picture of my test setup … it’s really easy.

Arduino and DS28B20 setup is super simple ...

Arduino and DS28B20 setup is super simple …

Parasite Power vs Regular Power

The alternative wiring is called “Parasite” mode, where we do NOT connect the +5V to the Vdd pin of the DS18B20.
Instead we connect Vdd of the DS18B20 to GND. The advantage being obvious: you only need 2 wires.

The downside of parasite mode is that you should limit the number of sensors and the wires should not be too long!
In the end, I’m not sure what to think of the Parasite mode, as I’d like to use this when the sensors are far away from the Arduino, but on the other hand this mode is not very suitable for that. Up to you which you prefer.

  Plenty of strange results have been report when using Parasite mode. Therefor I cannot recommend Parasite mode for a reliable setup.

Arduino Sketch for Measuring Temperature

Installing the OneWire Library

If you downloaded the ZIP file from either OneWire Project Page from Tweaking4All, then you should be able to use the Library Import option in the Arduino IDE software. From the menu, choose “Sketch Import Library Add Library” and select the ZIP file you downloaded.

In case this fails :
The Arduino IDE can’t seem to find the proper directory name in the ZIP file, so the easiest then is to unzip the file, make sure the directory is called “OneWire” (which holds the “examples” directory and a bunch of files) and drag it into the library directory of your Arduino IDE.

  To find the examples files in the menu’s, you might need to restart the Arduino IDE.

Loading the Sketch

The following Sketch comes with OneWire, so you’ll find it in the ZIP file. If you installed the OneWire Library, the you’ll find it in the examples.

Go to “File Examples OneWire” and select the “DS18x20_Temperature” example, which will look like the code below.

The code below uses the OneWire Library to conveniently go through all connected DS28B20 temperature sensors (see how to connect multiple sensors at the end of this article) and checks the details of each of them, to then display the data in the debug window (menu: “Tools Serial Monitor“).

The output in the debug window will look something like this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ROM = 28 88 84 82 5 0 0 6A
  Chip = DS18B20
  Data = 1 56 1 4B 46 7F FF A 10 D1  CRC=D1
  Temperature = 21.37 Celsius, 70.47 Fahrenheit
No more addresses.

ROM = 28 88 84 82 5 0 0 6A
  Chip = DS18B20
  Data = 1 56 1 4B 46 7F FF A 10 D1  CRC=D1
  Temperature = 21.37 Celsius, 70.47 Fahrenheit
No more addresses.

ROM = 28 88 84 82 5 0 0 6A
  Chip = DS18B20
  Data = 1 56 1 4B 46 7F FF A 10 D1  CRC=D1
  Temperature = 21.37 Celsius, 70.47 Fahrenheit
No more addresses.

Regular Power or Parasite Power? 

The DS18B20 can work in a regular mode or in a so called Parasite mode.
The normal mode requires 3 wires, whereas the Parasite mode uses only 2 wires.

You will need to set the right mode to get correct temperature readings:
  For Parasite mode line 65 should read: ds.write(0x44, 1);
  For Regular mode line 65 should read: ds.write(0x44);

Make sure you set the right data pin! 

In line 10, where it says “OneWire ds(2);” the data pin to be used is set.
In this example I used pin 2, however the default setting in the example that comes with OneWire uses pin 10.
(I had other plans for pin 10, so I did set mine to pin 2)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <OneWire.h>

// OneWire DS18S20, DS18B20, DS1822 Temperature Example
//
// http://www.pjrc.com/teensy/td_libs_OneWire.html
//
// The DallasTemperature library can do all this work for you!
// http://milesburton.com/Dallas_Temperature_Control_Library

OneWire  ds(2);  // on pin 2 (a 4.7K resistor is necessary)

void setup(void) {
  Serial.begin(9600);
}

void loop(void) {
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;

  if ( !ds.search(addr)) {
    Serial.println("No more addresses.");
    Serial.println();
    ds.reset_search();
    delay(250);
    return;
  }

  Serial.print("ROM =");
  for( i = 0; i < 8; i++) {
    Serial.write(' ');
    Serial.print(addr[i], HEX);
  }

  if (OneWire::crc8(addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return;
  }
  Serial.println();

  // the first ROM byte indicates which chip
  switch (addr[0]) {
    case 0x10:
      Serial.println("  Chip = DS18S20");  // or old DS1820
      type_s = 1;
      break;
    case 0x28:
      Serial.println("  Chip = DS18B20");
      type_s = 0;
      break;
    case 0x22:
      Serial.println("  Chip = DS1822");
      type_s = 0;
      break;
    default:
      Serial.println("Device is not a DS18x20 family device.");
      return;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44);        // start conversion, use ds.write(0x44,1) 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.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

  Serial.print("  Data = ");
  Serial.print(present, HEX);
  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.println();

  // Convert the data to actual temperature
  // because the result is a 16 bit signed integer, it should
  // be stored to an "int16_t" type, which is always 16 bits
  // even when compiled on a 32 bit processor.
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    //// default is 12 bit resolution, 750 ms conversion time
  }
  celsius = (float)raw / 16.0;
  fahrenheit = celsius * 1.8 + 32.0;
  Serial.print("  Temperature = ");
  Serial.print(celsius);
  Serial.print(" Celsius, ");
  Serial.print(fahrenheit);
  Serial.println(" Fahrenheit");
}

How to connect multiple Sensors

The DS18B20 Digital Temperature sensor allows you to set multiple in parallel. When doing this, the OneWire library will read all sensors.
Below I’ve illustrate 2 ways of wiring your sensors for bth methods of powering the sensors.

  For larger networks of sensors (more than 10), using smaller resistors should be considered, for example 1.6 KΩ or even less.

  It has been observed that large amounts of sensors (more than 10) in the same network can cause issues (colliding data), and for that purpose an additional resistor of say 100 … 120 Ω should be added between the data line to the Arduino and the data pin of the sensor, for each sensor !

The output of the previous demo Sketch for 2 sensors could like this:


1
2
3
4
5
6
7
8
9
10
ROM = 28 88 84 82 5 0 0 6A
  Chip = DS18B20
  Data = 1 51 1 4B 46 7F FF F 10 FE  CRC=FE
  Temperature = 21.06 Celsius, 69.91 Fahrenheit

ROM = 28 DA CA 27 5 0 0 49
  Chip = DS18B20
  Data = 1 4E 1 4B 46 7F FF 2 10 D9  CRC=D9
  Temperature = 20.87 Celsius, 69.57 Fahrenheit
No more addresses.

Selecting the right sensor

It would be nice to know what sensor result you’re looking at when using multiple sensors. So how do I identify the right one?

Serial Number

Since these sensors are digital, they do have a serial number, which can be used to identify from which sensor the originated.
All nice and dandy of course, but you’d need to know the that serial number first, right?

The debug output from our example sketch provides exactly that as a 64-bit serial number (the “ROM” value), for example:
28 88 84 82 5 0 0 6A    or    28 DA CA 27 5 0 0 49   in the example output above.

Again: It is suggested, for a large amounts of sensors (meaning 10 or more), to add a 100 … 120 Ω resistor between DS18B20 data pin and Arduino data pin (for each sensor!) to improve reliability. I can’t provide any experience concerning this, as I have only 2 sensors … for now.

Normal Power Mode

Below you will see how we can set multiple sensors in parallel using the regular powermode.

DS18B20 - Multiple sensors in Normal power mode

DS18B20 – Multiple sensors in Normal power mode

Parasite Power Mode

Parasite mode basically drops the wire for Vdd and shorts pin 1 and 3 of the sensor, and the sensor will pull power from the data pin.

DS18B20 - Multiple sensors in Parasite power mode

DS18B20 – Multiple sensors in Parasite power mode

More Resources

Below a list of practical resources, some offering very detailed technical information.

Donation options


Donations are very much appreciated, but not required. Donations will be used for web-hosting expenses, project hardware or a motivational boost (a drink or snack). Thank you very much for those have donated already! It's truly AwEsOmE to see that folks like our articles and small applications.

Comments


There are 82 comments. You can read them below.
You can post your own comments by using the form below, or reply to existing comments by using the "Reply" button.

  • Mar 22, 2014 - 11:17 PM - Willy Comment Link

    This would be great to add to the raspberry pi. Maybe combine both the ambilight and this to use with an overclock raspberry pi since an arduino has more power than the ws2801 need. Just a thought. Great work man. Your guides are great. Very informative. Keep up the good work

    Reply

    Willy

    • Mar 23, 2014 - 12:22 AM - hans Comment Link

      Thanks Willy! Much appreciate it! 

      I’m sure the Arduino can handle a sensor next to the boblight (if it doesn’t screw up timing too much). Then again … maybe the Raspberry Pi might be able to pull it off on itself as well. Are you thinking overheating detecting?

      I’m working on expanding the project in this article, by adding an Ethernet interface … works already pretty good!

      Reply

      hans

    • Mar 25, 2014 - 11:32 AM - hans Comment Link

      Yesterday I added an article for a cheap Ethernet connection, and today one on how read sensor data over the network … 

      Reply

      hans

  • Dec 30, 2014 - 5:32 PM - serafettin Comment Link

    How much can I get good days come easy .. 4 sensor information from all of them tied separately. Examples ..

    1.sens 15

    2.sen 20

    3.sens 8

    4.sens 35

    Can you help?

    Reply

    serafettin

    • Jan 1, 2015 - 6:39 AM - hans - Author: Comment Link

      Hi Serafettin,

      I understand that you’re using 4 different sensors, independently? Meaning you’re testing them one at a time, yet you get very random results?

      If that’s the case; 

      – Verify the conversion code?

      – I’d consider contacting your hardware supplier, there seems to be something wrong?

      – Are the sensors identical?

      With the sensors I have, I do see a tiny discrepancy between two sensors, but that’s about 0.1 degree … which I do not consider an issue, and is not nearly as bad as what you have observed.

      Reply

      hans

    • Jan 1, 2015 - 10:29 AM - hans - Author: Comment Link

      Hi Serafettin,

      I received a reply from you by email, but was unable to read … I only speak English, German or Dutch … 
      Also; please do not reply by email, since I won’t be able to reply here (could be useful info for others) …. 

      Reply

      hans

  • Apr 17, 2015 - 2:11 PM - jeffrey Stuart Comment Link

    Do you have a version using an LCD Display 12X4, 16X2 or 20X4

    I would very much appreciate this

    Thank you in advance

    Reply

    jeffrey Stuart

    • Apr 18, 2015 - 2:33 AM - hans - Author: Comment Link

      Hi Jeffrey!

      Well, I do have such a display available, and I like the idea of extending this article with such a display.
      However, my display is laying at home and I have been traveling a lot the past few months. So I’ll have to wait until I’m home again to work with that, or order an Arduino, Temp sensor and Display while traveling …

      Let me see which one comes first ….

      Reply

      hans

  • Apr 19, 2015 - 6:40 AM Comment Link
    PingBack: wordpress.yousmellmail.com

    […] links that helped me created this build. Arduino Ethernet – Pushing data to a (PHP) server How to measure temperature with your Arduino and a DS18B20 Brick-Temperature-DS18B20 Arduino Starter Kit Example […]

  • Jul 1, 2015 - 3:51 PM - Taz Comment Link

    Keep in mind that the pin diagrams for the DS18B20 in the wiring diagrams are oriented with you looking at the pins from the bottom. Made the mistake of misinterpreting the diagram and accidentally made a sensor unhappy by plugging it in backwards. Hope this helps!

    Reply

    Taz

  • Jul 28, 2015 - 2:06 AM - Viktor Comment Link

    Hi all,

    Very nice tutorial, thanks. I would try to make a dashboard with dweet and freeboard like this one. Can somebody help me with this or direct me to some other tutorial? I have 5 Temp Sensors at the moment, 2 HMC5883L Magnetometer are comming later (i want to check the on/off status of my heating pump).

    Thanks and BR
    Viktor

    Reply

    Viktor

    • Jul 28, 2015 - 4:17 AM - hans - Author: Comment Link

      Hi Viktor!

      Sounds like an awesome project!
      I can’t really help too much right now as I have yet to start a project with dweet.io or freeboard.io, the link you provided does seem to cover quite a bit though.

      The widget looks very slick, so I might take a look at that one of these days.
      Hopefully other user can chime in here?

      Reply

      hans

  • Aug 12, 2015 - 3:32 PM - Elson Avallone Comment Link

    Hi, My name is Elson Avallone and I’m Phd student at Universidade Estadual Paulista “Júlio de Mesquita Filho” – UNESP Bauru. I would like to have autorization to use your Arduino programm system and also to put it in my thesys.

    Please send me your answer

    Reply

    Elson Avallone

    • Aug 13, 2015 - 2:26 AM - hans - Author: Comment Link

      Hi Elson,

      feel free to use the information in this article for your thesis.
      Please mention the source if possible.

      Good luck with your thesis – I know how much work that can be … 

      Reply

      hans

  • Aug 21, 2015 - 6:15 AM - deen Comment Link

    how to make program on pir sensor plus temperature sensor?? when the temperature sensor state on the lcd player at 27 degree celcius,the pir sensor will not work eventough the person walk through it but when the temperature is below then 27 degree celcius,the pir sensor will work normally….

    Reply

    deen

  • Sep 19, 2015 - 1:14 PM - Sal Ciampa Comment Link

    Hi. Is it posible to have this read the temp then Tweet it?? I’d like to try building it…but I have NO experience with Arduino or coding :(

    Reply

    Sal Ciampa

    • Sep 19, 2015 - 3:46 PM - hans - Author: Comment Link

      Hi Sal!

      Ehm, I have never done it, but you should be able to do it.
      Take a look at these two articles: Arduino ENC28j60 ethernet, and Arduino Ethernet Data-push.

      Now neither of these two articles will show you how to Tweet, but will show you some of the network basics.

      To be able to Tweet, you’d need to probably consult libraries like Tweet-library for Arduino, or this Twitter Library (seems to have more details). In the end; the temperature is just text, so I’d assume that with help of these libraries you’d be able to Tweet them.

      Possible issues might be running out of memory on for example a Nano or an Uno. You might have to resort to a Arduino Mega.

      Hope this help … 

      Reply

      hans

  • Sep 23, 2015 - 12:53 PM - Joop van Schaik Comment Link

    Hi

    I want print a value from 237 in 23.7. Can you that explain to me.

    greetings from holland

    Joop

    Reply

    Joop van Schaik

    • Sep 23, 2015 - 1:39 PM - hans - Author: Comment Link

      Hi Joop!

      Good question, I’m carefully assuming you mean: how to print a floating point number to serial …
      (I’m sure you figured out how to divide 237 by 10 to get 23.7 on your own haha).

      There are no “easy” tricks to do this, but you could consider using the function dtostrf().

      Untested example (not having my gear near me), but it should be something like the code below.
      No special include needed as far as I recall.

      Hope this helps … there are other methods. 

      char outChr[7];
      
      void setup() {
        dtostrf(123.45678,7,3,outChr);
        Serial.print(outChr); // output should be 7 chars long, precision 3, so this would print 123.456
      }
      void loop() {  
      }
      Reply

      hans

  • Dec 14, 2015 - 3:39 PM - Ray Comment Link

    Thank you for good sharing!!!

    I could work these program.

    I want to connect  processing with this one.

    How could I do that?

    Reply

    Ray

    • Dec 14, 2015 - 4:09 PM - hans - Author: Comment Link

      Hi Ray!

      Thanks and you’re welcome! 

      I’m not sure what you mean with “I want to connect processing with this one.”?
      I’d like to help if I can, but maybe you could provide more info (if you think this might become a long topic; please consider using the Forum otherwise the comments become swamped with a lot of off-topic information).

      Reply

      hans

  • Dec 15, 2015 - 1:20 PM - Owen Luke Comment Link

    Hi there..

    Firstly thanks for taking the time to put together this detail. I have a question that I hope you can help with. when I run the code it reads the temperature as -43’c. I have tried 3 different sensor all with the same result. If heat the device the temperature raises inline with the heat source. I ahve check that I am not using parasitic power mode and have checked the code reflects that.

    This is driving me mad so any thoughts you have would really help.

    Cheers

    Owen

    Reply

    Owen Luke

    • Dec 15, 2015 - 1:47 PM - hans - Author: Comment Link

      Hi Owen,

      that indeed sounds like something that would annoy me too.

      I’d check a few things first:

      1) Start with one sensor
      2) Make sure you’re using the right pins on the sensor and Arduino
      3) If that doesn’t work; try a different pin on the Arduino (adapt code!)

      Is it always giving -43C?

      Reply

      hans

  • Dec 22, 2015 - 12:22 PM - Francisco Silva Comment Link

    Hello,

    I want to monitorize 6 sensors and show their temperature in a htlm page.

    I have all the code, but i just see the temperature of one sensor.

    Can you help me?

    Reply

    Francisco Silva

    • Dec 22, 2015 - 12:34 PM - Francisco Silva Comment Link

      See this forum post.

      Reply

      Francisco Silva

    • Dec 22, 2015 - 3:51 PM - hans - Author: Comment Link

      Hi Reply Francisco,

      I moved your question and code (due to it’s size) to the forum.
      I’m traveling at the moment so I might not be able to get to answering your question right away, but you’ll find it in the forum: this post

      Reply

      hans

      • Dec 23, 2015 - 6:02 AM - Francisco Silva Comment Link

        Hello Hans,

        Thanks for your aswer. 

        How can i get the adress of the 6 sensors?

        And put the temperature in each one correspondely, by a variable.

        Reply

        Francisco Silva

      • Dec 23, 2015 - 9:59 AM - hans - Author: Comment Link

        You’d only need to modify the code (see the forum post).
        If you’d prefer to see variables, then you’d need to work with passing a dynamic array to the function, making things a little cleaner but more complicated.

        Reply

        hans

  • Dec 26, 2015 - 4:55 AM - Henk Comment Link

    Beware of our Chinese friends .. sometimes the waterproof sensors come with a different default resolution.

    Setting it to 12 bit goes like:

    ds.reset(); // chinese sensor has default 9 bit resolution, set to 12 and store
    ds.select(addr);
    ds.write(0x4E);  //write to scratch
    ds.write(0x00); // low alarm, set to preferred value if needed
    ds.write(0x00); // high alarm
    ds.write(0x7F); // set to 12 bits resolution
    ds.reset();
    ds.select(addr); 
    ds.write(0x48,1);  // store config in eeprom
    delay(10); // extra delay needed for eeprom write

    After storing in eeprom 12 bit will be loaded as default. So the code is only needed once.

    Reply

    Henk

    • Dec 27, 2015 - 8:40 AM - hans - Author: Comment Link

      Hi Henk!

      Awesome info! Yeah our Chinese friends tend to screw around with imitation which sometimes miss the marker.
      This is good info! Great catch! 

      Did the sensor model you used have a different identification number/model?

      Reply

      hans

      • Dec 28, 2015 - 1:28 AM - Henk Comment Link

        Hi Hans,

        No, nothing special about them, just the unmarked waterproof version. I ordered 5 of them, and all had the same 9-bit setting.

        Our Chinese friends are very inventive, but they do produce a lot of fake items.
        Try the MAX7219 LED driver .. it has even a different pinning than the original.
        And the protection diodes they use on some modules introduce a 1 V drop. Add that to the 0,3-0,5 V
        a Nano introduces when powered from USB and instead of 5 V your module runs on 3,5 V. It than does either not run at all, or
        is very unstable.
        There are many more examples where modules/IC’s are not a drop-in replacement for the original.
        So IMO unless you understand electronics and are able to read a datasheet do not order from China.
        Things might not work as expected .. very frustrating if you just want to play with the Arduino environment.

        Regards, Henk

        Reply

        Henk

        • Dec 28, 2015 - 8:45 AM - hans - Author: Comment Link

          Yeah I know, that’s why I more and more opt for the “original” products.
          I have seen quite a few LED strands and Arduino boards as well, almost identical, but just not quite and enough to cause issues.

          Thanks again for the info Henk! 
          Have a happy New Year! 

          Reply

          hans

  • Jan 4, 2016 - 4:49 PM - Miro Comment Link

    Hi.
    How to do it, I know the address?
    28 88 84 82 5 0 0 6A
    I do not want her to retrieve

    Reply

    Miro

    • Jan 5, 2016 - 8:05 AM - hans - Author: Comment Link

      Hello Miro!

      I assume you mean that the sensor with serial number “28 88 84 82 5 0 0 6A” is connected, but you would not want it to be read?

      I’m not having access to any of my tools right now (I’m traveling), but it would be something like the following.

      To avoid reading it, you should capture the data from this section of code, into a string, say mySerialNumber (so modify the code here to create a string instead of printing it to serial):

      Serial.print("ROM =");
        for( i = 0; i < 8; i++) {
          Serial.write(' ');
          Serial.print(addr[i], HEX);
        }

      and put and if … then … around line 38  to 111.

      Something like 

      if (mySerialnumber!="28 88 84 82 5 0 0 6A") {   // just before line 38
      ....  // lines 38 - 111
      } // just after line 111
      Reply

      hans

      • Jan 5, 2016 - 10:04 AM - Miro Comment Link

        Hi.

        If you do not want.
        Something like that.
        http://henrysbench.capnfatz.com/henrys-bench/ds18b20-arduino-user-manual-introduction-and-contents/ds18b20-arduino-users-manual-part-3-reading-a-single-temperature/

        I’d like to do with this code and calling functions
        float temperatura (28 88 84 82 5 0 0 6A) {
        }

        DallasTemperature.h not good.
        Too slow.
        I am sorry.
        I am a beginner.

        Reply

        Miro

      • Jan 5, 2016 - 2:01 PM - hans - Author: Comment Link

        Oh now I see what you mean (don’t apologize for being a beginner, we all are or have been there as well ).

        I’d have to take some time to write something like that. Unfortunately, I’m traveling, so I’m a little limited in what I can do.
        I’d need my Arduino and Temp sensor to do some tests.

        Just in my head, untested, the code on the link you posted, pretty much does what you’re looking for:

        #include <OneWire.h>
        #include <DallasTemperature.h>
        // Create a Onewire Referenca and assign it to pin 10 on your Arduino
        OneWire oneWire(10);
        // declare as sensor referenec by passing oneWire reference to Dallas Temperature. 
        DallasTemperature sensors(&oneWire);
        // declare your device address
        // YOUR ADDRESS GOES HERE!!!!
        DeviceAddress tempSensor = {0x28, 0xFF, 0x2B, 0x45, 0x4C, 0x04, 0x00, 0x10};
        // A Variable to hold the temperature you retrieve
        float tempC;

        void setup(void)
        {
          // start serial port
          Serial.begin(9600);
          
          // set the resolution to 9 bit - Valid values are 9, 10, or 11 bit.
          sensors.setResolution(tempSensor, 9);
          // confirm that we set that resolution by asking the DS18B20 to repeat it back
          Serial.print("Sensor Resolution: ");
          Serial.println(sensors.getResolution(tempSensor), DEC); 
          Serial.println();
        }


        void loop(void)

          // Tell the Sensor to Measure and Remember the Temperature it Measured
         
          sensors.requestTemperaturesByAddress(tempSensor); // Send the command to get temperatures
         
          // Get the temperature that you told the sensor to measure
          tempC = sensors.getTempC(tempSensor);
          
          Serial.print("Temp C: ");
          Serial.print(tempC,4); // The four just increases the resolution that is printed
          Serial.print(" Temp F: ");
          // The Dallas Temperature Control Libray has a conversion function... we'll use it
          Serial.println(DallasTemperature::toFahrenheit(tempC),4);
          delay(1000);
        }

        The trick is to be found in this little piece of code:

          sensors.requestTemperaturesByAddress(tempSensor); // Send the command to get temperatures
          tempC = sensors.getTempC(tempSensor);

        The property call “DallasTemperature::toFahrenheit(tempC)” will convert it to Fahrenheit if needed.

        Reply

        hans

        • Aug 3, 2017 - 1:13 PM - GAry Comment Link

          Hi

          Great string of information and reply’s for a newbie like me to learn from. Appreciate your time.

          I have a string with 9 of the DS18B20 using +5v power (not parasite)
          I would like to use the Dallas library as above but want to assign the 9 sensor temp values (in Fahrenheit) to 9 unique integer variables (don’t need decimal accuracy) to pass to further code further down the Sketch. (controlling a relay board by temperature to run fans and furnaces)

          Thinking there is a way to do a counter loop to automatically create the variables and sequentially or by address assign the values. (something like kT1 to kT9). (Would be nice to be able to replace a bad sensor and the code would be able to update the serial # and keep working. Not sure can keep sensors in order and assigned to the right variable so it is in the same physical place on the string)

          Having all the sensors show up in the monitor is nice as well for troubleshooting.

          I have all the libraries and some code but having a problem getting all the software modules to work together. Any chance you could take the code above and suggest how to get it to work with the 9 sensors sent to unique variables?

          Many thanks

          GAry

          Reply

          GAry

          • Aug 12, 2017 - 4:07 PM - hans - Author: Comment Link

            Hi Gary,

            Let me see if I can help (sorry for the late response).

            The only way I can see this to work would be by storing the serial numbers so we can link them to your code or designated name for a given sensor.

            In line 32 you see how the serial number of a sensor is being read

              Serial.print("ROM =");
              for( i = 0; i < 8; i++) {
                Serial.write(' ');
                Serial.print(addr[i], HEX);
              }

            After a first run we could make an array, to define the serial numbers and the matching sensor names, something like this in the beginning of the code (before “void setup()” and fill in your own serial numbers and sensor names of course):

            char* SensorSerials[]={"28 88 84 82 5 0 0 6A", "28 88 84 82 5 0 0 6A", "28 88 84 82 5 0 0 6A", "28 88 84 82 5 0 0 6A", "28 88 84 82 5 0 0 6A","28 88 84 82 5 0 0 6A","28 88 84 82 5 0 0 6A","28 88 84 82 5 0 0 6A","28 88 84 82 5 0 0 6A"}; 
            
            char* SensorLabels[]={"Sensor1", "Sensor2", "Sensor3", "Sensor4", "Sensor5", "Sensor6", "Sensor7", "Sensor8", "Sensor9"};

            Now that part at line 32 that I just showed, should be modified as well, so that we catch the serial number in a variable. After that, compare the read information with the serials in the SensorSerials, so we can find which one is the right one. For whatever number in the array it matches, for example #4, we can now pull the name from the other array. Inn the example this could be Serial.print(SensorLabels[4]);.

            It will take a little reading up on for loops, arrays and strings, but feel free to ask if you need assistance with that – or even better: start a forum topic so we can post code examples and such (see the Arduino Forum).

            hans

  • Jan 6, 2016 - 1:11 AM - Miro Comment Link

    You do not use function DallasTemperature.
    DallasTemperature function is not good.
    Too slow.
    Only functions OneWire.

    Reply

    Miro

    • Jan 6, 2016 - 8:24 AM - hans - Author: Comment Link

      I have not been able to test it …

      If it’s too slow then one could use:

      fahrenheit = celsius * 1.8 + 32.0;
      Reply

      hans

  • Jan 22, 2016 - 11:47 PM - Martin Comment Link

    I’ve used a DS18B20 wired to a PL2303TA USB adapter and it works great, however

    the 4.7k resistor doesn’t go between Vdd and Data, but in between Vdd in on the DS18B20 and Vdd out from the PL2303TA.

    I’m wondering if that’s going to cause any issues,- it seems to work though.

    Reply

    Martin

    • Jan 23, 2016 - 4:21 AM - hans - Author: Comment Link

      Hi Martin,

      I have no experience with the PL2303TA … maybe one of the visitors/users of Tweaking4All can give you info.
      As far as I can see, if the readings you’re getting are right, then I probably wouldn’t worry too much about it.

      Reply

      hans

  • Feb 4, 2016 - 4:36 AM - chffh Comment Link

    integrate NI RTD and arduino for temp variation

    Reply

    chffh

    • Feb 4, 2016 - 4:56 AM - hans - Author: Comment Link

      As far as I know (for more info, see this link), an RTD is a resistance temperature detector, which means the resistance changes as temperature changes. This is obviously not done in the same way as described here. A good article on how to wire it to an Arduino and an example sketch can be found at OpenEnergyMonitor.org.

      Hope this helps?

      Reply

      hans

  • Feb 21, 2016 - 6:01 AM Comment Link
    PingBack: www.lenfancedeleau.org

    […] //www.tweaking4all.com/hardware/arduino/arduino-ds18b20-temperature-sensor/ […]

  • Mar 31, 2016 - 12:32 AM - ismail Comment Link

    hi..im new in arduino

    question : after i Go to “File” “Examples” “OneWire” and select the “DS18x20_Temperature” example, which will look like the code below. should i to verify or compile this onewire 

    Reply

    ismail

    • Mar 31, 2016 - 4:28 AM - hans - Author: Comment Link

      You can always try code. 

      I just looked and it looks a lot like the code I have – I think it will work just fine. 

      Reply

      hans

  • Apr 4, 2016 - 2:35 AM - ismail Comment Link

    what mean if on my serial monitor shown “no more addresses”

    Reply

    ismail

    • Apr 4, 2016 - 3:55 AM - hans - Author: Comment Link

      Hi Ismail,

      it means that the Arduino couldn’t find any more temperature sensors.
      The OneWire library (ds) will look for device addresses, and when it cannot find one, or is done with all the sensors, it will say just that.

      Reply

      hans

  • Apr 8, 2016 - 8:37 AM - Paul Comment Link

    It works great, thanks!

    Now I would like to use the data in this way:  it should start a fan that is controlled by a mosfet p30N06LE at a certain fixed speed

    How do I couple the data coming from the DS18B20 with the fanspeed sotware? Is this e.g by setting if Temperature >35 then set pin 3 255

    Or can you recommend a place where to find this

    Reply

    Paul

    • Apr 8, 2016 - 10:52 AM - hans - Author: Comment Link

      Hi Paul!

      Great to hear it works! 

      As for setting your fan speed through a pin, ehm … I’m afraid the Arduino does not allow analog output (see also PWM and AnalogWrite), then again, it might work with what they call PWM.

      As for “linking” temperature to fan speed;
      You could, if you figured out if AnalogWrite/PWM would work for you, use indeed an if statement, around line 108, something like:

      if(celsius>35) { 
        ...
      }

      or with multiple if’s (assuming you’re talking Celsius, otherwise use the variable “fahrenheit”):

      if(celsius>35) {
       analogWrite(3, 255); // fan speed highest
      } else if (celsius>30) {
       analogWrite(3, 200); // fan speed lower
      } else if (celsius>25) {
       analogWrite(3, 100); // fan speed even lower
      } else if (celsius>20) {
       analogWrite(3, 50); // fan speed slowest
      } else {
       analogWrite(3, 0); // fan OFF
      }
      Reply

      hans

      • May 15, 2016 - 12:44 PM - Paul Comment Link

        Hi Hans,

        thanks very much for your answer, it works now. I took a pretty low value in analogWrite (115) and a big capacitor to filter the noise of the PWM.

        Reply

        Paul

  • May 1, 2016 - 9:07 AM - Johan Comment Link

    Thanks for this article, i have a shield with a LCD monitor i want to display data from the sensor on. But only pin that is available for sensor is pin 13.

    Cant seem to get the sensor to work at pin 13 on my UNO. Can you please help me?

    Thanks

    Johan

    Reply

    Johan

    • May 2, 2016 - 5:03 AM - hans - Author: Comment Link

      Hi Johan,

      Did you try using pin 2 for the sensor?
      (or another free pin)

      Reply

      hans

      • May 2, 2016 - 5:34 AM - Johan Comment Link

        Pin 2 works but as i said only pin available is 13..

        Reply

        Johan

      • May 2, 2016 - 5:47 AM - hans - Author: Comment Link

        Oops, my bad, … 

        Pin 13 will not work for OneWire, since the “internal” LED is already connected to that one (source).
        Is there any way you can make another pin available? Ie. swapping them?
        Not sure what all the other pins are used for.

        Reply

        hans

  • May 16, 2016 - 9:42 AM - DaniM Comment Link

    Hi, im having troubles whit my sensor.. 

    in my serial monitor i got 

    No more addresses.

    No more addresses.

    No more addresses.

    No more addresses.

    Any sugestions ? TY

    Reply

    DaniM

    • May 18, 2016 - 4:17 PM - hans - Author: Comment Link

      Hi DaniM!

      By the look of it, this can be caused for 2 possible reasons;

      1) The wiring isn’t done correctly (Data vs Power) of the sensor,
      2) You’re having a Chinese “clone” sensor, which responds differently than the regular DS18B20, see for example this comment.

      Reply

      hans

  • May 30, 2016 - 6:42 PM - Richard Ribeiro Comment Link

    If I wanna used the i2c to show the temperature how I make this?

    Reply

    Richard Ribeiro

    • May 31, 2016 - 8:09 AM - hans - Author: Comment Link

      Hi Richard,

      honestly, I wouldn’t know,… not to say that it’s impossible, I just really wouldn’t know.
      Maybe one of the readers here can chime in if they know more details about using I2C for the sensor. 

      Reply

      hans

  • Sep 5, 2016 - 2:22 PM - mostafa Comment Link

    hi

    when I connect ds18b20 to arduino after the upload the program first arduino blink then off

    why????

    Reply

    mostafa

    • Sep 5, 2016 - 2:23 PM - hans - Author: Comment Link

      I’m sorry Mostafa, I’m not sure what you mean.
      You upload the sketch and after that you connect the sensor to the Arduino?

      Reply

      hans

  • Jan 4, 2017 - 8:27 PM - Marie Comment Link

    Can this code be compatible with Arduino 101? I keep receiving an error when compiling to the 101 board. 

    Reply

    Marie

    • Jan 5, 2017 - 8:53 AM - hans - Author: Comment Link

      Hi Marie,

      I have not had the opportunity to play with the Arduino 101, so I am not be able to confirm if the code should work or not.
      I do know the 101 uses a different processor (Intel vs ATMega) – but I would not expect this to be a problem.

      What is the error message you’re seeing?

      Reply

      hans

  • Jan 5, 2017 - 10:46 PM - Ed Symons Comment Link

    I imported the OneWire library from the .zip file per the instructions.  I try to compile the DS18x20_Temperature code and get the following result:

    collect2.exe: fatal error: cannot find ‘ld’

    compilation terminated.

    exit status 1

    Error compiling.

    Do you know what the problem could be?

    Reply

    Ed Symons

    • Jan 6, 2017 - 9:29 AM - hans - Author: Comment Link

      Hello Ed,

      this seem to be a problem seen by others as well – even though I never ran into this. I’m working on a Mac though.
      It seems to be related to the Linker (part of the compiling process) – see also this topic in the official Arduino Forums.

      I do not know if this is specific for a particular Windows version (XP) or not … but one user claims that this fixed the problem for him:

      I closed Arduino IDE then I deleted all the consoleXXXXXXX.tmp, buildXXXXXXX.tmp, and untitledXXXXXXX.tmp folders in

      c:\users\{username}\AppData\Local\Temp

      Restarted Arduino IDE and I was all good to go again

      Some users claim it’s Windows XP related, others have narrowed it down to the Arduino IDE version (check if you have the latest version).

      Hope this helps a little bit … please let us know if you found what fixed this.

      Reply

      hans

    • Jan 17, 2017 - 2:42 PM - Ed Symons Comment Link

      I already had 1.67 installed and was using that ok except for the problem described.  I tried installing the 1.67 version posted instead and that worked.  I am running Windows 10.

      Reply

      Ed Symons

  • Jan 16, 2017 - 6:38 AM - faz Comment Link

    hi may i know if i want to combine 2 temperature sensor which is LM35 and thermistor using arduino, how to do this because im so new in coding.All helps here are very appreciated.tqvm 

    Reply

    faz

    • Jan 16, 2017 - 8:42 AM - hans - Author: Comment Link

      Hi Faz,

      I have never worked with the LM35, but found this reference on the Arduino.cc website.
      Looks like reading one or two LM35’s shouldn’t be too hard.

      float tempC;
      int tempReading;
      int tempPin1 = 0; int tempPin2 = 1;
      void setup()
      {
      analogReference(INTERNAL);
      Serial.begin(9600);
      }
      void loop()
      { // Read analog pin 0
      tempReading = analogRead(tempPin1);
      tempC = tempReading / 9.31; Serial.print("Pin 0 = ");
      Serial.println(tempC); // Read analog pin 1 tempReading = analogRead(tempPin); tempC = tempReading / 9.31; Serial.print("Pin 1 = "); Serial.println(tempC); delay(1000); }

      Hope this helps – since I do not have an LM35, I was unable to test this.
      If you’d like to discuss the code, feel free to start a forum topic … 

      Reply

      hans

  • Jan 18, 2017 - 11:22 AM Comment Link
    PingBack: escapequotes.net

    […] Tweaking4All.com […]

  • Apr 8, 2017 - 12:20 AM - rahmat Comment Link

    why when i try in my serial monitor just appear ” no more addresses”?

    help me please…

    Reply

    rahmat

    • Apr 8, 2017 - 12:18 PM - hans - Author: Comment Link

      Hi Rahmat,

      could it be that your router cannot handle more clients?
      I have never seen this error, so I can only guess that the DNS of your router doesn’t have any IP addresses left available.

      Reply

      hans

  • Apr 8, 2017 - 10:21 AM - raffaele Comment Link

    I don’t now why but when I insert +5V  wire arduino uno go down!
    Short Circuit ?
    I don’t now, but wiring is correct and I Try 3 dsb120 (chinese) with some issue.
    Any idea?
    Raffaele

    Reply

    raffaele

    • Apr 8, 2017 - 12:20 PM - hans - Author: Comment Link

      Hi Raffaele,

      I assume you triple checked the wiring?
      When the Uno goes down I can imagine +5V shorting with GND which results in no more power = Uno shutdown.
      And your power supply will not like that too much.

      Reply

      hans

  • Aug 23, 2017 - 11:31 AM - Jens Comment Link

    So, I’ve been at this for a while now and have a very weird problem.

    I can read from one sensor fine, but once I try to use two sensors, only sensor 2 works correctly while sensor 1 displays 127.81 degrees (not -127).

    I have wired them up for normal power mode. Now here comes the weird part. If disconnect the 5V from sensor 2 (the one working correctly) suddenly both of them display the correct temperture.

    Since you wrote that parasite power mode can cause problems I dont know if I should leave it like that…and since one of them is still connect to 5V I dont know if it even is parasite mode or some weird hybrid.

    Reply

    Jens

    • Aug 26, 2017 - 10:16 AM - hans - Author: Comment Link

      Hi Jens,

      it sounds like you’re using a weird hybrid version of the parasite mode.
      I should not recommend this, but I’d use it like this for a while and see what happens. 
      Or rewire for Parasite and see if that makes a difference. Interesting finding though,.

      Reply

      hans



Your Comment …

Friendly request to not post large files here (like source codes, log files or config files). Please use the Forum for that purpose.

Please share:
*
*
Notify me about new comments (email).
       You can also use your RSS reader to track comments.


Tweaking4All uses the free Gravatar service for Avatar display.
Tweaking4All will never share your email address with others.