How to Measure and Record the Working Time of a DS18b20 Using Arduino Uno

I want to use the Uno board and a logic analyzer to analyze several timings of the DS18B20:

  • The time to initialize the DS18B20 and monitor the online information
  • The time to wait for the DS18B20 to release the bus
  • The time to trigger the temperature conversion
  • The time to send the read data command
  • The time to read the register value and obtain the temperature value

My wiring method is using D2 for transmission, and a 10K resistor is connected between VCC and DQ.

Below is my code, but the output results are always incorrect.

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

#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

DeviceAddress DS18B20_ID;

void setup() {
  Serial.begin(9600);

  unsigned long initStartTime = micros();

  sensors.begin();

  unsigned long initEndTime = micros();

  Serial.print("Initialization Time: ");
  Serial.print(initEndTime - initStartTime);
  Serial.println(" us");
}

void loop() {
  if (Serial.available() > 0) {
    String command = Serial.readStringUntil('\n');
    command.trim();

    if (command == "GET") {
      unsigned long waitStartTime = micros();

      delayMicroseconds(550);

      unsigned long waitEndTime = micros();

      unsigned long convStartTime = micros();

      sensors.requestTemperatures();

      unsigned long convEndTime = micros();

      unsigned long readCmdStartTime = micros();

      float temperature = sensors.getTempCByIndex(0);

      unsigned long readCmdEndTime = micros();

      unsigned long readRegStartTime = micros();

      Serial.print("Temperature: ");
      Serial.print(temperature);
      Serial.println(" C");

      unsigned long readRegEndTime = micros();

      Serial.print("Wait for Bus Release Time: ");
      Serial.print(waitEndTime - waitStartTime);
      Serial.println(" us");

      Serial.print("Trigger Conversion Time: ");
      Serial.print(convEndTime - convStartTime);
      Serial.println(" us");

      Serial.print("Send Read Command Time: ");
      Serial.print(readCmdEndTime - readCmdStartTime);
      Serial.println(" us");

      Serial.print("Read Register Value Time: ");
      Serial.print(readRegEndTime - readRegStartTime);
      Serial.println(" us");
    } else {
      Serial.println("Unknown command.");
    }
  }
}

I looked up the manuals and related articles online, and the data below are the correct ones.

data time (us)
The time to initialize the DS18B20 and monitor the online information 610
The time to wait for the DS18B20 to release the bus 550
The time to trigger the temperature conversion 887
The time to send the read data command 886
The time to read the register value and obtain the temperature value 560

Please help me.

Perhaps you don't realize that DS18b20 has it's OWN PROCESSOR and the times given are for the internal processor.

1 Like

Hello, may I ask how should I proceed?

Seems like you are doing just fine!

Are you mixed up us and ms ? The typical data conversion time of DS18B20 is hundreds milliseconds rather than microseconds

As about the measurements - in some cases you measured the time of outputting the data to the Serial instead of interface timings of sensor itself.

The following two time intervals have not been accounted for yet: one is the time taken for the bus release step, and the other is the time for two 16-bit data read instructions.

Ok got to ask … why do you want this information ???
Seems an odd thing to want to do - if timing is an issue there are non blocking methods to use the ds18b20

I purchased a batch of 18b20 sensors, approximately more than 300.
When using them, I found that the temperature readings are fine, but the conversion efficiency and time are not quite right.
I want to try to restore all the parameters related to time as specified in the datasheet.

As I said you can run them with non blocking code

  • start the conversion , go off do other stuff , come back and take the reading.

Unless you spent a good wedge of cash ( from Farnell they are £7 each plus tax) , you won’t have genuine chips , so would expect differences .
( eBay , Aliexpress etc etc) .
There is no fix for altering the time , so you won’t gain anything by measuring it .

( best price direct from manufacturers is £3.20 each for 250+ plus tax etc ).

There is a sketch for checking if they are genuine .

Look here

“ Hence, there will be some edge cases between Families A and B, but simply measuring the time used for temperature conversion will often be sufficient to determine if a sensor is counterfeit.”

Having said all that I’ve a bunch of counterfeits and they do non critical the task I ask of them !!

After looking at this project, I feel that it is not far from the answer I want, but I still want to ask more in-depth: How should I modify the code to obtain the time corresponding to the table below?


Thanks again for your answer, it helped me a lot.