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;
        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);

sensor is not connected in parasite mode, and there is only one sensor.

How can i change the resolution from 12 to 9 bit?




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 Jul 16 '19 at 9:45
  • 1
    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 Jul 16 '19 at 10:08

Seems to work better:

sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
New contributor
Brandon is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.