Reading Ds18b20 without delay

Good day, I want to know if there is a way to read ds18b20 without dallas library only with one wire library and without delay the sketch by using millis(), dallas library not working probably and when remove delay it freeze arduino over time, That why i need help in such a way to read from sensor without dallas library and delay, Best regards, Sayed..

dallas library not working probably

Most unlikely.

You can, using the library, read the temperature sensors synchronously, waiting for them to complete a reading, or asynchronously, being called back when the reading is complete.

The delay while waiting for the temperature sensor to take a reading is NOT the cause of your problem, whatever it is.

Where IS your code? You were supposed to post it!

Sayed: and without delay the sketch by using millis() [...]

millis() will not delay the sketch, unless you didn't apply it properly.

Sayed: Good day, I want to know if there is a way to read ds18b20 without dallas library only with one wire library and without delay the sketch by using millis(), dallas library not working probably and when remove delay it freeze arduino over time, That why i need help in such a way to read from sensor without dallas library and delay, Best regards, Sayed..

A DS18B20 is a OneWire sensor and the OneWire library is for OneWire Sensors, so it hould be fine to use the OneWire library with DS18B20 sensors.

About your " delay" and "freeze" telling: The sensor needs some time to do temperature conversion, i think 750 milliseconds approximately.

So your sketch will typically need two functions: 1.- request a temperature conversion from the sensor 2.- read the converted temperature value from the sensor

2nd function to be called 750ms (or later)= after the first one

Use the conversion time to do different things in your sketch, don't do "delay" or "busy waiting!!

sorry if some not understand my topic,
i didnt need any delay in the sketch code while reading the temperature from the sensor,
so i remove the 750 ms delay from dallas library, and using the following sketch
but over the time the sketch fail,

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
LiquidCrystal_I2C lcd(0x27, 20, 4);
double tempC = 0;               // Temperature Reading
unsigned long interval = 0;
DeviceAddress Thermometer;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); //(Remove all 'Serial' commands if not needed)
  Wire.begin();
  lcd.begin();        // initialize the lcd for 20 chars 4 lines, turn on backlight
  sensors.begin();
  sensors.getAddress(Thermometer, 0);
  pinMode(3, OUTPUT);
  digitalWrite(3, LOW);

  while (!Serial) {
    ;
  }

}

void loop() {
  // put your main code here, to run repeatedly:
  if (millis() > interval) {
    interval = millis() + 1000ul;

    lcd.setCursor(1, 1);
    lcd.print("Temperature:");
    sensors.requestTemperatures();
    tempC = sensors.getTempC(Thermometer);
    lcd.print(tempC);

  }
  if (tempC < 37.6) {
    digitalWrite(3, LOW);
  } else if(tempC >= 37.7) {
    digitalWrite(3, HIGH);
  }
}

then i think to remove the dallas library,

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
OneWire  ds(2);
LiquidCrystal_I2C lcd(0x27, 20, 4);
double tempC = 0;               // Temperature Reading
unsigned long interval = 0;
unsigned int tempS = 0;
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); //(Remove all 'Serial' commands if not needed)
  Wire.begin();
  lcd.begin();        // initialize the lcd for 20 chars 4 lines, turn on backlight
  pinMode(3, OUTPUT);
  digitalWrite(3, LOW);

  while (!Serial) {
    ;
  }

}

void loop() {
  // put your main code here, to run repeatedly:


  if (tempS == 0 && millis() > interval ) {
    ds.reset();
    ds.select(addr);
    ds.write(0x44, 1);        // start conversion, with parasite power on at the end
    tempS = 1;

  } else if (tempS == 1 && millis() > interval) {
    // we might do a ds.depower() here, but the reset will take care of it.
    tempS = 0;
    ds.depower();
    present = ds.reset();
    ds.select(addr);
    ds.write(0xBE);         // Read Scratchpad
    for ( i = 0; i < 9; i++) {           // we need 9 bytes
      data[i] = ds.read();
    }
    int16_t raw = (data[1] << 8) | data[0];
    if (type_s) {
      raw = raw << 3; // 9 bit resolution default
      if (data[7] == 0x10) {
        // "count remain" gives full 12 bit resolution
        raw = (raw & 0xFFF0) + 12 - data[6];
      }
    } else {
      byte cfg = (data[4] & 0x60);
      // at lower res, the low bits are undefined, so let's zero them
      if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
      else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
      else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
      //// default is 12 bit resolution, 750 ms conversion time
    }
    celsius = (float)raw / 16.0;
    if (tempC > (celsius + 0.5) || tempC < (celsius - 0.5)) {

    tempC = celsius;
  } else {
    tempC = celsius;

    lcd.setCursor(1, 1);
      lcd.print("Temperature:");
      lcd.print(tempC);

      if (tempC < 37.6) {
        digitalWrite(3, LOW);
      } else if (tempC >= 37.7) {
        digitalWrite(3, HIGH);
      }
    }

  }
}

the problem with last code that some time the reading is not correct and fluctuate so i add like filter for controlling the temperature, if temperature fluctuation more than 0.5 or less than the last reading the control of pin 3 will disable, the problem is that temperature some time reach more than setpoint by about 2 degree and some time become just plus or minus the setpoint by 0.2 degree C

i didnt need any delay in the sketch code while reading the temperature from the sensor,
so i remove the 750 ms delay from dallas library

The sensor does, though, so removing the delay is the wrong thing to do.

Maybe this will help?

http://www.scargill.net/reading-dallas-ds18b20-chips-quickly/

PaulS: The sensor does, though, so removing the delay is the wrong thing to do.

i remove the delay from Dallas library but i keep using millis() in the code to wait 750ms if you could look at the code.

  if (millis() > interval) {
    interval = millis() + 1000ul;

    lcd.setCursor(1, 1);
    lcd.print("Temperature:");
    sensors.requestTemperatures();
    tempC = sensors.getTempC(Thermometer);
    lcd.print(tempC);

  }

i am trying to get the reading without delay my sketch

outsider: Maybe this will help?

http://www.scargill.net/reading-dallas-ds18b20-chips-quickly/

he use delay again, what i need is the use of millis for timing for reading of temperature instead of delay in order to keep sketch running without any delay,

Your problem is that you are using only the integrated query and response version of the temperature read. In the library, there are two separate functions, one to query and one to collect the response. You are presently not using them. Hence no amount of time manipulation will help you.

not understand, you mean it is not possible to use this sensor without stopping the sketch by delay?

you mean it is not possible to use this sensor without stopping the sketch by delay?

NO!. It is possible to use the sensor without using delay. But, it takes time between when you tell the sensor that you want to take a reading and when the sensor has data available. You have two choices. You can ask the sensor what the temperature is, and then wait for it to have an answer.

Or, you can tell the sensor that you need to know the temperature, and to please call you when it knows.

With the second approach, you can be doing other things, and the library will call you (execute some code of yours) when it has an answer.

But, you can NOT insist that it take 0 time to get data to you.

Sayed: not understand, you mean it is not possible to use this sensor without stopping the sketch by delay?

I think we're getting confused over words here. The sensor hardware has an inherent delay. You can't change that. But you have a choice about what you do while it executes its delay. You can sit and do nothing while you wait for a result, or you can resume your processing and come back and collect the result later, when it is ready.

This is just another expression of what PaulS said above, I just thought saying it twice would make it twice as likely to be understood.

The difference is in which methods you call the library with. It has methods for both approaches. You have to choose the one you want.