ds18b20 only returns -127°C

Hi,
I use the following code for arduino UNO to use my ds18b20 sensor:

#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 11

OneWire ourWire(ONE_WIRE_BUS);
DallasTemperature sensors(&ourWire);

void setup()
{
delay(1000);
Serial.begin(115200);
Serial.println(“Misura la temperatura con sensore DS18B20”);
delay(1000);
sensors.begin();
}

void loop()
{
Serial.println();
sensors.requestTemperatures();
Serial.print(“Temperature = “);
Serial.print(sensors.getTempCByIndex(0));
Serial.println(” C degrees”);
delay(100);
}
but I constantly see on the screen “-127 C degrees”. I connect the sensor to GND, 5V and Pin 11. I tried two differents sensors and different Pins, so I guess the only problem could be in the code.
Hope someone can help, thank you

Welcome to the Arduino forum. I suspect you did not try the example in the IDE, nor did you read the documentation on the temperature sensor. This is code from my drying oven control program. Can you see the difference?

void check_current_Temp() {
  sensors.requestTemperatures();           // Send the command to get temperatures
  current_Temp = sensors.getTempCByIndex(0);
  running_Temp = current_Temp;             // use only the integer part of returned temperature value
  if (running_Temp >= max_Temperature) {  // have we met or exceeded the max temperature limit?
    turnOven_off();                       // yes!
    temp_Emergency = true;
    long_beep = true;
    reset_the_Parameters();
    show_Operator_message(5);
  }

Welcome to the forum

Can you please read the How to get the best out of this forum. Try to modify your original post to use code tags. It should look like the code from Paul.

I recommend you get trough some of the examples that come with the IDE, especially look at the BlinkWithoutDelay

File -> Examples -> 02.Digital -> BlinkWithoutDelay

This will be the best thing you can learn about Arduino now. The use of delay is going to bring you nothing but trouble soon.

Additionally, I recommend you avoid calling function inside other function calls for now. e.g.

Serial.print(sensors.getTempCByIndex(0));

You do not know what data type is returned by the get function and what data type the print function can take. In the long run, understanding what is going on will help you a lot.

-127 means "not connected". This has nothing to do with the code. EDIT!! Actually, I just might be wrong about that. Your lines

OneWire ourWire(ONE_WIRE_BUS); DallasTemperature sensors(&ourWire);

may be kosher, but why be different? Everybody else uses

OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire);

Do you have the required pull-up resistor on the input connected to the sensor?

Hi Lizz__17

I copy-pasted your code to my Arduino IDE the only change I made was to set Serial.begin (9600). An Arduino can't barely handle 115.200 bps

Miraculously it worked perfectly - even with the strange "DallasTemperature sensors(&ourWire);"

Note that I took an Arduino Nano, a stock DS18B20, powered the DS18B20 with 5V and placed a pullup 4.7 k resistor between Nano pin 11 and 5.5V.

In other words: your sketch is OK. Check your wiring maybe an interesting article on DS18B20 and Arduino is on https://thesolaruniverse.wordpress.com/2017/08/17/the-ds18b20-temperature-sensor-implementation-with-an-arduino/

Photoncatcher Succes

You might also check that your DS18B20 is kosher. There are a lot of fakes about and, if it looks like a transistor, it may indeed be one.

I would be a bit chary about that solar article. There is quite a lot of loose language in there.

Nick_Pyner: You might also check that your DS18B20 is kosher. There are a lot of fakes about and, if it looks like a transistor, it may indeed be one.

I bought 10 or more of the DS18b20's off ebay. Instead of being £5 each, they were £1 each, so I knew what I was letting myself in for. Surprisingly, they worked fine in most of my projects. However, in some projects, I initially had problems with the temperature readings and thought it was something to do with them being fakes. I downloaded a sketch from the following Chris Petrich github page:-

https://github.com/cpetrich/counterfeit_DS18B20/

The Chris Petrich sketch is great. He has done a lot of research and testing with the ds18b20s. The sketch reports if a sensor is a clone or not, and there is a lot of info about the devices. The sketch did in fact report that all my ebay sensors were clones/fakes, and that the one expensive RS Components device I had was kosher. In my case, it turned out that I had wiring and interference problems in the problem projects, and the fake clone sensors were in fact working ok. Steve