Is DS18B20 really 12 bit ?

Hi there,

I don't have the hardware yet so I'm simulating it with Wokwi.
I want to connect a DS18B20 temperature sensor to a Nano and read and display a 12 bit temp value (3 decimal digits). Easy enough, or so I thought...
The sketch :

#include "OneWire.h"
#include "DallasTemperature.h"

#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

void setup() {
  uint8_t sensorAddress[8];

  // Begin serial communication:
  Serial.begin(115200);
  sensors.begin();

  sensors.getAddress(sensorAddress, 0);
  Serial.print("Sensor Address:");
  for (byte i = 0; i < 8; i++) {
    Serial.print(F(" 0x"));
    if (sensorAddress[i] < 0x10) Serial.write('0');
    Serial.print(sensorAddress[i], HEX);
  }
  Serial.println();
}

void loop() {
  sensors.requestTemperatures();

  float tempC = sensors.getTempCByIndex(0); // the index 0 refers to the first device

  // Print the temperature in Celsius in the Serial Monitor:
  Serial.print("Temperature: ");
  Serial.print(tempC, 3);
  Serial.write('°');
  Serial.println('C');

  // Wait 1 second:
  delay(1000);
}

and the diagram.json :

{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": 0, "left": 0, "attrs": {} },
    {
      "type": "board-ds18b20",
      "id": "temp1",
      "top": 4.18,
      "left": 257.23,
      "rotate": 90,
      "attrs": { "familyCode": "40", "deviceID": "111111111111", "temperature": "15.375" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r1",
      "top": 23.15,
      "left": 172.8,
      "attrs": { "value": "4700" }
    }
  ],
  "connections": [
    [ "nano:5V.2", "r1:1", "red", [ "h0" ] ],
    [ "nano:5V.2", "temp1:VCC", "red", [ "h0" ] ],
    [ "temp1:DQ", "r1:2", "green", [ "v0" ] ],
    [ "nano:23", "temp1:DQ", "green", [ "v0" ] ],
    [ "nano:2", "temp1:DQ", "green", [ "v-14.4", "h124.3", "v38.4" ] ],
    [ "nano:GND.2", "temp1:GND", "black", [ "v14.4", "h191.5" ] ]
  ],
  "dependencies": {}
}

whose important part about the DS18B20 sensor is this :

"attrs": { "familyCode": "40", "deviceID": "111111111111", "temperature": "15.375" }

"familyCode": "40" means DS18B20 as opposed to the default "familyCode": "10" which designate the DS18S20.

As you can see running the simulation, the temperature resolution is 9 bit or half a °C.
My ultimate goal is to have 4 temp sensors accessed using their ROM address (this part actually already woks) but I need to read all 12 bit "first".

Is there anything wrong with my code ?

This is a discussion of accuracy vs precision. The DS18B20 is accurate to +-0.5 Celsius. It can measure with up to 12 bit precision. Whither the extra bits are meaningful is in the eye of the beholder.

The result can be configured for 9-12 bits.

2 Likes

That's right, the DS18B20 is accurate to +-0.5 Celsius as the DS18S20 is.

From the datasheets, the S is 9 bit only and the B is 9 - 12 bit. For some reason, my Wokwi simulation shows just the opposite, I can read "the temp cursor" correctly if using "familyCode": "10" (DS18S20) but it is 0.5°C "floored" with the DS18B20.

Is it due to the simulation of this sensor in Wokwi ?

If the simulator does not behave as you expect from the sensor data sheet, blame the simulator and test the actual hardware.

2 Likes

Which line of code sets the bit resolution?

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

// Data wire is connected to Arduino digital pin 2
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature sensors(&oneWire);

void setup(void)
{
  // Start serial communication for debugging purposes
  Serial.begin(9600);
  // Start up the library
  sensors.begin();
}

void loop(void) {
  // Define the resolutions to set (9, 10, 11, 12 bits)
  int resolutions[] = {9, 10, 11, 12}; // List of resolutions
  const int numResolutions = sizeof(resolutions) / sizeof(resolutions[0]); // Number of resolutions

  for (int i = 0; i < numResolutions; i++) {
    // Set the resolution
    sensors.setResolution(resolutions[i]);
    Serial.print("Resolution set to: ");
    Serial.print(resolutions[i]);
    Serial.println(" bits");

    // Allow some time for the setting to take effect
    delay(1000);

    // Request temperatures from the sensors
    sensors.requestTemperatures();

    // Print the temperature in Celsius and Fahrenheit
    Serial.print("Celsius temperature: ");
    Serial.print(sensors.getTempCByIndex(0)); 
    Serial.print(" - Fahrenheit temperature: ");
    Serial.println(sensors.getTempFByIndex(0));

    // Read and print the resolution of the sensor
    int currentResolution = sensors.getResolution();
    Serial.print("Current resolution: ");
    Serial.print(currentResolution);
    Serial.println(" bits");

    // Wait for 2 seconds before changing to the next resolution
    delay(1000);
  }

  // Delay before restarting the loop
  delay(1000);
}

Also, check the sensor datasheet.
I can't see how the bit resolution would improve readings.
Binary to decimal might be confusing for non integer values.

That depends on what you mean by "improve readings". The readings won't be more accurate, but there will be more measurable steps between two temperatures.

Which line of code sets the bit resolution?

    sensors.setResolution(resolutions[i]);

Post the results that are printed when you run the code posted in reply #5, which sets various different resolutions.

void loop(void) {
  // Define the resolutions to set (9, 10, 11, 12 bits)
  int resolutions[] = {9, 10, 11, 12}; // List of resolutions
  const int numResolutions = sizeof(resolutions) / sizeof(resolutions[0]); // Number of resolutions

  for (int i = 0; i < numResolutions; i++) {
    // Set the resolution
    sensors.setResolution(resolutions[i]);
    Serial.print("Resolution set to: ");
    Serial.print(resolutions[i]);
    Serial.println(" bits");

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.