DS18B20 Temperature Sensor not working

I'm new to arduino and i'm trying to connect a ds18b20 temperature sensor to my arduino. My program is exactly the same as this video.

I'm using a Mega 2560 and I have the long waterproof temperature sensor cable. I've connected the black and the red wire's of the temperature sensor to ground of the arduino and the yellow wire to a digital pin 8 and also a pull up 4.7k resistor that connects to 5v of the arduino.
The serial monitor keeps showing me the temperature as -127. Weird thing is, while the program is running if i remove the temperature sensor it still shows me the value, seems its not detecting the sensor. Please help It's for my university project.

Thank you.

Can you post the code you are using? (use code tags)

The value of -127 comes probably from

#define DEVICE_DISCONNECTED_C -127 <<<<<< from DallasTemperature.h

at least if you are using the Dallas Temp Control Library. Can you confirm?

You can trace back the code in the library to the isConnected() call.

somehow the library does not connect to the device.

Think the black wire should be ground and the red to +5V ???

The code I’m using is

#include <OneWire.h>

#include <DallasTemperature.h>

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

void setup() {
  // put your setup code here, to run once:
sensors.begin();
Serial.begin(9600);
}
void loop() {
  // put your main code here, to run repeatedly:
  sensors.requestTemperatures();
  float currentTemp;
  currentTemp = sensors.getTempCByIndex(0);
  Serial.print("Temperature = ");
  Serial.println(currentTemp,2);
  
  delay(1000);
}

Actually, I did use the red wire as 5v before and the yellow as ground and it didn’t work either and had to google the right pin.

Thank you for your quick response.

code looks OK on first sight

Deep_Ak:
Actually, I did use the red wire as 5v before and the yellow as ground and it didn't work either and had to google the right pin.

You might have fried the sensor due to wrong wiring.

Sigh, are you sure? I doubt it cause even when I connected it wrong it was still showing me -127. Any other possible problem?

Deep_Ak:
I've connected the black and the red wire's of the temperature sensor to ground of the arduino and the yellow wire to a digital pin 8 and also a pull up 4.7k resistor that connects to 5v of the arduino.

If you have really connected it that way, go back and fix it. The 127 tells you it is a bad, or no, connection. You have probably not done any damage.

A decent tutorial is here

complete with wiring diagram.

I’ve connected the black and the red wire’s of the temperature sensor to ground of the arduino and the yellow wire to a digital pin 8 and also a pull up 4.7k resistor that connects to 5v of the arduino.

Actually, I did use the red wire as 5v before and the yellow as ground and it didn’t work either and had to google the right pin.

Clearly you miswired it.
(though neither of the ways you wired it is necessarily damaging) Damaging would be like reversing the red and black wires when you connect it.
see attached

waterproof sensor

400px-DS18B20_Hardware.jpg

1 Like

well, there maybe something wrong with your module, you can have a look at the temperature sensor, which it's used to make a project for scientific care for the plant in office to collect the temperature data of the environment. Maybe it can be your help.

Try a different sketch

Hi,
the sensor works but stops reading # 26, what can it be?

thx.

The DS18B20 is probably the easiest sensor to use that I have ever used.

Instead of the code you are currently using, try using the code in this tutorial

JoyTabella:
Hi,
the sensor works but stops reading # 26, what can it be?

thx.

You might need to put a bit more effort in explaining the issue. 'Stops reading #26' will be understood by nobody at the moment.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks… Tom… :slight_smile: