Differences on DS18B20 resolution

I am using an Arduino DUE with a temperature sensor DS18B20.
I want to change the resolution of the sensor and I aspect to obtain more "precise" results with the resolution = 12 and a rought estimation with resolution = 9.
However I when I print the temperatures (obtained with different resolutions) I get same temperature (see attachment the image)!! How is possible that??

From the serial monitor I get:

34.31    Resolution: 9
34.31    Resolution: 10
34.31    Resolution: 11
34.31    Resolution: 12

-----------------------------
34.25    Resolution: 9
34.25    Resolution: 10
34.25    Resolution: 11
34.25    Resolution: 12

-----------------------------
34.19    Resolution: 9
34.19    Resolution: 10
34.19    Resolution: 11
34.19    Resolution: 12

Here is the code I am using:

// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is connected to GPIO4
#define ONE_WIRE_BUS 4



// Setup a oneWire instance to communicate with a OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature sensors(&oneWire);

DeviceAddress sensor1 = { 0x28, 0xCA, 0xBA, 0x55, 0x5, 0x0, 0x0, 0xD0 };

void setup(void){
  Serial.begin(115200);
  sensors.begin();
}

void loop(void){ 
  sensors.requestTemperatures(); // Send the command to get temperatures
  
  Serial.println("");
  Serial.println("-----------------------------");
  sensors.setResolution(sensor1, 9);
  Serial.print(sensors.getTempC(sensor1));
  Serial.print("    Resolution: ");
  Serial.print(sensors.getResolution(sensor1));
  Serial.println();

  
  sensors.setResolution(sensor1, 10);
  Serial.print(sensors.getTempC(sensor1));
  Serial.print("    Resolution: ");
  Serial.print(sensors.getResolution(sensor1));
  Serial.println();
  
  sensors.setResolution(sensor1, 11);
  Serial.print(sensors.getTempC(sensor1));
  Serial.print("    Resolution: ");
  Serial.print(sensors.getResolution(sensor1));
  Serial.println();
  
  sensors.setResolution(sensor1, 12);
  Serial.print(sensors.getTempC(sensor1));
  Serial.print("    Resolution: ");
  Serial.print(sensors.getResolution(sensor1));
  Serial.println();

}

Temperature.jpg

I solved: I need to send the command to get temperatures for every measurement!

  sensors.setResolution(sensor1, 10);
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.print(millis());
  Serial.print(" ");
  Serial.print(sensors.getTempC(sensor1));
  Serial.print(" ");
  
  sensors.setResolution(sensor1, 11);
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.print(millis());
  Serial.print(" ");
  Serial.print(sensors.getTempC(sensor1));
  Serial.print(" ");
  
  sensors.setResolution(sensor1, 12);
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.print(millis());
  Serial.print(" ");
  Serial.print(sensors.getTempC(sensor1));
  Serial.println();

@OP

Let us get clarified the meaning of 'Resolution' in the context of DS18B20 Temperature Sensor.

1. There is a programmable ADC (analog-to-digital converter) inside the DS18B20 sesnor. We can command the sensor to take a sample of 'input (how warm it is) signal' and convert it into 9-bit or 10-bit of 11-bit or 12-bit which is later on presented in 0C unit.

2. The following diagram (taken from the data sheets of the sensor) tells about the bit pattern of the ADC value.
![/cod


![dsTempReg.png|582x84](upload://nsFja9gYpz38F9YtmrB1g5v0oPB.png)
Figure-1: 

**(1)** The Fig-1 says that the temperature value has two parts -- integer part and the fractional part. This is to say that the temperature is a floating point number (simply float). In Fig-1, the decimal point (the . symbol) is not shown/included; but, it is understood and it is located just after this: 2<sup>0</sup> and before this: 2<sup>-1</sup>. There is also a sign (S) bit which (when assumes Logic-High state) represents -ve temperature. (The float value is encoded in 2's complement form and not in binary32 format.)

**(2)** In Fig-1, we observe that there are upto 4 digits that we can include after the decimal point and these are: 2<sup>-1</sup>, 2<sup>-2</sup>, 2<sup>-3</sup>, and 2<sup>-4</sup> with respective positional values of: 0.5<sup>0</sup>C, 0.25<sup>0</sup>C, 0.125<sup>0</sup>C, and 0.0625<sup>0</sup>C.

**(3)** Now, the meanings of setting up 'Resolution' to 9-bit, 10-bit, 11-bit, and 12-bit' stand as:
**(a)** 9-bit: 
1-bit for sign, 7-bit for integer part, and 1-bit for fractional part (1-digit after decimal point) 

Temperature range: xxx.0<sup>0</sup>C to xxx.5<sup>0</sup>C in 0.5<sup>0</sup>C discrete step.

The command to present this float temperature (dsTemp) of 1-digit precision (number of digit after the decimal point) when the ADC Resolution is 9-bit. 
Serial.print(temp, 1); //temp must be in float; 2nd argument 1 refers 1-digit precision

(Let us observe that there is subtle difference between precision and resolution.)

**(b)** 10-bit: 
1-bit for sign, 7-bit for integer part, and 2-bit for fractional part (2-digit after decimal point) 
Temperature range: xxx.00<sup>0</sup>C to xxx.75<sup>0</sup>C in 0.25<sup>0</sup>C discrete step.

The command to present this float temp:
Serial.print(dsTemp, 2);

**(c)** 11-bit: 
1-bit for sign, 7-bit for integer part, and 3-bit for fractional part (3-digit after decimal point) 
Temperature range: xxx.000<sup>0</sup>C to xxx.875<sup>0</sup>C in 0.125<sup>0</sup>C discrete step.

The command to present this float temp:
Serial.print(dsTemp, 3);

**(d)** 12-bit: 
1-bit for sign, 7-bit for integer part, and 4-bit for fractional part (4-digit after decimal point) 
Temperature range: xxx.0000<sup>0</sup>C to xxx.9375<sup>0</sup>C in 0.0625<sup>0</sup>C discrete step.

The command to present this float temp:
Serial.print(dsTemp, 4);

Hope that the issue is clear!

**The Test Codes:** (tested in UNO)

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 2  //DPin-2 of UNO at signal terminal

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float dsTemp;

void setup(void)
{
 Serial.begin(9600);
 sensors.begin();      //initialize One-Wire Sensor -- DS18B20
}

void loop(void)
{
 sensors.setResolution(9);  //before each measurement, set resolution
 //sensors.setResolution(10);
 //sensors.setResolution(11);
 //sensors.setResolution(12);
 sensors.requestTemperatures();  // Temp conversion command; waiting here until comversion is done
 dsTemp = sensors.getTempCByIndex(0);  //read temp data from Sensor #0 and convert to celsius float

Serial.println(dsTemp, 1);  //1-digit precision
 //Serial.println(dsTemp, 2);  //2-digit precision
 //Serial.println(dsTemp, 3);  //3-digit precision
 //Serial.println(dsTemp, 4);  //4-digit precision
 delay(1000);    //sample temperature at 1-sec interval
}![/cod


![dsTempReg.png|582x84](upload://nsFja9gYpz38F9YtmrB1g5v0oPB.png)

Dear GolamMostafa,
thanks you very much for your clarification and useful hints about DS18B20 :slight_smile: