Coding isue ? relay doesn't operate

Dear Arduino lovers ,

I have a limited coding experience, till today with time and reflexion I ever find how to move out with my project but this time I'm lost....
I can't operate the relay,after initialising it simply stay in ON state, could you help me to sort out ht is wrong with my bellow code ?
I wnated to activite the solar pump when S1 is above temperature of S2 else leave it OFF.

thank by advance


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

// Data wire is plugged into pin 13 on the Arduino
#define ONE_WIRE_BUS 13 // Arduino pin connected to DS18B20 sensor's DQ pin
#define precision 12 //onewire precision dallas sensor
//const int digPin = 12;

int sen_number = 0; //counter of Dallas sensors
int S1TempC, S2TempC, S3TempC, S4TempC;
int Ch_Pump = 6;   //Relay pump pin 

// flow measurement definition
int flowPin = 2;  //This is the input pin on the Arduino
double flowRate;  //This is the value we intend to calculate.
volatile int count; //This integer needs to be set as volatile to ensure it updates correctly during the interrupt process.


OneWire oneWire(ONE_WIRE_BUS);  // Setup a oneWire instance to communicate with devices
DallasTemperature sensors(&oneWire); // pass oneWire to DallasTemperature library
DeviceAddress T1, T2, T3, T4;// arrays to hold device adresses
//float tempCelsius;    // temperature in Celsius
//float tempFahrenheit; // temperature in Fahrenheit

void setup()
{
  Serial.begin(9600); // initialize serial



  // put your setup code here, to run once:
  pinMode(flowPin, INPUT);           //Sets the pin as an input
  attachInterrupt(0, Flow, RISING);  //Configures interrupt 0 (pin 2 on the Arduino Uno) to run the function "Flow"
pinMode(Ch_Pump, OUTPUT);
digitalWrite(Ch_Pump, LOW);
    sensors.begin();    // initialize the sensor
  
Serial.print("*Temp Sensor:* ");
Serial.print("Found: ");


Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" Devices.");


// report parasite power requirements
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
        
// Search for devices on the bus and assign based on an index.
if (!sensors.getAddress(T1, 0)) Serial.println("Not Found Sensor 1");
if (!sensors.getAddress(T2, 1)) Serial.println("Not Found Sensor 2");
if (!sensors.getAddress(T3, 2)) Serial.println("Not Found Sensor 3");
if (!sensors.getAddress(T4, 3)) Serial.println("Not Found Sensor 4");

//show the addresses we found on the bus
for (int k =0; k < sensors.getDeviceCount(); k++) {
Serial.print("Sensor "); Serial.print(k+1);
Serial.print(" Address: ");
if (k == 0) { printAddress(T1); Serial.println();}
 else if (k == 1) { printAddress(T2); Serial.println();}
 else if (k == 2) { printAddress(T3); Serial.println();}
 else if (k == 3) { printAddress(T4); Serial.println();}
}

// set the resolution to 12 bit per device
sensors.setResolution(T1, precision);
sensors.setResolution(T2, precision);
sensors.setResolution(T3, precision);
sensors.setResolution(T4, precision);

for (int k =0; k < sensors.getDeviceCount(); k++) {
Serial.print("Sensor "); Serial.print(k+1);
Serial.print(" Resolution(best=12 bit): ");
if (k == 0) { Serial.print(sensors.getResolution(T1), DEC); Serial.println();
} else if (k == 1) { Serial.print(sensors.getResolution(T2), DEC); Serial.println();
} else if (k == 2) { Serial.print(sensors.getResolution(T3), DEC); Serial.println();
} else if (k == 3) { Serial.print(sensors.getResolution(T4), DEC); Serial.println();
}
}
Serial.println("Flow sensor in use YF-S201 (1-30l/min)"); 
}


// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
//zero pad the address if necessary
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
float tempC = sensors.getTempC(deviceAddress);
Serial.print(",Temp.[°C]|[°F]:, ");
Serial.print(tempC);
//Serial.print("  °C  ");
Serial.print(" , ");        // separator between Celsius and Fahrenheit
Serial.print(DallasTemperature::toFahrenheit(tempC));
Serial.print(" , ");
//Serial.print("  °F  ");
}


// function to print a device's resolution
//void printResolution(DeviceAddress deviceAddress)
//{}


void printData(DeviceAddress deviceAddress)
{
Serial.print(" Address: ");
printAddress(deviceAddress);
Serial.print(" ");
printTemperature(deviceAddress);
//Serial.println();
}
 



void loop()
{
float S1TempC = sensors.getTempC(T1);
float S2TempC = sensors.getTempC(T2);
float S3TempC = sensors.getTempC(T3);
float S4TempC = sensors.getTempC(T4);
 digitalWrite(Ch_Pump, LOW);

  count = 0;      // Reset the counter so we start counting from 0 again
  interrupts();   //Enables interrupts on the Arduino
  
// call sensors.requestTemperatures() to issue a global temperature request to all devices on the bus
//Serial.print("Reading DATA..."); 
sensors.requestTemperatures(); 
//Serial.println("DONE");
// print the device information
for (int k =0; k < sensors.getDeviceCount(); k++) {
Serial.print("Sensor "); Serial.print(k+1); Serial.print(",");
if (k == 0) { printData(T1);}
 else if (k == 1) { printData(T2);}
 else if (k == 2) { printData(T3);}
 else if (k == 3) { printData(T4);}
}
if (sen_number == sensors.getDeviceCount()) {
sen_number = 0; // reset counter
}


// ***Relay Pump Managment***

//Serial.print("S1TempC: ");Serial.print(S1TempC);Serial.print(" ");
//Serial.print("S2TempC: ");Serial.print(S2TempC);Serial.print(" ");
//Serial.print("S3TempC: ");Serial.print(S3TempC);Serial.print(" "); // verif. fonc. variables SxTempC
//Serial.print("S4TempC: ");Serial.print(S4TempC);Serial.print(" ");



if ((S1TempC < S2TempC)) 
{
 digitalWrite(Ch_Pump, LOW);
Serial.print("Temp.: S1< S2 = PUMP OFF --> ");
}
else if(S1TempC > S2TempC) 
{
  digitalWrite(Ch_Pump, HIGH);
   Serial.print("Temp : S1 > S2 = PUMP ON --> ");
  }
}



//Serial.print("Sensor Number="); Serial.println(sen_number);
delay(30000); // give the time between each temp measure and used for rotation calculation --> 10.000 = 10sec
noInterrupts(); //Disable the interrupts on the Arduino

  //Start the math of Flow measurement
  flowRate = (count * 2.236);        //rotation determination : Take counted pulses(rotation) in the last "delay" period and multiply by 2.236mL (mean of info found over internet 2,22 2,5ml/rotation)
  flowRate = flowRate * 2;         //time conversion : Convert "delay period" to minutes, giving you mL / Minute
  flowRate = flowRate / 1000;       //Convert mL to Liters, giving you Liters / Minute


 //Flow measurement indication
Serial.print("Flow YF-S201 [l/min]:, ");  
Serial.println(flowRate);         //Print the variable flowRate to Serial in l/min
//Serial.print("  l/min");  
//Serial.println("  - normal pump flow 13,3l/min");  
//sen_number++ ;
}

void Flow()
{
   count++; //Every time this function is called, increment "count" by 1
}

Please post a wiring diagram, showing all the connections and components clearly labeled. A hand drawn picture is much preferred over Fritzing diagrams.

Both of these lines are really bad ideas, and need to go.

delay(30000); // give the time between each temp measure and used for rotation calculation --> 10.000 = 10sec
noInterrupts(); //Disable the interrupts on the Arduino

You should not manipulate the interrupts unless you know exactly what they are used for, and precisely why you need to turn them on or off.

Do you see reasonable values if you print them before testing them ?

is the relay being used capable of being driven by a digital output of an Arduino which has limited current capability?

Try using the blink example in the IDE and see if you can get the relay to just go on and off.

Once you fix this, learn about arrays.

Check to make sure the relay isn't turned off by HIGH and turned on by LOW this is counterintuitive but is probably one of your problems

Your code tests for t< and t> but NOT t= change < to <= and see if that helps.

+1.

Temporarily replace the relay with an LED and 1K series resistor to see if you digital output is changing according to the logic of your program.

Then try to operate the relay just placing 5 volts or GND at the point where you did have the digital output hooked up.

Then figure out your power supply issue if those two tests work…

DVQ - divide and conquer.

a7

I thank you all a lot for all your answers, great team Loved it.
I was on the field yesterday early in the morning (or late in the night) and unfortumatly haven't the chance to come back this week so I neeeded to finish the debug remotly (even more complicated)...

So I used the flowmeter and temp. probes that I allready know working -> debuged/tested earlier in the project by testing every fonction separatly.

I took your feedback one by one and thinked on what I can directly check and what would be needed to be checked on site.
As I allreay used a relay in previous project I had a strange felling on your answer @frugaltinker, I think I allready experienced it.
So I exchange HiGH<->LOW and check if on 5 sample the flowmeter get something.
for condition as pump OFF : It get only a very small value on 1st sample then nothing (relay init. most probably).
condition pump ON (simulated by high S1 probe temp value): I saw all line with a flow value as expected.
So finaliy my NO (normally open) relay output is due to this "high" "low" change a NC, I unfortunatly completly missed this option...

Once again many thank for your smart answer, lovely.

to conclude/sumarize with other points coming up :

  • script " noInterrupts()" on flowmeter : I took it from an exemple and saw it working so I keep it but yes agree with you I don't really know the meaning of that.
  • delay (30000) : I need it as I create a .txt file and plot it using Realterm and Kst2, as my system will need to be 24h/24h operating I needed to reduce the amount of line otherwise the system need a lot of ressources.
  • relay was working with the 5VDC, the relay is a standard from AZ Delivery the most typical one (I unfortunatly don't have the correct ref.) but was working fine with 5VDC.
  • "Arrays" I never used or saw this fonction but if it can help me to sort out some code I will definitly learn about it. Originaly I learned API Automation with boolean coding Arduino C++ sometime make me really bad.

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