DS18B20 (getting constant 0C and 32F as result on serial monitor)

Here is my code:-

#include <OneWire.h> //Libraries to be included
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 2 //ONE_WIRE_BUS will take the input from data_pin 2

OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices

DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.

float Celcius=0;
float Fahrenheit=0;

void setup(void)
{
Serial.begin(9600);
sensors.begin();
}

void loop(void)
{
sensors.requestTemperatures();
delay(1000);
Celcius=sensors.getTempCByIndex(0);
Fahrenheit=sensors.toFahrenheit(Celcius);
Serial.print(" C ");
Serial.print(Celcius);
Serial.print(" F ");
Serial.println(Fahrenheit);

}

I am connecting the wires correctly. I don't understand why it shows 0C. Please help.

The code works fine on my Uno with 18B20 sensor. Do you have the required pullup resistor between 5V and the sensor signal output?

Serial monitor output:

 C  25.50 F  77.90
 C  25.50 F  77.90
 C  25.50 F  77.90
 C  25.50 F  77.90
 C  25.50 F  77.90
 C  25.50 F  77.90

I don't have a pull up resistor

"report to moderator" is not a valid reply option :grin:

The sensor will not work without the pullup. The resistor value is not critical. 10K to 2K should work. 4.7K is the recommended value.

The following diagram (Fig-1) explains the need of pull-up resistor for DS18B20 sensor to work.
dsq1.png
Figure-1:

dsq1.png