Problem with DS18B20 output:

Dear all,
I'm trying to interface a DS18B20 one wire digital thermometer sensor with Arduino Due. I'm powering the sensor by an external power supply as recommended (not parasitic mode). Also I'm using the code/ program which was given in Arduino forum. I found some problem in the DS18B20 output, that is some dummy values ("-127") are coming in between the temperature.

The code and the output are given below. Anyone please suggest / guide me?

I want to interface this DS18B20 sensor with Arduino Due. Also please suggest me how to test/ read its output?

Code:
#include <OneWire.h>
#include <DallasTemperature.h>
#define I_TEMP 11
OneWire onewire11(I_TEMP);
DallasTemperature sensors11(&onewire11);

void setup(void)
{
// start serial port
Serial.begin(9600);
sensors11.begin();
}
void loop(void)
{
Serial.println("Requesting temperatures...");
sensors11.requestTemperatures();
Serial.println("");
Serial.print("I = ");
Serial.println(sensors11.getTempCByIndex(0));
Serial.println("");
}

Output Received:
I = 25.87

Requesting temperatures...

I = -127.00

Requesting temperatures...

I = 25.94

Requesting temperatures...

I = -127.00

Requesting temperatures...

I = -127.00

Requesting temperatures...

I = 25.94

Requesting temperatures...

I = -127.00

Requesting temperatures...

I = 25.94

Requesting temperatures...

I = -127.00

Requesting temperatures...

I = -127.00

Requesting temperatures...

I = 25.87

Requesting temperatures...

I = -127.00

Requesting temperatures...

I = 25.94

Requesting temperatures...

I = -127.00

Requesting temperatures...

I = -127.00

Requesting temperatures...

I = 25.94

Requesting temperatures...

I = -127.00

Requesting temperatures...

I = 25.94

Requesting temperatures...

I = -127.00

Requesting temperatures...

I = -127.00

Requesting temperatures...

I = 25.94

Requesting temperatures...

I = -127.00

Requesting temperatures...

I = 25.87

Requesting temperatures...

I = -127.00

Requesting temperatures...

I = -127.00

Requesting temperatures...

I = 25.87

Requesting temperatures...

I = -127.00

Requesting temperatures...

I = 25.75

Requesting temperatures...

I = -127.00

Requesting temperatures...

I = -127.00

Requesting temperatures...

I = 25.69

Requesting temperatures...

I = -127.00

Requesting temperatures...

I = 25.50

Requesting temperatures...

What happens if you add a delay of 2 seconds at the end of loop() ?
delay(2000);

The value -127 is a value indicating connection problems.

#define DEVICE_DISCONNECTED -127

As you have sometimes reasonable readings, the first thing I would check is the quality of the wiring,

  • have you connected the grounds of both power supplies?

  • do you use a pull up resistor?

I think you will find you are going too fast for the sensor to keep up.

As suggested (although not nice code !) put a delay of 1 sec or more at the end of the Loop.

Basically after taking a reading from a DS18B20 you have to wait a certain amount of time (dependant on whether it is a 9Bit or 12bit reading) before it is ready with an answer and can then respond to the next request.

Craig

The delay needed is built in the library (I worked on async version of the lib)

Can you measure with a DMM the voltage near the sensor?

how do you power the DS18B20?

How long is the cable between Arduino and sensor?

Is there a source of electrical noise nearby?

PLease answer the questions if you want help.

to make sure, that the code waits until the temperature conversion is finished, add this in your setup():

sensors11.setWaitForConversion(true);

are you using parasite power? i also had problems with this when i tried to read sensors over a longer ( > 5m) distance.
There is also an application note that says that it is not recommended to use parasite powered mode in field bus application.

David

robtillaart:
The delay needed is built in the library (I worked on async version of the lib)

Can you measure with a DMM the voltage near the sensor?

how do you power the DS18B20?

How long is the cable between Arduino and sensor?

Is there a source of electrical noise nearby?

PLease answer the questions if you want help.

Really Rob ? I did not know that - thanks for the heads up.

Yes i would point to cabling and/or resistance issues.

Time for a circuit schematic etc from the OP

Craig

First, I thank everyone... For your response.

The voltage at the pin DQ is 4.7V DC.

I powered the sensor with "external supply" mode not parasite powered.

The cable length between the Arduino and sensor is 2m.

No electrical noises nearby. I have powered the sensor from Arduino (+5V) and Arduino is powered with a 6v NiMH battery.

Yes i'm using a pull up resistor. The resistor used is a variable potentiometer. Rated 10K ohm. I set the resistance at 4.7K ohm.

I did not change or vary the delay. I will introduce it and let you know.

Thanks

jeyason:
The cable length between the Arduino and sensor is 2m.

No electrical noises nearby.

I did not change or vary the delay. I will introduce it and let you know.

If the sensor was supplied with cable it should be fine. If not, even 2m of poor unshielded cable can cause you grief. I had strife with a 1.2m cable extension in an ordinary suburban domestic environment.

Anything less than one second between readings can be a problem and I believe the 127 might be an indication of inability to read as well as no connection. I guess more to the point is to question why you would need more frequent readings anyway.

jeyason:
First, I thank everyone... For your response.

The voltage at the pin DQ is 4.7V DC.

I powered the sensor with "external supply" mode not parasite powered.

The cable length between the Arduino and sensor is 2m.

No electrical noises nearby. I have powered the sensor from Arduino (+5V) and Arduino is powered with a 6v NiMH battery.

Yes i'm using a pull up resistor. The resistor used is a variable potentiometer. Rated 10K ohm. I set the resistance at 4.7K ohm.

I did not change or vary the delay. I will introduce it and let you know.

Thanks

Try changing your resistance to 2.2K ohm - i know the literature all says 4.7K and i used to start with this but i find much more success with the 2.2K resisters

Craig

and Arduino is powered with a 6v NiMH battery.

you should at least use 7Volt

from - http://arduino.cc/en/Main/ArduinoBoardUno -
The board can operate on an external supply of 6 to 20 volts. If supplied with less than 7V, however, the 5V pin may supply less than five volts and the board may be unstable. If using more than 12V, the voltage regulator may overheat and damage the board. The recommended range is 7 to 12 volts.