Ds18b20 temperature sensor -127 error

Hi, I am working on a project. I am using ds18b20 temperature sensor and at first it was working well. After I tried it again, it reads only -127 degree celcius. This is my code, the conditions are for relay function. What could be the problem? Thank you.

#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 3

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

int delayCount = 200;
int printDelay = 5000;

//temperature
float Cs = 0;
float Fr = 0;

float Celsius = 0;
float Fahrenheit = 0;

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

pinMode(3, OUTPUT); //peltier
pinMode(4, OUTPUT); //cooler
}

void loop() {

compTemp();

}
//Water Temp Sensor
void compTemp()
{
Serial.print(Celsius);
Serial.print(" C ");
Serial.print(Fahrenheit);
Serial.println(" F");
delay(1000);

// Read Temperature
sensors.requestTemperatures();
Cs = sensors.getTempCByIndex(0);
Fr = sensors.toFahrenheit(Cs);

//Select if Celsius [Cs] or Fahrenheit [Fr]
float temp = Cs;

if (temp > 23)
{
digitalWrite(3, LOW);
digitalWrite(4, LOW);
delay(delayCount);
}

else if (temp < 12)
{
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
delay(delayCount);

}

Serial.print("Temp:");
Serial.print(temp, 1);
Serial.print("C");
}

Since it worked well at first, you can check again whether a cable has come loose or whether you have changed something in the circuit diagram.

@aizhelle
Please read the forum guidelines and isert the code using the code tags.

Celsius or Fahrenheit are always 0.
Do you have a pull-up resistor on the bus?

Are those the pins you are actually using for both the DS18B20 and the peltier cooler?

@david_2018 good catch.
Plenty wrong with the code.

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