OneWire is currently maintained by Paul Stoffregen. If you find a bug or have an improvement (to the library), email paul at pjrc dot com. Please be sure you are using the latest version of OneWire.
(This page still needs some more generalisation to 1-Wire, as some code is specific to "using the DS1820" and a specifc LCD. It also needs a cleaning of the library paragraph that includes references to older versions of libraries.)
On a 1-Wire network, which Dallas has dubbed a "MicroLan" (trademark), a single "master" device communicates with one or more 1-Wire "slave" devices over a single data line, which can also be used to provide power to the slave devices. (Devices drawing power from the 1-wire bus are said to be operating in parasitic power mode.) http://sheepdogguides.com/arduino/asw1onew1.htm Tom Boyd's guide to 1-Wire may tell you more than you want to know... but it may also answer questions and inspire interest.
The 1-wire temperature sensors have become particularly popular, because they're inexpensive and easy to use, providing calibrated digital temperature readings directly. They are more tolerant of long wires between sensor and Arduino. The sample code below demonstrates how to interface with a 1-wire device using Jim Studt's OneWire Arduino library, with the DS18S20 digital thermometer as an example. Many 1-Wire chips can operate in both parasitic and normal power modes.
MicroLans can be accessed directly by an Arduino, using the mature Arduino OneWire library. Alternatively, they can be accessed through an interface which costs a little money, but reduces the workload inside the Arduino. The interface cost $8 in kit form at 2/2010. There is a guide to using it from an independent source.
The chip can be powered two ways. One (the "parasitic" option) means that only two wires need go to the chip. The other may, in some cases, give more reliable operation (parasitic often works well), as an extra wire carrying the power for the chip is involved. For getting started, especially if your chip is within 20 feet of your Arduino, the parasitic option is probably fine. The code below works for either option, anyway.
Parasite power mode
When operating in parasite power mode, only two wires are required: one data wire, and ground. At the master, a 4.7k pull-up resistor must be connected to the 1-wire bus. When the line is in a "high" state, the device pulls current to charge an internal capacitor.
This current is usually very small, but may go as high as 1.5 mA when doing a temperature conversion or writing EEPROM. When a slave device is performing one these operations, the bus master must keep the bus pulled high to provide power until the operation completes; a delay of 750ms is required for a DS18S20 temperature conversion. The master can't do anything during this time, like issuing commands to other devices, or polling for the slave's operation to be completed. To support this, the OneWire library makes it possible to have the bus held high after the data is written.
Normal (external supply) mode
With an external supply, three wires are required: the bus wire, ground, and power. The 4.7k pull-up resistor is still required on the bus wire. As the bus is free for data transfer, the microcontroller can continually poll the state of a device doing a conversion. This way, a conversion request can finish as soon as the device reports being done, as opposed to having to wait 750ms in "parasite" power mode.
Note on resistors:
For larger networks, you can try smaller resistors.
The ATmega328/168 datasheet indicates starting at 1k6 and a number of users have found smaller to work better on larger networks.
Each 1-Wire device contains a unique 64-bit 'rom' code, consisting of an 8-bit family code, a 48-bit serial number, and an 8-bit CRC. The CRC is used to verify the integrity of the data. For example, the sample code, below, checks if the device being addressed is a DS18S20 temperature sensor by checking for its family code, 0x10. To use the sample code with the newer DS18B20 sensor, you'd check for a family code of 0x28, instead, and for the DS1822 you'd check for 0x22.
Before sending a command to a slave device, the master must first select that device using its rom code. Alternatively, you can address a command to all slave devices by issuing a 'skip rom' command (0x, instead. This is only safe if you are sure there is only one slave device on the MicroLAN. for commands that don't elicit a response from the slave devices - data collision will occur if data is requested from more than one slave.
Reading a 1Wire device requires multiple steps. The details are device-dependent, in that devices are capable of reporting different measurables. The popular DS18B20, for example, reads and reports temperature, while a DS2438 reads voltage, current, and temperature.
The two main steps of a read process are:
A command is issued to the device to perform an internal conversion operation. With a DS18B20, this is the Ox44 byte command. In the OneWire library, this is issued as ds.write(0x44), where ds is an instance of the OneWire class. After this command is issued, the device reads the internal ADC,and when this process is complete, it copies the data into the Scratchpad registers. This length of this conversion process varies depending on the resolution and is listed in the device datasheet. a DS18B20 takes from 94 (9-bit resolution) to 750ms (12-bit resolution) to convert temperature (c.f. DS18B20 Datasheet). While the conversion is taking place, the device may be polled, e.g. using in the ds.read() command in OneWire, to see if it has successfully performed a conversion.
Once the data has been converted, it is copied into the Scratchpad memory,where it may be read. Note that the Scratchpad may be read at any time without a conversion command to recall the most previous reading, as well as the resolution of the device and other device-dependent configuration options.
Asynchronous vs. Synchronous read/write
The majority of existing code for 1Wire devices, particularly that written for Arduino, uses a very basic "Convert, Wait, Read" algorithm, even for multiple devices. This creates several problems:
Program timing for other functions
Arguably the biggest problem with using the above methodology is that unless threading measures are undertaken, the device must sit (hang) and wait for the conversion to take place if a hardcoded wait time is included. This presents a serious problem if other timed processes exist, and even if they don't -- many programs wait for user input, process data, and perform many other functions that cannot be put on hold for the time necessary for a temperature conversion process. As noted above and below, a 12-bit conversion process for a DS18B20 can take as long as 750ms. There is simply no reason to use this method except laziness. One can issue a conversion command and return later to pick up the measurement with a Read Scratchpad command.
Scaling for Poll Speed with multiple devices
Another major problem with the "Convert, Wait, Read" method is that it scales very poorly, and for no good reason. All conversion commands can be issued in series (or simultaneously, by issuing a Skip ROM and then Convert command), and the result can then be read back in succession. See discussion here: http://interfaceinnovations.org/onewireoptimization.html
Adjustment of wait time to required conversion time
This factor is really just an artifact of inefficient coding for 1Wire read processes. Typically, at best code is written with the maximum required conversion time listed in the data sheet, and often with a GENEROUS overhead on top of this. In the example below, for example, 1000ms is given, while the datasheet lists 750ms as the maximum conversion time, and typical conversion takes please is 625ms or less. Furthermore, the value should be adjusted for the resolution that is currently being polled. A 9-bit conversion, for example, will take 94ms or less, and waiting for 1000ms simply doesn't make sense. As noted above, the most efficient way to poll is the use a read time slot to poll the device. In this fashion one can know exactly when the result is ready and pick it up immediately.
For discussion and code examples on this topic, please see: http://www.cupidcontrols.com/2014/10/moteino-arduino-and-1wire-optimize-your-read-for-speed/
In 2007, Jim Studt created the original OneWire library that makes it easy to work with 1-Wire devices. The forum thread describes the evolution. Jim's original version only worked with arduino-007 and required a large (256-byte) lookup table to perform CRC calculations. This was later updated to work with arduino-0008 and later releases. The most recent version (unavailable at this moment?) eliminates the CRC lookup table; it has been tested under arduino-0010.
Version 2.0 merges Robin James's improved search function and includes Paul Stoffregen's improved I/O routines (fixes occasional communication errors), and also has several small optimizations.
Version 2.1 adds compatibility with Arduino 1.0-beta and an improved temperature example (Paul Stoffregen), DS250x PROM example (Guillermo Lovato), chipKit compatibility (Jason Dangel), CRC16, convenience functions and DS2408 example (Glenn Trewitt).
Miles Burton derived its Dallas Temperature Control Library from it as well.
For more compact version of the code above, as well as for description of sensor's command interface look here.
In order to convert the HEX code to a temperature value, first you need to identify if you are using a DS18S20, or DS18B20 series sensor. The code to read the temperature needs to be slightly different for the DS18B20 (and DS1822), because it returns a 12-bit temperature value (0.0625 deg precision), while the DS18S20 and DS1820 return 9-bit values (0.5 deg precision).
First off, you need to define some variables, (put right under loop() above)
Then for a DS18B20 series you will add the following code below the
This block of code converts the temperature to deg C and prints it to the Serial output.
Above example works only for the B-type of the DS1820. Here is a code example that works with the lower resolution DS1820 and with multiple sensors diplaying their values on a LCD. Example is working with Arduino pin 9. Feel free to change that to an appropriate pin for your use. Pin 1 and 3 of the DS1820 has to be put to ground! In the example a 5k resistor is put from pin 2 of DS1820 to Vcc (+5V). See LiquidCrystal documentation for connecting the LCD to the Arduino.