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.
Information from DS18B20 datasheet
|9 bits||0.5°C||93.75 ms|
|10 bits||0.25°C||187.5 ms|
|11 bits||0.125°C||375 ms|
|12 bits||0.0625°C||750 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.
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.
Here is the related resolution set depending on the values of R0 and R1:
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):
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 (12 bits vs 10 bits vs 9 bits):
NB : display has only 1 digit that’s why you cannot see 0,25 resolution 😉
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)
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.
Then like this your DS18B20 will always have the good resolution at startup during 10 years according datasheet 🙂 !