Skip to content

Commit

Permalink
Refactor DS18B20 Temperature Conversion Time Handling
Browse files Browse the repository at this point in the history
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
  • Loading branch information
echavet committed May 26, 2023
1 parent 094bf6c commit a2943e3
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/thermometer.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,22 +222,24 @@ const Drivers = {
board.io.sendOneWireReset(pin);
board.io.sendOneWireWrite(pin, device, CONSTANTS.CONVERT_TEMPERATURE_COMMAND);
});

// the delay gives the sensor time to do the calculation
board.io.sendOneWireDelay(pin, 1);

// board.io.sendOneWireDelay(pin, 1);

// the DS18B20 datasheet specifies that a conversion actually takes up to 750 milliseconds.
let conversionTime = 750; // Time needed for temperature conversion

readOne = () => {
let device;

if (devicesToRead.length === 0) {
setTimeout(readThermometer, freq);
setTimeout(readThermometer, Math.max(0, freq - conversionTime)); // adjust delay to achieve desired freq
return;
}

device = devicesToRead.pop();

// read from the scratchpad
board.io.sendOneWireReset(pin);

board.io.sendOneWireWriteAndRead(pin, device, CONSTANTS.READ_SCRATCHPAD_COMMAND, CONSTANTS.READ_COUNT, (err, data) => {
if (err) {
this.emit("error", err);
Expand All @@ -248,10 +250,10 @@ const Drivers = {
this.emit("data", getAddress(device), result);

readOne();
});
};

readOne();
});
};
setTimeout(readOne, conversionTime);
};

readThermometer();
Expand Down

0 comments on commit a2943e3

Please sign in to comment.