Step 4: Source code

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 .

<p>In parasitic mode, you need to connect VDD to Ground. Additionally, <br>you may need to replace the resistor as with 4.7k&Omega; the sensor may not <br>get enough power to run at all.<br>On a 10 meter cable run with <br>parasitic power, I get reliable results when resistor is less than <br>2k&Omega;. With 4.7k&Omega;, I got over 95% read fails. With 2.3k&Omega; I got around 18% <br>fails. With 1.9k&Omega; I get 1.7% fails.</p>

Thanks for the tip. It can be useful in the future for everyone.<br><br>I have no problem with a 20 cm long cable for years.<br>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.<br><br>https://mega.nz/#!goIjiBYZ!9n3JgJIbjBawqDzXGTLCMYved5HXWn4z8Y1UaZGmswI<br><br>BR,<br>Mark

<p>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!</p>

<p>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 &quot;charge&quot; 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.</p>

<p>Here's a quote from the datasheet for the DS18B20: &quot;When the DS18B20 is used in parasite power mode, the VDD pin must be connected to ground.&quot;</p>

<p>I am facing some problem here.</p><p>1) Where should those library files be actually placed? ../libraries or some home folder where I keep my source codes?</p><p>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. </p><p>#include Nested deeply</p><p>I am at my wit's end. Please help!</p>

<p>Hi, </p><p>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. </p><p>Can anyone help with some code to create or access these variables.</p><p>Thanks.</p>

Hi<br><br>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);<br><br>Regards,<br>Mark

<p>Thanks Mark, </p><p>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? </p><p>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 &quot;WConstants.h: Nosuch file or directory&quot; and a bunch of variables that &quot;was not declared in this scope&quot; like digitalPinToBitMask and delayMicroseconds etc. The errors from your sketch are pointing to the LCD4bit_mod library file location and also mention the &quot;WConstants.h: Nosuch file or directory&quot; and a bunch of variables that &quot;was not declared in this scope&quot;. </p><p>Is there likely to be a simple fix for this?</p>

<p>Well I can now see I was a little off the mark. I have found that the individual readings can be accessed by</p><p> float temp1 = sensors.getTempCByIndex(0);</p><p> float temp2 = sensors.getTempCByIndex(1); etc....</p><p>The WConstants.h error can be resolved by removing the #include &quot;WConstants.h&quot; from the .cpp file and adding #include &quot;Arduino.h&quot; to the .h file as per <a href="https://sites.google.com/site/kingauswebpage/my-projects/cannot-find-wconstants-h" rel="nofollow">https://sites.google.com/site/kingauswebpage/my-pr...</a></p>

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

Hi<br><br>That's cool. I found it in my project folder too :)<br>if you need it for some reasons:<br><br>http://oscomputer.hu/download/arduino/DallasTemperature_OneWire_for_Arduino_1.0.4.zip<br><br>an older version:<br>http://oscomputer.hu/download/arduino/DallasTemperature_350.zip<br><br>Regards,<br>Mark

<p>Thanks Mark,</p><p>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. </p>

Hello<br><br>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<br><br>regards,<br>Mark

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

<p>I can still recommend you using Arduino 1.0.4.</p><p>That version is working and can be compiled. See attached image.</p>

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?

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

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