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:
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
25 Comments
6 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.
Reply 1 year ago
Watch what you're saying: "you need to connect VDD to Ground."!!! :-) That can lead someone to short-circuit Arduino's power lines! It's more like: "Connect pins 1 and 3 of the DS18B20 together and to Arduino Ground and don't connect Arduino's VDD line to the chip, at all."
Reply 1 year ago
https://cdn.sparkfun.com/datasheets/Sensors/Temp/D...
Page 2, 3
Optional VDD pin. See “Parasite Power” section for
details of connection. VDD must be grounded for
operation in parasite power mode.
I didn't use VDD pin in my project. So yes, be careful.
Reply 6 years ago
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
5 years ago
my ds18b29 sensor has only two wires so howto interface with it.
please give me details of the pins
Reply 5 years ago
sorry its ds18b20
6 years ago
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!
Reply 5 years ago
/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"
7 years ago on Introduction
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!
Reply 6 years ago
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.
Reply 6 years ago
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."
8 years ago on Introduction
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.
Reply 8 years ago on Introduction
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
Reply 8 years ago on Introduction
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?
Reply 8 years ago on Introduction
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...
8 years ago on Introduction
I found a link:
https://github.com/opb/DS18B20-Arduino-Library
Reply 8 years ago on Introduction
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
Reply 8 years ago on Introduction
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.
Reply 8 years ago on Introduction
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
Reply 8 years ago on Introduction
Mark,
Thanks for the code. I get another error now, see attached, and am using it for a sous vide application.