DS18B20 : How to change resolution (9/10/11/12 bits)?

DS18B20 from Maxim (formerly Dallas) is a great chip for measuring temperature in your projects. But do you know you can set up the temperature resolution measurement? Yes, the DS18B20 gives you the possibility to choose the resolution of the temperature.

DS18B20

Information from DS18B20 datasheet

ModeResolConversion time
9 bits0.5°C 93.75 ms
10 bits0.25°C187.5 ms
11 bits0.125°C375 ms
12 bits 0.0625°C750 ms

The DS18B20 has an internal register in which the user can set values (called scratchpad). User can modify Byte 2,3 and 4.

On our case, we will focus on the Byte 4 which is called the “configuration register” and which contains the bits used to select the resolution.

image

The EEPROM will then save this resolution in order that after a power disconnection, the component will keep this resolution as default.

Let’s now talk about the Byte 4 Configuration Register: This byte contains two bits which are used to change resolution: R0 and R1.

image

Here is the related resolution set depending on the values of R0 and R1:image

Now, what we need to do is to set this register to the right value and write it to the scratchpad. For this, we will use the write scratchpad command (4Eh):

image

Time to code

In this first version, we’ll see how to set up the resolution on a temporary way. The set up will be reset on device power down.

Output

Output (12 bits vs 10 bits vs 9 bits): 

NB : display has only 1 digit that’s why you cannot see 0,25 resolution 😉

Capture d’écran 2015-11-17 à 12.53.49

 

Save the new resolution in EEPROM

If you want to save in EEPROM this resolution, you will need to apply the copy scratchpad command (48h)image

Take a look art the 2 last lines:

Be careful that once you’ve stored the resolution into the EEPROM, there is no need to do this at each power up or at runtime. Moreover it’s not recommended to keep these lines of code because it will write in EEPROM at each time you will run your device.image

Then like this your DS18B20 will always have the good resolution at startup during 10 years according datasheet 🙂 !

Enjoy 😀

 

7 Comments

  1. The “ds.select(addr)” is missing after the “reset()” in the second code snipped, which suppose to write to the EEPROM. Try this:

    ds.search(addr); // address on 1wire bus

    if (addr[0] == DS18B20) // check we are really using a DS18B20
    {
    ds.reset(); // rest 1-Wire
    ds.select(addr); // select DS18B20

    ds.write(0x4E); // write on scratchPad
    ds.write(0x00); // User byte 0 – Unused
    ds.write(0x00); // User byte 1 – Unused
    ds.write(0x7F); // set up en 12 bits (0x7F)

    ds.reset(); // reset 1-Wire
    ds.select(addr); // select DS18B20

    ds.write(0x48); // copy scratchpad to l’EEPROM
    delay(15); // wit for end of write

    }

    Reply
  2. Hi, thanks very much for the article – it helped me understand a lot about how the config info is stored. I have several ds18b20 sensors installed (connected to Raspberry Pi 2). My sensors were set by default to 9-bit, and I need 12 bits of resolution for my application. (I understand that that is exceeding the specific accuracy of the sensor at most temperatures, but I am looking at temperature change over time, and 0.05C change is meaningful to me.)

    Question: I see that you have included code to change the resolution. I don’t understand what this code is. What language is this written in, and how can I run it? Even the most basic notes on this would allow me to do the Googling necessary to use your code.

    And also, am I meant to replace the “addr” with the address of my device (e.g. “28-0000072a599a”)? Or does this code run as-is without modification?

    Thank you very much!!!

    Reply

Leave a Comment.