Skip to content

Commit a2943e3

Browse files
authored
Refactor DS18B20 Temperature Conversion Time Handling
Currently, the DS18B20 driver uses a fixed delay of 1 millisecond (board.io.sendOneWireDelay(pin, 1);) after requesting each sensor to perform a temperature conversion. However, the DS18B20 datasheet specifies that a conversion actually takes up to 750 milliseconds. This misalignment could potentially lead to inaccuracies when reading from multiple DS18B20 sensors simultaneously, particularly if the requested frequency (options.freq) is shorter than the time required for a temperature conversion. Additionally, it's important to note that the implementation of the delayTask function on the Firmata side is not fully functional and relies on the inclusion of a "FirmataScheduler" module, which merely adds processor cycles and does not provide a true delay mechanism. rwaldron#1823
1 parent 094bf6c commit a2943e3

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

lib/thermometer.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -222,22 +222,24 @@ const Drivers = {
222222
board.io.sendOneWireReset(pin);
223223
board.io.sendOneWireWrite(pin, device, CONSTANTS.CONVERT_TEMPERATURE_COMMAND);
224224
});
225-
226-
// the delay gives the sensor time to do the calculation
227-
board.io.sendOneWireDelay(pin, 1);
225+
226+
// board.io.sendOneWireDelay(pin, 1);
227+
228+
// the DS18B20 datasheet specifies that a conversion actually takes up to 750 milliseconds.
229+
let conversionTime = 750; // Time needed for temperature conversion
228230

229231
readOne = () => {
230232
let device;
231233

232234
if (devicesToRead.length === 0) {
233-
setTimeout(readThermometer, freq);
235+
setTimeout(readThermometer, Math.max(0, freq - conversionTime)); // adjust delay to achieve desired freq
234236
return;
235237
}
236238

237239
device = devicesToRead.pop();
240+
238241
// read from the scratchpad
239242
board.io.sendOneWireReset(pin);
240-
241243
board.io.sendOneWireWriteAndRead(pin, device, CONSTANTS.READ_SCRATCHPAD_COMMAND, CONSTANTS.READ_COUNT, (err, data) => {
242244
if (err) {
243245
this.emit("error", err);
@@ -248,10 +250,10 @@ const Drivers = {
248250
this.emit("data", getAddress(device), result);
249251

250252
readOne();
251-
});
252-
};
253253

254-
readOne();
254+
});
255+
};
256+
setTimeout(readOne, conversionTime);
255257
};
256258

257259
readThermometer();

0 commit comments

Comments
 (0)