Introduction: Temperature With DS18B20

Picture of 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

Picture of 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

Picture of 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

Picture of Inventory

1st step is to start the sensors:
sensors.begin();

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

3rd step is to request the temperatures from the devices:
sensors.requestTemperatures();

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

Comments

MulaD2 (author)2017-02-10

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

please give me details of the pins

MulaD2 (author)MulaD22017-02-10

sorry its ds18b20

SundarB (author)2016-03-28

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!

diy_bloke (author)SundarB2016-12-29

/sketchbook/libraries

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"

sisal1 (author)2016-03-29

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.

mosvath (author)sisal12016-03-29

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.

https://mega.nz/#!goIjiBYZ!9n3JgJIbjBawqDzXGTLCMYved5HXWn4z8Y1UaZGmswI

BR,
Mark

MadScott (author)2015-02-25

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!

JohnB389 (author)MadScott2016-01-10

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.

fstone35 (author)JohnB3892016-03-28

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."

herrold (author)2014-01-31

Hi,

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.

Thanks.

mosvath (author)herrold2014-02-01

Hi

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);

Regards,
Mark

herrold (author)mosvath2014-02-01

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?

herrold (author)herrold2014-02-05

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...

happyh0ur (author)2014-01-06

I found a link:
https://github.com/opb/DS18B20-Arduino-Library

mosvath (author)happyh0ur2014-01-06

Hi

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

http://oscomputer.hu/download/arduino/DallasTemperature_OneWire_for_Arduino_1.0.4.zip

an older version:
http://oscomputer.hu/download/arduino/DallasTemperature_350.zip

Regards,
Mark

happyh0ur (author)mosvath2014-01-11

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.

mosvath (author)happyh0ur2014-01-11

Hello

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

regards,
Mark

happyh0ur (author)mosvath2014-01-21

Mark,

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

mosvath (author)happyh0ur2014-01-21

I can still recommend you using Arduino 1.0.4.

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

mosvath (author)happyh0ur2014-01-11

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);

delay(1);

digitalWrite(13, LOW);

}

void scanSensors(void)

{

byte i;

byte present = 0;

byte data[12];

byte addr[8];

while (ds.search(addr))

{

Serial.print("R=");

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");

return;

}

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

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

maxsensors++;

}

else {

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

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

maxsensors++;

}

else {

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

Serial.print("Device ID: ");

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

Serial.println();

return;

}

}

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

ds.reset();

ds.select(addr);

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.select(addr);

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

Serial.print("P=");

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();

flashled();

}

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

ds.reset_search();

delay(250);

}

void setup(void) {

Serial.begin(9600);

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;

sensors.begin();

scanSensors();

}

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++)

{

flashled();

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

Serial.print("Sensor ");

Serial.print(i,DEC);

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

}

}

happyh0ur (author)2014-01-06

The Dallas Temperature library link shows to be offline on the download.milesburton.com site. I have looked at a few sites and they all seem to point to this broken link. Is there any other avenue to access this library?

aslan0490 (author)2012-02-21

can this project be used for different types of sensors like temp,press,humidity etc..?

mosvath (author)aslan04902012-02-22

Yes, sure. As it is based on Arduino, many sensors can be used easily. You can start here: http://arduino.cc/playground/Main/InterfacingWithHardware#presssens