I'm using DS18B20 sensor with this library, the 750ms delay is causing problem in timer part of my code. the code below is from DallasTemperature.cpp:
// returns number of milliseconds to wait till conversion is complete (based on IC datasheet)
int16_t DallasTemperature::millisToWaitForConversion(uint8_t bitResolution) {
switch (bitResolution) {
case 9:
return 94;
case 10:
return 188;
case 11:
return 375;
default:
return 750;
}
I'm reading the temperature like this:
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS A0
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {}
void loop () {
float currentTemp = sensors.getTempCByIndex(0);
sensors.requestTemperatures();
}
sensor is not connected in parasite mode, and there is only one sensor.
How can i change the resolution from 12 to 9 bit?
2 Answers 2
Simple:
sensors.setResolution(9);
It normally defaults to 9, but then probes the connected device to find the maximum resolution it supports and sets it to that. This would normally be done in sensors.begin()
which you omitted from your snippet above. If you don't have that it will be on 9 bit anyway, and also the bus probably won't work regardless.
-
thanks, do i have to declare the address too? sensors.begin(); sensors.setResolution(9); should be in void setup?ElectronSurf– ElectronSurf2019年07月16日 09:45:14 +00:00Commented Jul 16, 2019 at 9:45
-
2
sensors.begin()
finds the number of sensors connected and works out the maximum resolution supported.sensors.setResolution(9)
sets the resolution for all future transactions for all connected devices.Majenko– Majenko2019年07月16日 10:08:49 +00:00Commented Jul 16, 2019 at 10:08
Seems to work better:
sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);