0.2.2 DS18B20_INT

This commit is contained in:
rob tillaart 2023-02-04 13:31:19 +01:00
parent 1e993d995f
commit 7c34f6a280
20 changed files with 408 additions and 67 deletions

View File

@ -6,7 +6,7 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: arduino/arduino-lint-action@v1
with:
library-manager: update

View File

@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6

View File

@ -10,7 +10,7 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: json-syntax-check
uses: limitusus/json-syntax-check@v1
with:

View File

@ -6,11 +6,22 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.2.2] - 2023-02-03
- improve disconnect detection
- add **isConnected(retries)**
- change signature **bool setResolution(bits)** to see if device is still available.
- update GitHub actions
- update license 2023
- add example DS18B20_performance.ino (start not complete)
- add example DS18B20_two_sensor.ino
- add example oneWireSearch.ino
- update readme.md
## [0.2.1] - 2022-11-02
- add changelog.md
- add rp2040 to build-CI
## [0.2.0] - 2022-06-23
- fix #10 getTempCentiC() (thanks negroKiordi)
- fix reading sensor

View File

@ -1,13 +1,11 @@
//
// FILE: DS18B20_INT.cpp
// AUTHOR: Rob.Tillaart@gmail.com
// VERSION: 0.2.1
// VERSION: 0.2.2
// DATE: 2017-07-25
// PUPROSE: library for DS18B20 temperature sensor - integer only.
// URL: https://github.com/RobTillaart/DS18B20_INT
// https://github.com/RobTillaart/DS18B20_RT
//
// HISTORY: see changelog.md
#include "DS18B20_INT.h"
@ -35,6 +33,17 @@ DS18B20_INT::DS18B20_INT(OneWire* ow)
bool DS18B20_INT::begin(uint8_t retries)
{
isConnected(retries);
if (_addressFound)
{
_setResolution();
}
return _addressFound;
}
bool DS18B20_INT::isConnected(uint8_t retries)
{
_addressFound = false;
for (uint8_t rtr = retries; (rtr > 0) && (_addressFound == false); rtr--)
@ -46,18 +55,6 @@ bool DS18B20_INT::begin(uint8_t retries)
_addressFound = _deviceAddress[0] != 0x00 &&
_oneWire->crc8(_deviceAddress, 7) == _deviceAddress[7];
}
if (_addressFound)
{
_oneWire->reset();
_oneWire->select(_deviceAddress);
_oneWire->write(WRITESCRATCH);
// two dummy values for LOW & HIGH ALARM
_oneWire->write(0);
_oneWire->write(100);
_oneWire->write(_resolution); // lowest as we do only integer math.
_oneWire->reset();
}
return _addressFound;
}
@ -78,9 +75,16 @@ bool DS18B20_INT::isConversionComplete(void)
int16_t DS18B20_INT::getTempC(void)
{
if (isConnected(3) == false)
{
return DEVICE_DISCONNECTED;
}
int16_t rawTemperature = _readRaw();
rawTemperature >>= 4;
if (rawTemperature < -55) return DEVICE_DISCONNECTED;
if (rawTemperature < -55)
{
return DEVICE_DISCONNECTED;
}
return rawTemperature;
}
@ -98,21 +102,16 @@ bool DS18B20_INT::getAddress(uint8_t* buf)
}
void DS18B20_INT::setResolution(uint8_t bits)
bool DS18B20_INT::setResolution(uint8_t bits)
{
uint8_t newRes = 0;
switch (bits)
{
case 12: newRes = TEMP_12_BIT; break;
case 11: newRes = TEMP_11_BIT; break;
case 10: newRes = TEMP_10_BIT; break;
default: newRes = TEMP_9_BIT; break;
}
if (newRes != _resolution)
{
_resolution = newRes;
begin();
case 12: _resolution = TEMP_12_BIT; break;
case 11: _resolution = TEMP_11_BIT; break;
case 10: _resolution = TEMP_10_BIT; break;
default: _resolution = TEMP_9_BIT; break;
}
return begin();
}
@ -131,12 +130,19 @@ uint8_t DS18B20_INT::getResolution()
int16_t DS18B20_INT::getTempCentiC(void)
{
if (isConnected(3) == false)
{
return DEVICE_DISCONNECTED;
}
int16_t rawTemperature = _readRaw();
// rawTemperature = rawTemperature * 100 / 16;
rawTemperature *= 25;
rawTemperature >>= 2;
// use at own risk.
// if (rawTemperature < -5500) return DEVICE_DISCONNECTED * 100;
// use at own risk. (not tested)
if (rawTemperature < -5500)
{
return DEVICE_DISCONNECTED * 100;
}
return rawTemperature;
}
@ -157,6 +163,22 @@ int16_t DS18B20_INT::_readRaw(void)
}
bool DS18B20_INT::_setResolution()
{
if (_addressFound)
{
_oneWire->reset();
_oneWire->select(_deviceAddress);
_oneWire->write(WRITESCRATCH);
// two dummy values for LOW & HIGH ALARM
_oneWire->write(0);
_oneWire->write(100);
// lowest as default as we do only integer math.
_oneWire->write(_resolution);
_oneWire->reset();
}
return _addressFound;
}
// -- END OF FILE --

View File

@ -2,7 +2,7 @@
//
// FILE: DS18B20_INT.h
// AUTHOR: Rob.Tillaart@gmail.com
// VERSION: 0.2.1
// VERSION: 0.2.2
// DATE: 2017-07-25
// PUPROSE: Minimalistic library for DS18B20 temperature sensor
// uses only integer math (no float to minimize footprint)
@ -21,7 +21,7 @@
//
#define DS18B20_INT_LIB_VERSION (F("0.2.1"))
#define DS18B20_INT_LIB_VERSION (F("0.2.2"))
#include "Arduino.h"
#include "OneWire.h"
@ -37,13 +37,16 @@ class DS18B20_INT
public:
explicit DS18B20_INT(OneWire * ow);
bool begin(uint8_t retries = 3);
bool isConnected(uint8_t retries = 3);
void requestTemperatures(void);
int16_t getTempC(void);
bool isConversionComplete(void);
bool getAddress(uint8_t* buf);
void setResolution(uint8_t bits = 9);
uint8_t getResolution();
bool setResolution(uint8_t bits = 9);
uint8_t getResolution(); // returns cached value
int16_t getTempCentiC(void);
@ -54,6 +57,7 @@ private:
uint8_t _resolution;
int16_t _readRaw();
bool _setResolution();
};

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2017-2022 Rob Tillaart
Copyright (c) 2017-2023 Rob Tillaart
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -24,18 +24,31 @@ I'm a great fan of the above library however some time ago I needed to strip it
to save a few dozen bytes. I reworked that minimalistic version into a library and I
added a number of Arduino examples to help you get started.
This library is also related to - https://github.com/RobTillaart/DS18B20_RT
#### Related
This library is related to
- https://github.com/RobTillaart/DS18B20_INT
- https://github.com/RobTillaart/DS18B20_RT
- https://github.com/milesburton/Arduino-Temperature-Control-Library
## Interface
```cpp
#include "DS18B20_INT.h"
```
#### Core
This DS18B20_INT library supports only the DS18B20, only one sensor per pin, no parasite
mode, no Fahrenheit and no alarm functions. The only feature the class supports is
the asynchronous reading of the temperature by means of three core functions:
- **DS18B20_INT(OneWire \*)** constructor needs a reference to OneWire object.
- **bool begin(uint8_t retries = 3)** resets oneWire and set resolution to 9 bit.
returns true if all is OK. there will be a number of retries to connect, default 3.
- **bool begin(uint8_t retries = 3)** resets oneWire and set resolution default to 9 bit.
Returns true if address / device is found and all is OK.
There will be a number of retries to connect, default 3.
- **bool isConnected()** Returns true if address / device is found.
- **void requestTemperatures()** trigger temperature conversion.
- **bool isConversionComplete()** check if conversion is complete.
- **int16_t getTempC()** returns temperature in whole degrees only. -55..125
@ -43,20 +56,22 @@ or -127 = DEVICE_DISCONNECTED
- **bool getAddress()** returns true if the sensor is configured (available).
### CentiC part
#### CentiC part
The following functions are experimental since 0.2.0
They allow to use a higher resolution while not using floats.
This keeps the library small.
- **void setResolution(uint8_t bits = 9)** sets the internal resolution to 9, 10, 11 or 12 bits.
- **bool setResolution(uint8_t bits = 9)** sets the internal resolution to 9, 10, 11 or 12 bits.
Other numbers will be mapped on 9.
This will affect the conversion time for a measurement.
Internally it will call **begin()** to set the new resolution if needed.
Internally it will call **begin()** to set the new resolution.
Returns false if no address / device can be found.
- **void getResolution()** returns the bits set, default 9.
Convenience function.
- **getTempCentiC(void)** returns the measured temperature times 100. -5500..12500
So 10.62°C will be returned as 1062.
Note one might need to set the resolution.
**Warning** The DEVICE_DISCONNECTED is not tested for, but is commented in the code.
Use at own risk.
@ -75,23 +90,23 @@ This library supports only one DS18B20 per Arduino/ MCU pin.
// | o | 2 DATA
// \ o | 3 VCC
// \---+
```
(always check datasheet)
Connect a pull-up resistor 4.7 KOhm between pin3 and pin2.
When the wires are longer this resistor needs to be smaller.
### Pull up resistor
#### Pull up resistor
An **indicative** table for pull up resistors, (E12 series), to get started.
Note: thicker wires require smaller resistors (typically 1 step in E12 series)
| Length | - 5.0 Volt | - 3.3 Volt |
|--------------:|------------:|-----------:|
| 10cm (4") | 10K0 | 6K8 |
| Length | 5.0 Volt | 3.3 Volt | Notes |
|----------------:|------------:|-----------:|:--------|
| 10cm (4") | 10K0 | 6K8 | might work without |
| 20cm (8") | 8K2 | 4K7 |
| 50cm (20") | 4K7 | 3K3 |
| 100cm (3'4") | 3K3 | 2K2 |
@ -99,7 +114,23 @@ Note: thicker wires require smaller resistors (typically 1 step in E12 series)
| 500cm (16'8") | 1K0 | \* |
| longer | \* | \* |
\* = no info, smaller
\* = no info, smaller?
## Performance
To elaborate connected state.
Tested on UNO, 16 MHz, time in microseconds.
Measured with DS18B20_performance.ino
| function | not connected | connected |
|:-----------------------|:---------------:|:-----------:|
| begin | 920 | - |
| getAddress | 4 | - |
| requestTemperatures | 1276 | - |
| isConversionComplete | 72 | - |
| getTempC | 912 | - |
## Credits
@ -110,9 +141,24 @@ and all people who contributed to that lib.
## Future
#### Must
- elaborate performance connected state.
#### Should
- add examples
- a multi sensor == multiple pins, no bus
#### Could
- add rounding for **getTempC()**.
- now it truncates, so it can be 0.5°C off.
- add "0.5" to raw and truncate
- add "0.5" to raw and truncate improves only for 10 bits and higher.
#### Wont
- unit tests
- get it working

View File

@ -2,6 +2,7 @@
// FILE: DS18B20_INT.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Minimalistic demo
// URL: https://github.com/RobTillaart/DS18B20_INT
#include "DS18B20_INT.h"
@ -18,6 +19,9 @@ void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("DS18B20_INT_LIB_VERSION: ");
Serial.println(DS18B20_INT_LIB_VERSION);
sensor.begin();
}

View File

@ -2,6 +2,7 @@
// FILE: DS18B20_INT_getTempCentiC.ino
// AUTHOR: Rob Tillaart
// PURPOSE: Minimalistic demo
// URL: https://github.com/RobTillaart/DS18B20_INT
#include "DS18B20_INT.h"
@ -18,6 +19,9 @@ void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("DS18B20_INT_LIB_VERSION: ");
Serial.println(DS18B20_INT_LIB_VERSION);
sensor.begin();
sensor.setResolution(12);
Serial.println(sensor.getResolution());

View File

@ -2,7 +2,7 @@
// FILE: DS18B20_getAddress.ino
// AUTHOR: Rob Tillaart
// PURPOSE: DS18B20 lib getAddress demo
// URL: https://github.com/RobTillaart/DS18B20_INT
#include <OneWire.h>
#include <DS18B20_INT.h>

View File

@ -2,6 +2,7 @@
// FILE: DS18B20_minimum.ino
// AUTHOR: Rob Tillaart
// PURPOSE: most minimal sketch
// URL: https://github.com/RobTillaart/DS18B20_INT
//
// WARNING: this sketch does not wait for isConversionComplete()
// and therefore temperature read is probably incorrect
@ -22,6 +23,8 @@ void setup(void)
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("DS18B20_INT_LIB_VERSION: ");
Serial.println(DS18B20_INT_LIB_VERSION);
sensor.begin();
}

View File

@ -0,0 +1,108 @@
//
// FILE: DS18B20_performance.ino
// AUTHOR: Rob Tillaart
// PURPOSE: DS18B20 lib getAddress demo
// URL: https://github.com/RobTillaart/DS18B20_INT
#include <OneWire.h>
#include <DS18B20_INT.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DS18B20_INT sensor(&oneWire);
DeviceAddress da;
uint32_t start, stop;
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("DS18B20_INT_LIB_VERSION: ");
Serial.println(DS18B20_INT_LIB_VERSION);
delay(10);
start = micros();
bool b = sensor.getAddress(da);
stop = micros();
Serial.print("\ngetAddress: \t");
Serial.println(b);
Serial.print("Time: \t");
Serial.println(stop - start);
delay(10);
start = micros();
sensor.begin();
stop = micros();
Serial.print("\nbegin: \t");
Serial.println(b);
Serial.print("Time: \t");
Serial.println(stop - start);
delay(10);
start = micros();
b = sensor.getAddress(da);
stop = micros();
Serial.print("\ngetAddress: \t");
Serial.println(b);
Serial.print("Time: \t");
Serial.println(stop - start);
delay(10);
if (b == false)
{
Serial.println("No address found!");
}
Serial.print("Address: ");
for (uint8_t i = 0; i < 8; i++)
{
if (da[i] < 0x10) Serial.print('0');
Serial.print(da[i], HEX);
}
Serial.println();
delay(10);
start = micros();
sensor.requestTemperatures();
stop = micros();
Serial.print("\nrequestTemperatures: \t");
Serial.println();
Serial.print("Time: \t");
Serial.println(stop - start);
delay(10);
delay(750);
start = micros();
b = sensor.isConversionComplete();
stop = micros();
Serial.print("\nisConversionComplete: \t");
Serial.println(b);
Serial.print("Time: \t");
Serial.println(stop - start);
delay(10);
start = micros();
int temp = sensor.getTempC();
stop = micros();
Serial.print("\ngetTempC: \t");
Serial.println(temp);
Serial.print("Time: \t");
Serial.println(stop - start);
delay(10);
}
void loop()
{
}
// -- END OF FILE --

View File

@ -2,6 +2,7 @@
// FILE: DS18B20_simple.ino
// AUTHOR: Rob Tillaart
// PURPOSE: equivalent of DallasTemperature library Simple
// URL: https://github.com/RobTillaart/DS18B20_INT
#include <OneWire.h>
@ -21,7 +22,10 @@ void setup(void)
Serial.print("DS18B20_INT_LIB_VERSION: ");
Serial.println(DS18B20_INT_LIB_VERSION);
sensor.begin();
if (sensor.begin() == false)
{
Serial.println("not connected!");
}
}

View File

@ -0,0 +1,59 @@
//
// FILE: DS18B20_INT_two_sensors.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo with two sensors (on two pins)
// URL: https://github.com/RobTillaart/DS18B20_INT
#include "DS18B20_INT.h"
// numbers chosen to match pin numbers.
#define ONE_WIRE_BUS2 2
#define ONE_WIRE_BUS3 3
OneWire oneWire2(ONE_WIRE_BUS2);
OneWire oneWire3(ONE_WIRE_BUS3);
DS18B20_INT inside(&oneWire2);
DS18B20_INT outside(&oneWire3);
void setup(void)
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("DS18B20_INT_LIB_VERSION: ");
Serial.println(DS18B20_INT_LIB_VERSION);
inside.begin();
outside.begin();
// different resolution shows nicely the async behavior
inside.setResolution(12);
outside.setResolution(10);
// request initial read
inside.requestTemperatures();
outside.requestTemperatures();
}
void loop(void)
{
// print the temperature when available and request a new read
if (inside.isConversionComplete())
{
Serial.print("inside:\t");
Serial.println(inside.getTempC());
inside.requestTemperatures();
}
if (outside.isConversionComplete())
{
Serial.print("outside:\t\t");
Serial.println(outside.getTempC());
outside.requestTemperatures();
}
}
// -- END OF FILE --

View File

@ -0,0 +1,69 @@
//
// FILE: oneWireSearch.ino
// AUTHOR: Rob Tillaart
// PURPOSE: scan for 1-Wire devices + code snippet generator
// DATE: 2015-june-30
// URL: http://forum.arduino.cc/index.php?topic=333923
// URL: https://github.com/RobTillaart/DS18B20_RT
//
// inspired by http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
#include <OneWire.h>
void setup()
{
Serial.begin(115200);
Serial.println("//\n// Start oneWireSearch.ino \n//");
for (uint8_t pin = 2; pin < 13; pin++)
{
findDevices(pin);
}
Serial.println("\n//\n// End oneWireSearch.ino \n//");
}
void loop()
{
}
uint8_t findDevices(int pin)
{
OneWire ow(pin);
uint8_t address[8];
uint8_t count = 0;
if (ow.search(address))
{
Serial.print("\nuint8_t pin");
Serial.print(pin, DEC);
Serial.println("[][8] = {");
do {
count++;
Serial.println(" {");
for (uint8_t i = 0; i < 8; i++)
{
Serial.print("0x");
if (address[i] < 0x10) Serial.print("0");
Serial.print(address[i], HEX);
if (i < 7) Serial.print(", ");
}
Serial.println(" },");
} while (ow.search(address));
Serial.println("};");
Serial.print("// nr devices found: ");
Serial.println(count);
}
return count;
}
// -- END OF FILE --

View File

@ -18,6 +18,7 @@ getTempCentiC KEYWORD2
# Constants (LITERAL1)
DS18B20_INT_LIB_VERSION LITERAL1
DEVICE_DISCONNECTED LITERAL1

View File

@ -23,7 +23,7 @@
"version": "^2.3.5"
}
],
"version": "0.2.1",
"version": "0.2.2",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=DS18B20_int
version=0.2.1
version=0.2.2
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Library for DS18B20 restricted to a single sensor per pin.

View File

@ -40,6 +40,12 @@ unittest_teardown()
}
unittest(test_constants)
{
assertEqual(-127, DEVICE_DISCONNECTED);
}
unittest(test_constructor)
{
OneWire oneWire(4);