How to Use DS18B20 Temperature Sensor - Arduino Tutorial

104,357

320

17

Introduction: How to Use DS18B20 Temperature Sensor - Arduino Tutorial

About: Arduino Tutorials by Codebender.cc Team

In this tutorial you will learn how to use DS18B20 temp sensor with Arduino.

The temperature will be printed on the serial monitor of Codebender or Arduino IDE.

Presentation Video:

Before we start let's see more information about this sensor.

Step 1: About DS18B20 Sensor

The DS18B20 communicates over a 1-Wire bus that by definition requires only one data line (and ground) for communication with Arduino. It has an operating temperature range of -55°C to +125°C and is accurate to ±0.5°C over the range of -10°C to +85°C. In addition, the DS18B20 can derive power directly from the data line (“parasite power”), eliminating the need for an external power supply.

Each DS18B20 has a unique 64-bit serial code, which allows multiple DS18B20s to function on the same 1-Wire bus. Thus, it is simple to use one microprocessor to control many DS18B20s distributed over a large area. Applications that can benefit from this feature include HVAC environmental controls, temperature monitoring systems inside buildings, equipment, or machinery, and process monitoring and control systems.

Basic Features:

  • Unique 1-Wire Interface Requires Only One Port Pin for Communication
  • Each Device has a Unique 64-Bit Serial Code Stored in an On-Board ROM
  • Requires No External Components
  • Can Be Powered from Data Line; Power Supply Range is 3.0V to 5.5V
  • Measures Temperatures from -55°C to +125°C (-67°F to +257°F)
  • ±0.5°C Accuracy from -10°C to +85°C

Find more info here: datasheet

Step 2: What You Will Need

For this project you will need:

  • Arduino uno
  • Breadboard
  • DS18B20 temp sensor
  • 4.7K resistor

Step 3: The Circuit

The connections are pretty easy, see the image above with the breadboard circuit schematic.

  • Vcc & GND pins of sensor --> Arduino GND
  • Signal (middle pin) --> Arduino Pin 2
  • 4.7K Resistor between signal pin and 5V pin

Step 4: The Code

Here's the code, embedded using codebender!

  • Serial.println(sensors.getTempCByIndex(0)); returns temperature in Celsius scale
  • Serial.println(sensors.getTempFByIndex(0)); returns temperature in Fahrenheit scale

Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire! Try to add a second sensor and include the following command in your code: Serial.println(sensors.getTempFByIndex(1));

Try downloading the codebender plugin and clicking on the Run on Arduino button to program your Arduino with this sketch. And that's it, you've programmed your Arduino board! Press connect button to start serial communication with your Arduino board.

You can keep playing with that by clicking the "Edit" button and start making your own modifications to the code.

For example, try to change delay time and see how it changes the program. You can also try to connect more ds18b20 sensors!

Step 5: Well Done!

You have successfully completed one more Arduino "How to" tutorial and you learned how to use the DS18B20 temp sensor with Arduino.

Tip:

The DS18B20 sensor can be found also in waterproof version. (see image)

I hope you liked this, let me know in the comments.

There will be more of them, so make sure to click Follow button!

You can make your own Arduino Digital Thermometer for your desk with this unique instructable!

1 Person Made This Project!

Recommendations

  • Make it Glow Contest

    Make it Glow Contest
  • First Time Author Contest

    First Time Author Contest
  • PCB Challenge

    PCB Challenge

17 Discussions

0
paternostermariano1980
paternostermariano1980

Question 5 months ago on Step 5

Thank you. I realized your project for an art istallation project. It works very well. But I should integrate it smoothing the output data, so I need to get an average output every 1 msec of delay. The sensors has a min sampling frequency of 0.7 Hz. I have many codes for smoothing analog signals but the sensor work on digital pins. How can I get a smoothing? Consider that I will use the both pH and temp sensores. pH works on analogic pin. thx in advance

0
36000hz
36000hz

1 year ago

Thank you for posting this tutorial.
I spent all night trying to get a 2004A lcd to display the DS18B20's temperature correctly. I have not figured out what the issue was yet, but the characters on the display kept coming back corrupted. Finally this sketch worked! I am so very grateful. For now I am much too tired to continue to troubleshoot on it, so as soon as I get some rest I will get my information together so we can see if we can come up with the reason and a solution to this problem I had.

Until then... I am just so thrilled thrilled that I am finally on to the next step in this project, I could not wait to express my gratitude.
~Thank you.

0
MattB144
MattB144

3 years ago

mine prints out:

Pem`erApureis:-!27.

what have I done wrong?

0
Mar JuneG
Mar JuneG

Reply 3 years ago

i have the same problem

0
PaulNaude01
PaulNaude01

Reply 2 years ago

I had the same -127 error and tried all the suggestions posted here but in the end it was my signal wire that was not connected to the arduino (I used a development board pin which I though was connected. My suggestion: test all wires with a mulitmeter for connectivity from the sensor to the arduino pins.

0
SharifaS1
SharifaS1

Question 2 years ago on Step 5

why is the temperature sensor displaying -127 degree centigrade? wh could be the problem?
at

0
PaulNaude01
PaulNaude01

Answer 2 years ago

I had the same -127 error and tried all the suggestions posted here but in the end it was my signal wire that was not connected to the arduino (I used a development board pin which I though was connected. My suggestion: test all wires with a mulitmeter for connectivity from the sensor to the arduino pins.

0
SebastianH99
SebastianH99

Answer 2 years ago

You need to add a 5 K or 10 K resistor between the data and 5V lines

0
JagannathM2
JagannathM2

3 years ago

Can we use pin other than 2 for reading values?

0
ArnavS15
ArnavS15

3 years ago

Thank you worked!!!

0
GeeksTipsDotCom
GeeksTipsDotCom

4 years ago

One of the only real downside of this sensor is the fact that its not interchangeable because of its unique embedded silicon serial. If you want to change it with another one, you need to rescan the 1-Wire network and change the hexa address in your main code. The DHT22 instead is fully interchangeable and may be a good alternative. You can find here a comparison between DHT22 and DS18B20

0
Yvanle terrible
Yvanle terrible

4 years ago

interesting prez, guy,

You could add some infosinfo about "resolution", : Precision "9-10-11 or 12 bits" is ajustable on the DS18B20 (==> respected resolution then from 0.5 to 0.0625°C )

the more precision you want, the "longer" it take for a reading (0.1 sec to 1 sec approx - though, 1 sec reading is pretty fast anyway, i can not see a project that could note live with that !)

there is an IDE sketch (http://www.homautomation.org/2015/11/17/ds18b20-ho... for changing the precision through writing in the probe EEPROM, - so you have only to do it ONCE for all, it remembers after power loss

and, for signal smoothing, you might think of using Exponential Moving Average, This is doing a fine job for a compact code and limited memory ressource (http://playground.arduino.cc/Main/RunningAverage)

0
JoshuAc00k
JoshuAc00k

5 years ago on Step 5

Thank you for the simple tutorial. This made it very easy since before I didn't know there was a DallasTemperature library available. Thanks again!