0

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?

asked Jul 16, 2019 at 9:29

2 Answers 2

1

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.

answered Jul 16, 2019 at 9:43
2
  • thanks, do i have to declare the address too? sensors.begin(); sensors.setResolution(9); should be in void setup? Commented 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. Commented Jul 16, 2019 at 10:08
1

Seems to work better:

sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
sempaiscuba
1,0429 gold badges21 silver badges32 bronze badges
answered May 5, 2021 at 20:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.