Hello every one, [sorry for bad english], i have a problem with using a ds18b20 sensor and relay for my project (detect the temperature of water and pump hot or cold water), when i compile this code, it indicates that ds18b20 have some problems (-127.0 which means that i connect something wrong), but when i comment the part of code in which i call the pump to action, in serial monitor it looks like that all is ok, i mean without any changes in wiring it shows normal temperature.
i'm using arduino mega 2560, 2X relays, 2X pumps 12V and one waterproof ds18b20
Also: when i run this code, temperature sensor begin to warm
relay is connected in NC mode
#include <OneWire.h>
#include <DallasTemperature.h>
float limit = 30.0f;
#define ONE_WIRE_BUS 8
#define RELAY1 7
#define RELAY2 4
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
void setup(void)
{
Serial.begin(9600);
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
sensors.begin();
}
void loop(void)
{
sensors.requestTemperatures();
float current_temp = sensors.getTempCByIndex(0);
Serial.print("Temperature is: ");
Serial.println(current_temp);
if(current_temp > limit)
{
digitalWrite(RELAY2, LOW);
digitalWrite(RELAY1, HIGH);
}
if(current_temp < limit)
{
digitalWrite(RELAY1, LOW);
digitalWrite(RELAY2, HIGH);
}
// if current temp == limit do nothing
}
i attached how i connected the ds18b20