First I am a great fan of the Dallas Temperature Control Library (DTCL) of Miles Burton, however some time ago I needed to strip it down to save a few dozen bytes.
Today I reworked that minimalistic version into a class called DS18B20. I added some examples to help people get started. The library is published on https://github.com/RobTillaart/Arduino/tree/master/libraries/DS18B20
The DS18B20 library supports only the DS18B20, only one sensor per pin, no parasite mode, no Fahrenheit and no alarm function. The only feature the class supports is the asynchrone way of working by means of the three functions:
requestTemperatures() --> isConversionAvailable() --> readTempC()
This allowed the class to be both minimal in size and still be non-blocking. Furthermore it has a begin() and a setResolution() function.
The DS18B20_simple.ino sketch (see below) which is quite similar to the DTCL simple.ino example, compiles about 1KB smaller using the IDE 1.8.1. for an Arduino Uno.
As the async mode is faster I added an example to see the timing to get a read compared to the "proposed delay times". In my testruns I saw 10% to almost 20% shorter times depending on resolution (this may depend on sensor used and/or temperature, not tested extensively)
As always remarks and comments are welcome,
Rob
//
// FILE: DS18B20_simple.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.0.1
// PURPOSE: equivalent of DallasTemperature library Simple
//
// HISTORY:
// 0.0.1 = 2017-07-25 initial version
#include <OneWire.h>
#include <DS18B20.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DS18B20 sensor(&oneWire);
void setup(void)
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("DS18B20 Library version: ");
Serial.println(DS18B20_LIB_VERSION);
sensor.begin();
}
void loop(void)
{
sensor.requestTemperatures();
while (!sensor.isConversionComplete()); // wait until sensor is ready
Serial.print("Temp: ");
Serial.println(sensor.getTempC());
}