I am using DS18B20 v0.1.13 with OneWire v2.0.4. It takes 750ms to get a temperature reading. How can I improve it? Thanks.
ds18b20 = new DS18B20(D6, true) ;
float _temp;
int i = 0;
do
{
_temp = ds18b20->getTemperature();
} while (!ds18b20->crcCheck() && MAXRETRY > i++);
if (i < MAXRETRY)
{
temperature = ds18b20->convertToFahrenheit(_temp);
}
One thing would be to select a lower resolution since the max. 12bit will only allow a sample rate of one per 750ms.
However, that library does not (yet) support dynamic delay depending on the selected resolution, but that should not be too hard to implement.
Adding a variable for the delay and setting that in DS18B20::setResolution() might help. But I’m not sure if too short a delay may cause the “parasitic power” cap to fail charging up enough (for the P variant of the sensor).
I have updated the GitHub repo and published v0.1.14.
Have you tested the new version?
With that your code shouldn’t need to care about the delay. The library should not read any faster than acceptable for the resolution.
You just need to set the appropriate resolution for your speed requirements.
e.g. via ds18b20->setResolution(TEMP_9_BIT); for a 94ms delay.