Name: OneWireLibrary
Owner: Particle
Description: Provides support for the Dallas One Wire protocol on Particle devices.
Forked from: Hotaman/OneWireSpark
Created: 2016-10-27 21:39:39.0
Updated: 2018-03-27 00:53:14.0
Pushed: 2018-01-03 18:36:22.0
Size: 45
Language: C++
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
The One Wire (1-Wire) protocol is used in the popular DS18B20 temperature sensor and other devices from Maxim (formerly Dallas).
This library implements the Dallas One Wire (1-wire) protocol on the Particle Photon, Electron, Core, P0/P1, Red Bear Duo and compatible devices.
If you are using a DS18B20, DS1820 or DS1822 temperature sensor, you can simply use the DS18
object to read temperature.
Connect sensor:
D0
(or another pin) with a 2K-10K resistor to pin 3.lude "DS18.h"
sensor(D0);
loop() {
(sensor.read()) {
Particle.publish("temperature", String(sensor.celsius()), PRIVATE);
If you use another chip or you want to customize the behavior you can copy-paste one of the examples and modify it.
DS18
sensor(pin);
sensor(pin, parasitic);
Create an object to interact with one or more DS18x20 sensors connected to pin
(D0-D7, A0-A7, etc).
If parasitic
is true
, the power will be maintained at the end of a conversion so the sensor can parasitically draw power from the data pin. This mode is discourage since it can be harder to set up correctly. The value of the pull-up resistor is important in parasitic mode. See the references.
Save yourself some trouble, buy some DS18B20 (not DS18B20-PAR) and use the 3 pin powered mode.
read()
bool succes = sensor.read();
Search for the next temperature sensor on the bus and start a conversion. Return true
when the temperature is valid.
The default conversion time is 1 second.
Since it performs a 1-Wire search each time if you only have 1 sensor it's normal for this function to return false
every other time.
If you have more than 1 sensor, check addr()
to see which sensor was just read.
bool succes = sensor.read(addr);
Read a specific sensor, skiping the search. You could set up your code to read()
once in setup()
, save the addr
and in loop
always read this sensor only.
celsius()
fahrenheit()
t temperature = sensor.celsius();
t temperature = sensor.fahrenheit();
Return the temperature of the last read device. Only call after read()
returns true
.
Note: on the DS18B20, 85 C is returned when there's a wiring issue, possibly not enough current to convert the temperature in parasitic mode. Check the pull up value.
searchDone()
bool done = sensor.searchDone();
If read()
returns false
, check searchDone()
. If true
, this is not an error case. The next read()
will start from the first temperature sensor again.
crcError()
bool error = sensor.crcError();
Returns true
when bad data was received. ¯\(?)/¯
addr()
8_t addr[8];
or.addr(addr);
Copies the 1-Wire ROM data / address of the last read device in the buffer. All zeros if no device was found or search is done.
See the datasheet for your device to decode this.
type()
DS18Type type = sensor.type();
The type of the last read device. One of WIRE_DS1820
, WIRE_DS18B20
, WIRE_DS1822
, WIRE_DS2438
or WIRE_UNKNOWN
if the device is not a temperature sensor, no device was found or the search is done.
raw()
int16_t value = sensor.raw();
Integer value of the temperature without scaling. Useful if you want to do integer math on the temperature. The scaling between the raw value and physical value depends on the sensor.
data()
8_t data[9];
or.data(data);
Copies the 1-Wire scratchpad RAM / data of the last read device in the buffer. All zeros if there was a CRC error in the address search, no device was fuond or search is done.
See the datasheet for your device to decode this.
setConversionTime
sensor.setConversionTime(milliseconds);
This library pauses for 1000 milliseconds while waiting for the temperature conversion to take place. Check the datasheet before reducing this value.
OneWire
OneWire
, the 1-Wire protocol implementation used by the DS18
object, is documented in its header file.
Copyright 2016 Hotaman, Julien, Vanier, and many contributors (see individual files)
Licensed under the MIT license.