# ds18b20 only returns -127°C

**URL:** <https://forum.arduino.cc/t/ds18b20-only-returns-127-c/693360>\
**Category:** Sensors\
**Created:** [January 27, 2021, 4:36pm UTC](https://forum.arduino.cc/t/ds18b20-only-returns-127-c/693360 "2021-01-27T16:36:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![lizz\_17](https://avatars.discourse-cdn.com/v4/letter/l/e5b9ba/32.png) [@lizz\_17](https://forum.arduino.cc/u/lizz_17)\
**Post date:** [January 27, 2021, 4:36pm UTC](https://forum.arduino.cc/t/ds18b20-only-returns-127-c/693360/1 "2021-01-27T16:36:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Paul\_KD7HB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_kd7hb/32/1127958_2.png) [@Paul\_KD7HB](https://forum.arduino.cc/u/Paul_KD7HB)\
**Post date:** [January 27, 2021, 4:47pm UTC](https://forum.arduino.cc/t/ds18b20-only-returns-127-c/693360/2 "2021-01-27T16:47:42Z")

</div>

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?

```cpp
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);
  }

```

---

<div class="post-metadata">

**Author:** ![Klaus\_K](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/klaus_k/32/626942_2.png) [@Klaus\_K](https://forum.arduino.cc/u/Klaus_K)\
**Post date:** [January 27, 2021, 5:30pm UTC](https://forum.arduino.cc/t/ds18b20-only-returns-127-c/693360/3 "2021-01-27T17:30:09Z")

</div>

Welcome to the forum

Can you please read the [How to get the best out of this forum](https://forum.arduino.cc/index.php?topic=712208.0). 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.

---

<div class="post-metadata">

**Author:** ![Nick\_Pyner](https://avatars.discourse-cdn.com/v4/letter/n/e8c25b/32.png) [@Nick\_Pyner](https://forum.arduino.cc/u/Nick_Pyner)\
**Post date:** [January 27, 2021, 11:51pm UTC](https://forum.arduino.cc/t/ds18b20-only-returns-127-c/693360/4 "2021-01-27T23:51:36Z")

</div>

-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);

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [January 28, 2021, 6:17am UTC](https://forum.arduino.cc/t/ds18b20-only-returns-127-c/693360/5 "2021-01-28T06:17:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![photoncatcher](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/photoncatcher/32/221050_2.png) [@photoncatcher](https://forum.arduino.cc/u/photoncatcher)\
**Post date:** [January 28, 2021, 9:30pm UTC](https://forum.arduino.cc/t/ds18b20-only-returns-127-c/693360/6 "2021-01-28T21:30:06Z")

</div>

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 [The DS18B20 temperature sensor – implementation with an Arduino – thesolaruniverse](https://thesolaruniverse.wordpress.com/2017/08/17/the-ds18b20-temperature-sensor-implementation-with-an-arduino/)

Photoncatcher  
Succes

---

<div class="post-metadata">

**Author:** ![Nick\_Pyner](https://avatars.discourse-cdn.com/v4/letter/n/e8c25b/32.png) [@Nick\_Pyner](https://forum.arduino.cc/u/Nick_Pyner)\
**Post date:** [January 29, 2021, 1:15am UTC](https://forum.arduino.cc/t/ds18b20-only-returns-127-c/693360/7 "2021-01-29T01:15:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![steve1001](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/steve1001/32/1098613_2.png) [@steve1001](https://forum.arduino.cc/u/steve1001)\
**Post date:** [January 29, 2021, 3:25am UTC](https://forum.arduino.cc/t/ds18b20-only-returns-127-c/693360/8 "2021-01-29T03:25:03Z")

</div>

> 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:-

> **[GitHub - cpetrich/counterfeit\_DS18B20: How to tell original from fake DS18B20...](https://github.com/cpetrich/counterfeit_DS18B20/)**
>
> How to tell original from fake DS18B20 temperature sensors. - GitHub - cpetrich/counterfeit\_DS18B20: How to tell original from fake DS18B20 temperature sensors.

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 29, 2021, 3:25am UTC](https://forum.arduino.cc/t/ds18b20-only-returns-127-c/693360/9 "2021-05-29T03:25:55Z")

</div>

Closed on May 29, 2021, 3:25am UTC

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.
