Temperature controller help, relays not switching

looking for some pointers with my sketch. im trying to switch 2 relays. one for a heater and one fora fan. i wish to set the temperature and set a differance so if the temperature cooler than the temp-differance the heater will come on and the reverse for the cooling.

i have temperature readout on the app but im not getting any relays switching when they should. i have spent a fair few hours trying to figure this out now with now sucess so some pointers would be greatly apriciated.
thanks in advance
Adam.

``cpp
}
BLYNK_WRITE(V11){
//reads the setppoint
SetPoint = param.asFloat();
}
BLYNK_WRITE(V12){
//reads the differential
Differential = param.asFloat();
}

From the snippet of code that you’ve posted it’s almost impossible to tell what the issue is.

Post your full code, along with details of the hardware you’re using, how you’ve wired your relays, whether the relays are active high or low, etc. etc.

Pete.

im using a mega with ethernet. one relay is a ssr the other is a sainsmart 16ch. i can switch the relays using a button in the app but my sketch wont control them

/*
  DS18B20multiple.ino https://community.blynk.cc/t/arduino-mega-2650-with-w5100-multiple-ds18b20/16706/10
  YourDuino Multiple DS18B20 Temperature Sensors on 1 wire
  Connections:
  DS18B20 Pinout (Left to Right, pins down, flat side toward you)
  - Left   = Ground
  - Center = Signal (Pin 2):  (with 3.3K to 4.7K resistor to +5 or 3.3 )
  - Right  = +5 or +3.3 V

   Questions: terry@yourduino.com 
   V1.01  01/17/2013 ...based on examples from Rik Kretzinger
   
/*-----( Import needed libraries )-----*/

#define BLYNK_PRINT Serial // Enables Serial Monitor
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h> // This part is for Ethernet stuff
#define W5100_CS  10
#define SDCARD_CS 4
BlynkTimer timer;
char auth[] = "d27bf6f0a098498a81a5c65dab79b851"; // Put your Auth Token here.
int Vpin = 5;  // will use Virtual pins 5, 6 and 7 for Sensor data

#include <OneWire.h> // Get 1-wire Library here: http://www.pjrc.com/teensy/td_libs_OneWire.html

//Get DallasTemperature Library here:  http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library
#include <DallasTemperature.h>

/*-----( Declare Constants and Pin Numbers )-----*/
#define ONE_WIRE_BUS 48// DS18B20 on arduino pin2 corresponds to D4 on physical board
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);


/*-----( Declare Variables )-----*/
// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

DeviceAddress Probe01 = { 0x28, 0xFF, 0x2D, 0x5A, 0xC4, 0x17, 0x05, 0xEF  }; 
float temp;
int relayPin = 49;
int relay2Pin = 28;
int desiredTemp = 0;// set this to your desired temp
int tempDiff = 1;// this variable provides a small +/- temp differential that will prevent constant relay switching.

/***************************************************
   Send Sensor data to Blynk
 **************************************************/


BLYNK_WRITE(V11){
  //reads the setppoint
  desiredTemp = param.asFloat();
}
BLYNK_WRITE(V12){
  //reads the differential
  tempDiff = param.asFloat();
}

void getSendData()
{
  DS18B20.requestTemperatures();
  temp = DS18B20.getTempCByIndex(0);
  Serial.println(temp);
  Blynk.virtualWrite(5, temp); //virtual pin V10


  temp = DS18B20.getTempCByIndex(1);
  Serial.println(temp);
  Blynk.virtualWrite(11, temp); //virtual pin V11

  temp = DS18B20.getTempCByIndex(2);
  Serial.println(temp);
  Blynk.virtualWrite(12, temp); //virtual pin V12
}

void Heater() {
  if (temp  <= desiredTemp - tempDiff ); {
    digitalWrite(relayPin, HIGH);
  }
  if (temp >= desiredTemp );
  digitalWrite(relayPin, LOW);
}
void Cooler() {
  if (temp >= desiredTemp - tempDiff );{
    digitalWrite(relay2Pin, HIGH);
  }
  if (temp <=desiredTemp );
  digitalWrite(relay2Pin, LOW);
  }

void setup()   /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(9600);  // start serial port to show results
  Serial.print("Initializing Temperature Control Library Version ");
  Serial.println(DALLASTEMPLIBVERSION);
  
  DS18B20.begin(); // Initialize the Temperature measurement library
  
  // set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
  DS18B20.setResolution(Probe01, 10);


  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
  Blynk.begin(auth);  // Here your Arduino connects to the Blynk Cloud.  
  timer.setInterval(1000L, myTimerEvent);  // Setup a function to be called every second
}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  Blynk.run(); // All the Blynk Magic happens here...
  timer.run(); // Initiates BlynkTimer   


}//--(end main loop )---

void myTimerEvent() {
  Serial.print("Number of Devices found on bus = ");  
  Serial.println(DS18B20.getDeviceCount());   
  Serial.print("Getting temperatures... ");  
  Serial.println();   
   
  DS18B20.requestTemperatures();  // Command all devices on bus to read temperature  
  
  Serial.print("Probe 01 temperature is:   ");
  printTemperature(Probe01); 
  Serial.println();


}

/*-----( Declare User-written Functions )-----*/
void printTemperature(DeviceAddress deviceAddress)
{
   
   float tempC = DS18B20.getTempC(deviceAddress);

   if (tempC == -127.00) 
   {
    Serial.print("Error getting temperature  ");
   } 
    else
   {
     Serial.print("C: ");
     Serial.print(tempC);
     Serial.print(" F: ");
     Serial.print(DallasTemperature::toFahrenheit(tempC));
     Blynk.virtualWrite(Vpin, tempC);
     }
  }// End printTemperature
//*********( THE END )***********

You need pinmode declarations in void setup for relayPin and Relay2Pin, declaring them as outputs, same as this:

Pete.

help pete and thankyou for your replay. iv made the change but still not woking :(. strangely the cooling relay turns on when i pwoer the arduino before it has connected to the server.

You’re not calling for getSendData() neither void Heater() nor void Cooler() therefore they’re not being executed.

You need to add a timer to them in your setup, something like:

timer.setInterval(5000L, getSendData);
timer.setInterval(1500L, Heater);
timer.setInterval(1300L, Cooler);
1 Like

what would i need to include in the getSendData(). thank you for your help

The way it is, should work ish, however I believe you copied the example from somewhere as you’re measuring several probes via DS18B20.getTempCByIndex(0); and assigning to the same variable temp it would cause problems down the line.

How many probes do you have?

just the one for now but plan to add a second at some point, code still dosnt run with the added timer intervals either :confused:

Yes, it won’t run because you only have 1 probe and it’s looking for 4x… Also assigning the same reading to the same variable.

What is happening? What you see on your Serial Monitor would give some hints.

Have a look around and fix your temperature reading code.

my serial monitor shows my temperature perfevtly fine. strangely my numerical inputs for the setpoint and differance get changed to-127

/*
  DS18B20multiple.ino https://community.blynk.cc/t/arduino-mega-2650-with-w5100-multiple-ds18b20/16706/10
  YourDuino Multiple DS18B20 Temperature Sensors on 1 wire
  Connections:
  DS18B20 Pinout (Left to Right, pins down, flat side toward you)
  - Left   = Ground
  - Center = Signal (Pin 2):  (with 3.3K to 4.7K resistor to +5 or 3.3 )
  - Right  = +5 or +3.3 V

   Questions: terry@yourduino.com 
   V1.01  01/17/2013 ...based on examples from Rik Kretzinger
   
/*-----( Import needed libraries )-----*/

#define BLYNK_PRINT Serial // Enables Serial Monitor
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h> // This part is for Ethernet stuff
#define W5100_CS  10
#define SDCARD_CS 4
BlynkTimer timer;
char auth[] = "d27bf6f0a098498a81a5c65dab79b851"; // Put your Auth Token here.
int Vpin = 5;  // will use Virtual pins 5, 6 and 7 for Sensor data

#include <OneWire.h> // Get 1-wire Library here: http://www.pjrc.com/teensy/td_libs_OneWire.html

//Get DallasTemperature Library here:  http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library
#include <DallasTemperature.h>

/*-----( Declare Constants and Pin Numbers )-----*/
#define ONE_WIRE_BUS 48// DS18B20 on arduino pin2 corresponds to D4 on physical board
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);


/*-----( Declare Variables )-----*/
// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

DeviceAddress Probe01 = { 0x28, 0xFF, 0x2D, 0x5A, 0xC4, 0x17, 0x05, 0xEF  }; 
float temp;
int relayPin = 49;
int relay2Pin = 28;
int desiredTemp = 26;// set this to your desired temp
int tempDiff = 1;// this variable provides a small +/- temp differential that will prevent constant relay switching.

/***************************************************
   Send Sensor data to Blynk
 **************************************************/


BLYNK_WRITE(V11){
  //reads the setppoint
  desiredTemp = param.asFloat();
}
BLYNK_WRITE(V12){
  //reads the differential
  tempDiff = param.asFloat();
}

void getSendData()
{
  DS18B20.requestTemperatures();
  temp = DS18B20.getTempCByIndex(0);
  Serial.println(temp);
  Blynk.virtualWrite(5, temp); //virtual pin V10


  temp = DS18B20.getTempCByIndex(1);
  Serial.println(temp);
  Blynk.virtualWrite(11, temp); //virtual pin V11

  temp = DS18B20.getTempCByIndex(2);
  Serial.println(temp);
  Blynk.virtualWrite(12, temp); //virtual pin V12
}

void Heater() {
  if (temp  <= desiredTemp - tempDiff ); {
    digitalWrite(relayPin, HIGH);
  }
  if (temp >= desiredTemp );
  digitalWrite(relayPin, LOW);
}
void Cooler() {
  if (temp >= desiredTemp - tempDiff );{
    digitalWrite(relay2Pin, HIGH);
  }
  if (temp <=desiredTemp );
  digitalWrite(relay2Pin, LOW);
  }

void setup()   /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(9600);  // start serial port to show results
  Serial.print("Initializing Temperature Control Library Version ");
  Serial.println(DALLASTEMPLIBVERSION);
  
  DS18B20.begin(); // Initialize the Temperature measurement library
  
  // set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
  DS18B20.setResolution(Probe01, 10);
pinMode(relayPin, OUTPUT);
pinMode(relay2Pin, OUTPUT);

  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
  Blynk.begin(auth);  // Here your Arduino connects to the Blynk Cloud.  
  timer.setInterval(1000L, myTimerEvent);  // Setup a function to be called every second
  timer.setInterval(5000L, getSendData);
timer.setInterval(1500L, Heater);
timer.setInterval(1300L, Cooler);
}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  Blynk.run(); // All the Blynk Magic happens here...
  timer.run(); // Initiates BlynkTimer   


}//--(end main loop )---

void myTimerEvent() {
  Serial.print("Number of Devices found on bus = ");  
  Serial.println(DS18B20.getDeviceCount());   
  Serial.print("Getting temperatures... ");  
  Serial.println();   
   
  DS18B20.requestTemperatures();  // Command all devices on bus to read temperature  
  
  Serial.print("Probe 01 temperature is:   ");
  printTemperature(Probe01); 
  Serial.println();


}

/*-----( Declare User-written Functions )-----*/
void printTemperature(DeviceAddress deviceAddress)
{
   
   float tempC = DS18B20.getTempC(deviceAddress);

   if (tempC == -127.00) 
   {
    Serial.print("Error getting temperature  ");
   } 
    else
   {
     Serial.print("C: ");
     Serial.print(tempC);
     Serial.print(" F: ");
     Serial.print(DallasTemperature::toFahrenheit(tempC));
     Blynk.virtualWrite(Vpin, tempC);
     }
  }// End printTemperature
//*********( THE END )***********

Post the latest code that you’re using.

What sort of output are you getting from your serial monitor?
I’d add some extra serial.print statements in your void Heater and Cooler functions that print the values of temp, desiredTemp and tempDiff so that you can see what’s happening.

Pete.

I’m sure your serial Monitor will show a funny number.

Also you have declared:

int desiredTemp = 26;
int tempDiff = 1;

And are putting a float value to it;

  desiredTemp = param.asFloat();
  tempDiff = param.asFloat();

I was hoping that the arduino would use those values should blynk disconnect so I don’t overheat or anything and I keep a stable temperature, I’ll remove the valves .

Ok I’ll add that now and report back

You don’t need to take them out, just need to declare them right.

Be careful with that, your code is far from stable and preventing from overheat. Try it several times before using it on “the real world”

I’d like to add alarms and notifications down the line. I know my code is far from safe for now

How would I declare them? I’m a little lost but knowing how this works will help me in my next step

Hello

relais Code with temp sensor

#define BLYNK_PRINT Serial

#include <TimeLib.h>
#include <SPI.h>
#include <Fishino.h>
#include <BlynkSimpleFishino.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);
float tempout = 0;
int setTemp = 0;

DallasTemperature sensors(&oneWire);
BlynkTimer timer;
char auth[] = "efd259060b91bccfb6";

int marko = 0;
void setup()
{
  // Debug console
  Serial.begin(9600);
  pinMode( buttonPin, INPUT);
  pinMode(9, OUTPUT);
  timer.setInterval(1000, Temperaturmessung);
sensors.begin();
  Blynk.begin(auth, "", "9831500923");
}
BLYNK_WRITE(V5) {
  marko = param.asInt();
  } 
void Temperaturmessung(){
     
  sensors.requestTemperatures();
  tempout = (sensors.getTempCByIndex(0));
  Serial.print (String ("Temperatur ist : ") + sensors.getTempCByIndex(0));
 Blynk.virtualWrite(V4, tempout);
  if(tempout < marko){ 
    digitalWrite(9, LOW);
  }
  else if(tempout > marko){ 
    digitalWrite(9, HIGH);
  } 
  }
void loop()
{
  Blynk.run();
  timer.run();
}

ok so here is where i am now .

/*
  DS18B20multiple.ino https://community.blynk.cc/t/arduino-mega-2650-with-w5100-multiple-ds18b20/16706/10
  YourDuino Multiple DS18B20 Temperature Sensors on 1 wire
  Connections:
  DS18B20 Pinout (Left to Right, pins down, flat side toward you)
  - Left   = Ground
  - Center = Signal (Pin 2):  (with 3.3K to 4.7K resistor to +5 or 3.3 )
  - Right  = +5 or +3.3 V

   Questions: terry@yourduino.com 
   V1.01  01/17/2013 ...based on examples from Rik Kretzinger
   
/*-----( Import needed libraries )-----*/

#define BLYNK_PRINT Serial // Enables Serial Monitor
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h> // This part is for Ethernet stuff
#define W5100_CS  10
#define SDCARD_CS 4
BlynkTimer timer;
char auth[] = "d27bf6f0a098498a81a5c65dab79b851"; // Put your Auth Token here.
int Vpin = 5;  // will use Virtual pins 5, 6 and 7 for Sensor data

#include <OneWire.h> // Get 1-wire Library here: http://www.pjrc.com/teensy/td_libs_OneWire.html

//Get DallasTemperature Library here:  http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library
#include <DallasTemperature.h>

/*-----( Declare Constants and Pin Numbers )-----*/
#define ONE_WIRE_BUS 48// DS18B20 on arduino pin2 corresponds to D4 on physical board
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);


/*-----( Declare Variables )-----*/
// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

DeviceAddress Probe01 = { 0x28, 0xFF, 0x2D, 0x5A, 0xC4, 0x17, 0x05, 0xEF  }; 
float temp;
int relayPin = 49;
int relay2Pin = 28;
float desiredTemp = 26;// set this to your desired temp
float tempDiff = 1;// this variable provides a small +/- temp differential that will prevent constant relay switching.

/***************************************************
   Send Sensor data to Blynk
 **************************************************/


BLYNK_WRITE(V11){
  //reads the setppoint
  desiredTemp = param.asFloat();
}
BLYNK_WRITE(V12){
  //reads the differential
  tempDiff = param.asFloat();
}

void getSendData()
{
  DS18B20.requestTemperatures();
  temp = DS18B20.getTempCByIndex(0);
  Serial.println(temp);
  Blynk.virtualWrite(5, temp); //virtual pin V10
}
void Heater() {
  if (temp  < desiredTemp - tempDiff ); {
    digitalWrite(relayPin, HIGH);
 Serial.print(desiredTemp);
 Serial.println(tempDiff);
  }
 
  if (temp > desiredTemp );
  digitalWrite(relayPin, LOW);
}
void Cooler() {
  if (temp > desiredTemp - tempDiff );{
    digitalWrite(relay2Pin, HIGH);
  }
  if (temp < desiredTemp );
  digitalWrite(relay2Pin, LOW);
  }

void setup()   /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(9600);  // start serial port to show results
  Serial.print("Initializing Temperature Control Library Version ");
  Serial.println(DALLASTEMPLIBVERSION);
  
  DS18B20.begin(); // Initialize the Temperature measurement library
  
  // set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
  DS18B20.setResolution(Probe01, 10);
pinMode(relayPin, OUTPUT);
pinMode(relay2Pin, OUTPUT);

  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
  Blynk.begin(auth);  // Here your Arduino connects to the Blynk Cloud.  
  timer.setInterval(1000L, myTimerEvent);  // Setup a function to be called every second
  timer.setInterval(5000L, getSendData);
timer.setInterval(1500L, Heater);
timer.setInterval(1300L, Cooler);
}//--(end setup )---

void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  Blynk.run(); // All the Blynk Magic happens here...
  timer.run(); // Initiates BlynkTimer   


}//--(end main loop )---

void myTimerEvent() {
  Serial.print("Number of Devices found on bus = ");  
  Serial.println(DS18B20.getDeviceCount());   
  Serial.print("Getting temperatures... ");  
  Serial.println();   
   
  DS18B20.requestTemperatures();  // Command all devices on bus to read temperature  
  
  Serial.print("Probe 01 temperature is:   ");
  printTemperature(Probe01); 
  Serial.println();


}

/*-----( Declare User-written Functions )-----*/
void printTemperature(DeviceAddress deviceAddress)
{
   
   float temperatureC = DS18B20.getTempC(deviceAddress);

   if (temperatureC == -127.00) 
   {
    Serial.print("Error getting temperature  ");
   } 
    else
   {
     Serial.print("C: ");
     Serial.print(temperatureC);
     Serial.print(" F: ");
     Serial.print(DallasTemperature::toFahrenheit(temperatureC));
     }
  }// End printTemperature
//*********( THE END )***********

iv added in some serialprints to help see what is going on in the monitor relay2 now says on but i think that may be because it might work in reverse being a sainsmart. But relay (my ssr0) flashes on and off. also in the serial monitor, it shows the requested data in the wrong format.

pic of serial monitor to follow.

many thanks.