Skip to content

Commit 25cd5d1

Browse files
committed
Added support for resolution 9-12 bits
1 parent f98762f commit 25cd5d1

8 files changed

Lines changed: 314 additions & 12 deletions

File tree

README.md

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,26 @@ uint32_t getExpirationDelay() const;
8888
// Register callback for temperature changes
8989
// "changed" parameter indicates if temperature changed by > 0.3°C
9090
void listen(DS18ChangeCallback callback);
91+
92+
// Set temperature resolution (9, 10, 11, or 12 bits)
93+
// Higher resolution = more precise but slower conversion
94+
// Returns true if successful
95+
bool setResolution(DS18::Resolution resolution);
96+
97+
// Get current resolution setting
98+
DS18::Resolution getResolution() const;
99+
100+
// Get conversion time in milliseconds for current resolution
101+
uint16_t getConversionTime() const;
91102
```
92103
104+
**Resolution Options:**
105+
106+
- `RESOLUTION_9_BIT`: 0.5°C precision, 94ms conversion time
107+
- `RESOLUTION_10_BIT`: 0.25°C precision, 188ms conversion time
108+
- `RESOLUTION_11_BIT`: 0.125°C precision, 375ms conversion time
109+
- `RESOLUTION_12_BIT`: 0.0625°C precision, 750ms conversion time (default)
110+
93111
### Information
94112
95113
```c++
@@ -232,7 +250,7 @@ void setup() {
232250
continue;
233251
234252
temp.begin(18);
235-
253+
236254
temp.listen([](float temperature, bool changed) {
237255
JsonDocument doc;
238256
temp.toJson(doc.to<JsonObject>());
@@ -260,25 +278,55 @@ void setup() {
260278
continue;
261279

262280
temp.begin(18);
263-
281+
264282
// Temperature readings expire after 30 seconds
265283
temp.setExpirationDelay(30);
266284
}
267285

268286
void loop() {
269287
temp.read();
270-
288+
271289
if (temp.isValid()) {
272290
float t = temp.getTemperature().value_or(0);
273291
Serial.printf("Valid temperature: %.2f°C\n", t);
274292
} else if (temp.isExpired()) {
275293
Serial.println("Temperature reading expired!");
276294
}
277-
295+
278296
delay(2000);
279297
}
280298
```
281299

300+
### Resolution Control
301+
302+
```c++
303+
#include <MycilaDS18.h>
304+
305+
Mycila::DS18 temp;
306+
307+
void setup() {
308+
Serial.begin(115200);
309+
while (!Serial)
310+
continue;
311+
312+
temp.begin(18);
313+
314+
// Set resolution to 10-bit for faster conversion
315+
// Trade-off: lower precision but faster readings
316+
if (temp.setResolution(Mycila::DS18::Resolution::RESOLUTION_10_BIT)) {
317+
Serial.println("Resolution set to 10-bit");
318+
}
319+
320+
Serial.printf("Current resolution: %d-bit\n", 9 + static_cast<int>(temp.getResolution()));
321+
Serial.printf("Conversion time: %d ms\n", temp.getConversionTime());
322+
}
323+
324+
void loop() {
325+
temp.read();
326+
delay(1000);
327+
}
328+
```
329+
282330
## Advanced Usage
283331

284332
### Safe Temperature Access with std::optional
@@ -309,6 +357,7 @@ The library includes several examples in the `examples/` folder:
309357
- **SetAddress**: Using a specific sensor address
310358
- **MultipleDS18**: Multiple sensors on the same bus
311359
- **Json**: JSON output support
360+
- **Resolution**: Controlling sensor resolution for precision/speed trade-offs
312361
313362
## License
314363

docs/index.md

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,26 @@ uint32_t getExpirationDelay() const;
8888
// Register callback for temperature changes
8989
// "changed" parameter indicates if temperature changed by > 0.3°C
9090
void listen(DS18ChangeCallback callback);
91+
92+
// Set temperature resolution (9, 10, 11, or 12 bits)
93+
// Higher resolution = more precise but slower conversion
94+
// Returns true if successful
95+
bool setResolution(DS18::Resolution resolution);
96+
97+
// Get current resolution setting
98+
DS18::Resolution getResolution() const;
99+
100+
// Get conversion time in milliseconds for current resolution
101+
uint16_t getConversionTime() const;
91102
```
92103
104+
**Resolution Options:**
105+
106+
- `RESOLUTION_9_BIT`: 0.5°C precision, 94ms conversion time
107+
- `RESOLUTION_10_BIT`: 0.25°C precision, 188ms conversion time
108+
- `RESOLUTION_11_BIT`: 0.125°C precision, 375ms conversion time
109+
- `RESOLUTION_12_BIT`: 0.0625°C precision, 750ms conversion time (default)
110+
93111
### Information
94112
95113
```c++
@@ -232,7 +250,7 @@ void setup() {
232250
continue;
233251
234252
temp.begin(18);
235-
253+
236254
temp.listen([](float temperature, bool changed) {
237255
JsonDocument doc;
238256
temp.toJson(doc.to<JsonObject>());
@@ -260,25 +278,55 @@ void setup() {
260278
continue;
261279

262280
temp.begin(18);
263-
281+
264282
// Temperature readings expire after 30 seconds
265283
temp.setExpirationDelay(30);
266284
}
267285

268286
void loop() {
269287
temp.read();
270-
288+
271289
if (temp.isValid()) {
272290
float t = temp.getTemperature().value_or(0);
273291
Serial.printf("Valid temperature: %.2f°C\n", t);
274292
} else if (temp.isExpired()) {
275293
Serial.println("Temperature reading expired!");
276294
}
277-
295+
278296
delay(2000);
279297
}
280298
```
281299

300+
### Resolution Control
301+
302+
```c++
303+
#include <MycilaDS18.h>
304+
305+
Mycila::DS18 temp;
306+
307+
void setup() {
308+
Serial.begin(115200);
309+
while (!Serial)
310+
continue;
311+
312+
temp.begin(18);
313+
314+
// Set resolution to 10-bit for faster conversion
315+
// Trade-off: lower precision but faster readings
316+
if (temp.setResolution(Mycila::DS18::Resolution::RESOLUTION_10_BIT)) {
317+
Serial.println("Resolution set to 10-bit");
318+
}
319+
320+
Serial.printf("Current resolution: %d-bit\n", 9 + static_cast<int>(temp.getResolution()));
321+
Serial.printf("Conversion time: %d ms\n", temp.getConversionTime());
322+
}
323+
324+
void loop() {
325+
temp.read();
326+
delay(1000);
327+
}
328+
```
329+
282330
## Advanced Usage
283331

284332
### Safe Temperature Access with std::optional
@@ -309,6 +357,7 @@ The library includes several examples in the `examples/` folder:
309357
- **SetAddress**: Using a specific sensor address
310358
- **MultipleDS18**: Multiple sensors on the same bus
311359
- **Json**: JSON output support
360+
- **Resolution**: Controlling sensor resolution for precision/speed trade-offs
312361
313362
## License
314363

examples/Resolution/Resolution.ino

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <Arduino.h>
2+
#include <MycilaDS18.h>
3+
4+
Mycila::DS18 temp;
5+
6+
void setup() {
7+
Serial.begin(115200);
8+
while (!Serial)
9+
continue;
10+
11+
temp.setThreshold(0.0f); // any temperature change will be reported in callback
12+
13+
temp.listen([](float temperature, bool changed) {
14+
if (changed) {
15+
Serial.printf("Temperature changed: %f°C\n", temperature);
16+
}
17+
});
18+
19+
temp.begin(18);
20+
21+
Serial.printf("Current resolution: %d-bit\n", static_cast<int>(temp.getResolution()));
22+
Serial.printf("Conversion time: %d ms\n", temp.getConversionTime());
23+
}
24+
25+
uint32_t last = 0;
26+
uint8_t res = 3; // Mycila::DS18::Resolution::RESOLUTION_12_BIT;
27+
28+
void loop() {
29+
if (millis() - last > 10000) {
30+
res = (res + 1) % 4;
31+
Serial.printf("Changing resolution to %d-bit\n", 9 + res);
32+
// Set resolution to 10-bit for faster conversion
33+
// Options:
34+
// - RESOLUTION_9_BIT: 0.5°C precision, 94ms conversion time
35+
// - RESOLUTION_10_BIT: 0.25°C precision, 188ms conversion time
36+
// - RESOLUTION_11_BIT: 0.125°C precision, 375ms conversion time
37+
// - RESOLUTION_12_BIT: 0.0625°C precision, 750ms conversion time (default)
38+
if (!temp.setResolution(static_cast<Mycila::DS18::Resolution>(9 + res))) {
39+
Serial.println("Failed to set resolution");
40+
}
41+
42+
Serial.printf("Current resolution: %d-bit\n", static_cast<int>(temp.getResolution()));
43+
Serial.printf("Conversion time: %d ms\n", temp.getConversionTime());
44+
45+
last = millis();
46+
}
47+
48+
temp.read();
49+
delay(temp.getConversionTime() + 100);
50+
}

platformio.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ lib_dir = .
2626
; src_dir = examples/Search
2727
; src_dir = examples/Json
2828
; src_dir = examples/SetAddress
29-
src_dir = examples/MultipleDS18
29+
; src_dir = examples/MultipleDS18
30+
src_dir = examples/Resolution
3031

3132
[env:arduino-3]
3233
platform = https://github.com/pioarduino/platform-espressif32/releases/download/55.03.34/platform-espressif32.zip

src/MycilaDS18.cpp

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,11 @@ bool Mycila::DS18::read() {
148148
// read is valid, record the time
149149
_lastTime = millis();
150150

151-
const bool changed = std::abs(read - _temperature) >= MYCILA_DS18_RELEVANT_TEMPERATURE_CHANGE || !isValid();
151+
const bool changed = std::abs(read - _temperature) > _threshold || !isValid();
152152

153153
if (changed) {
154154
_temperature = read;
155-
ESP_LOGD(TAG, "%s 0x%llx @ pin %d: %.2f °C", _name, _deviceAddress, _pin, read);
155+
ESP_LOGD(TAG, "%s 0x%llx @ pin %d: %f °C", _name, _deviceAddress, _pin, read);
156156
}
157157

158158
if (_callback)
@@ -161,6 +161,57 @@ bool Mycila::DS18::read() {
161161
return true;
162162
}
163163

164+
bool Mycila::DS18::setResolution(DS18::Resolution resolution) {
165+
if (!_enabled)
166+
return false;
167+
168+
std::lock_guard<std::mutex> lock(_mutex);
169+
170+
OneWire32::Resolution owRes;
171+
switch (resolution) {
172+
case DS18::Resolution::RESOLUTION_9_BIT:
173+
owRes = OneWire32::RESOLUTION_9_BIT;
174+
break;
175+
case DS18::Resolution::RESOLUTION_10_BIT:
176+
owRes = OneWire32::RESOLUTION_10_BIT;
177+
break;
178+
case DS18::Resolution::RESOLUTION_11_BIT:
179+
owRes = OneWire32::RESOLUTION_11_BIT;
180+
break;
181+
case DS18::Resolution::RESOLUTION_12_BIT:
182+
owRes = OneWire32::RESOLUTION_12_BIT;
183+
break;
184+
default:
185+
return false;
186+
}
187+
188+
if (_oneWire->setResolution(_deviceAddress, owRes)) {
189+
_resolution = resolution;
190+
ESP_LOGI(TAG, "%s 0x%llx @ pin %d: Resolution set to %d-bit (%d ms conversion time)", _name, _deviceAddress, _pin, static_cast<int>(resolution), getConversionTime());
191+
return true;
192+
}
193+
194+
return false;
195+
}
196+
197+
Mycila::DS18::Resolution Mycila::DS18::getResolution() const {
198+
return _resolution;
199+
}
200+
201+
uint16_t Mycila::DS18::getConversionTime() const {
202+
switch (_resolution) {
203+
case DS18::Resolution::RESOLUTION_9_BIT:
204+
return 94;
205+
case DS18::Resolution::RESOLUTION_10_BIT:
206+
return 188;
207+
case DS18::Resolution::RESOLUTION_11_BIT:
208+
return 375;
209+
case DS18::Resolution::RESOLUTION_12_BIT:
210+
default:
211+
return 750;
212+
}
213+
}
214+
164215
#ifdef MYCILA_JSON_SUPPORT
165216
void Mycila::DS18::toJson(const JsonObject& root) const {
166217
root["enabled"] = _enabled;
@@ -171,5 +222,7 @@ void Mycila::DS18::toJson(const JsonObject& root) const {
171222
root["temp"] = getTemperature().value_or(0);
172223
root["time"] = _lastTime;
173224
root["valid"] = isValid();
225+
root["resolution"] = static_cast<int>(_resolution);
226+
root["conversion_time"] = getConversionTime();
174227
}
175228
#endif

0 commit comments

Comments
 (0)