DS18B20 speed problem

Hello every body,

I am using a DS18B20 temperature sensor with one wire arduino library on an arduino mega.

fisrt, i create the OneWire object on PIN 2

OneWire ds(2);

Then, i launch the aquisition to all DS18B20 on the bus : actually i have just one.

   ds.reset();            
   ds.skip();             
   ds.write(0x44);

Finally i read the sensor scratchpad:

float evaluate(OneWire* _ds, byte* _addr)
{
   float  _actualValue;
   Serial.print("P1 "); Serial.println(millis());
   _ds->reset();             
   _ds->select(_addr);        
   _ds->write(0xBE);        
 
    for (byte i = 0; i < 9; i++) 
        _data[i] = _ds->read();       
     
    if (OneWire::crc8(_data, 8) == _data[8]) 
        _actualValue = ((_data[1] << 8) | _data[0]) * 0.0625; 

    Serial.print("P2 "); Serial.println(millis());
    return _actualValue; 
}

the serial output is given below

P1 3066
P2 3920

I don’t undestand why it takes 900 ms to read the sensor. Do you have any idea ? Do you think i could reduce this time ?

Thanks

Presumably you have the device set for 12 bit precision. The datasheet says the conversion will take 750ms at that resolution. You've measured 854 - close enough. If you want to speed up the conversion you can change to a lower precision. At 9 bits the conversion time is 93.75ms.

Pete

There is always the option not to wait for the converted temperature but doing something else instead.

in pseudocode

void setup()
{
  ...
  requestNewTemperature();
}

void loop()
{
  if (newTemperatureAvailable())
  {
    t = getTemperature();
    requestNewTemperature();
  }
  // process t
 // and other things
}

I've written a similar asynchronous extension for the Dallas Temperature Control lib (3.7.x) that does just this. - https://github.com/milesburton/Arduino-Temperature-Control-Library -

Thank you for your reply !

it works

robtillaart: There is always the option not to wait for the converted temperature but doing something else instead.

in pseudocode

void setup()
{
  ...
  requestNewTemperature();
}

void loop() {   if (newTemperatureAvailable())   {     t = getTemperature();     requestNewTemperature();   }   // process t // and other things }





I've written a similar asynchronous extension for the Dallas Temperature Control lib (3.7.x) that does just this.
- https://github.com/milesburton/Arduino-Temperature-Control-Library -

Hi, I'm doing a similar project, can you tell me more about how to do it?

You might be a bit more specific about your project. The issue here is that the sensor takes time to do its job, up to 750ms, and it varies according to the resolution. Read the data sheet. Arduino can do other things in the meantime and, if you only want readings at intervals greater than 750ms, none of this is a problem. Having several sensors makes no difference either.

All you need is here http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html

In the unlikely event you need lightening-fast readings, you need a different sensor. You may even be demanding fast readings inadvertently, but no-one will know until you post the code.

kovil:

robtillaart:
There is always the option not to wait for the converted temperature but doing something else instead.

in pseudocode

void setup()

{
  …
  requestNewTemperature();
}

void loop()
{
  if (newTemperatureAvailable())
  {
    t = getTemperature();
    requestNewTemperature();
  }
  // process t
// and other things
}





I've written a similar asynchronous extension for the Dallas Temperature Control lib (3.7.x) that does just this.
- https://github.com/milesburton/Arduino-Temperature-Control-Library -

Hi, I’m doing a similar project, can you tell me more about how to do it?

as I said “… the Dallas Temperature Control lib (3.7.x) that does just this.”
Download the lib and check the waitforconversion2 example