Relay isn't staying on

Hi you all,

I am puzzeling with the next project.
I hooked up a toggle switch and 2 temp sensors to my arduino.
If the temp sensors get to an average of 21 degrees it switches on a relay.
However I need the relay to be manually switched on too.

I figured both out, seperatly form eachother. And this is working.
I got the sensors to turn on the relay, and I ve got the toggle switch hooked up and working.

However, when I add the 2 statements/programs together, the relay is just turning on and off al the time.
I tried to find the answer at this forum, but couldn't get it.

I power the arduino bij a 12V car battery, that is 'solarpanel charged' and this battery is also the powersuply for the load of the relay. Which is a magnatic velve. 12V

This is the code I written and combined.

ps: as you can see the temp is set to a value of 21 degrees. But when this temp is almost reached, the relay is also being tricked and switched on and off constantly - is there a simple line .. which helps to control the relay a bit more. some sort of bounderie?

Thanks for helping!

#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
#include <LiquidCrystal_I2C.h>

int switch_pin = 7;
int RELAY1 = 8;

DallasTemperature sensors(&oneWire);
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  
  sensors.begin();
  Serial.begin(9600);
  
  // lcd.begin();
  lcd.init ();
  lcd.clear();
  lcd.backlight();
  
  pinMode(switch_pin, INPUT);
  pinMode(RELAY1, OUTPUT);
  digitalWrite(RELAY1, LOW);

}

void loop() {
  if(digitalRead(switch_pin) == HIGH){
    digitalWrite(RELAY1, HIGH);
  }
  if(digitalRead(switch_pin) == LOW){
    digitalWrite(RELAY1, LOW);
  }

  sensors.requestTemperatures();
  float temp1C = sensors.getTempCByIndex(0);
  float temp2C = sensors.getTempCByIndex(1);
  int tempC = (int)(temp1C + temp2C) / 2;
  Serial.println("==================");
  Serial.println(tempC);
  //-----------------

 lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Temperature:");
  lcd.setCursor(0, 1);
  lcd.print("C:");
  lcd.setCursor(2, 1);
  lcd.print(sensors.getTempCByIndex(0));
  lcd.setCursor(10,1);
  lcd.print(sensors.getTempCByIndex(1));
 
  if (tempC > 21)
  {
    digitalWrite(RELAY1, HIGH);
    lcd.setCursor(13,0);
    lcd.print("ON");
  } else{
    digitalWrite(RELAY1, LOW);
    lcd.setCursor(13,0);
    lcd.print("OFF");
  }
  
  delay(2000);
}

Research the term "hysteresis".
C

You need to write your code so that if the switch is turned on then the sensors are not read and the switch becomes that master control.

Otherwise the switch may turn the relay on then immediately afterwards the sensor readings cause the relay to turn off

aahhhh righttt ofcourse...
yes! :sweat_smile: :sweat_smile:

however how do I do that?

if (the button is currently pressed)
{
  turn on the relay
}
else
{
  if (the temperature is in the right range)
  {
    turn on the relay
  }
  else
  {
    turn off the relay
  }
}
1 Like

Or perhaps you need another toggle to determine manual mode or automatic control.

oke.. and how do these work?
Do you have an example of such a switch?
..amazon...?

I'm suggesting that you use another one like your existing switch and use it to decide whether that switch controls the relay or whether the sensor does.

ohh like so.. I get it. Thnx!
good option, however when I get this problem done - I will be adding a third (float switch) to monitor the water level. And that one will be connected to the same relay/velve aswel.

lets see if I can get the suggestion of UKHeliBob work.

Hi,
How have you got your switch wired, do you use a pullup or pulldown resistor?

Can you please post a circuit diagram of your project, including power supplies, use a pen(cil) and paper and show an image of it would be fine.
Please do not use a Fritzy picture

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Hello Tom,

Sorry - I only am able to make Fritzy designs.
This is how I wired the toggle switch...

and the two temp sensors are wired by OneWire, so I only use one pin.

The powersupply is how I posted, done by a carbattery .... I bring the 12V down to 9V - for the input of the arduino - and use the 12V for the load of the relay.
I hope this helps you. ..

Because why do you ask this? To check the hardware? Or is there a difference in how a pull-up / down works for the code you need to make? Is it a hardware problem or a software/program problem?

It can be either, and so the more info you post, the more accurate the replies :slightly_smiling_face:

I would ask you to make a simple flow chart on a piece of paper regarding exactly how you want the logic to go.
For example, do you want to have a control switch that determines if the relay will come on at all in the first place, while leaving the monitoring system powered on? If so, there are two ways to do this.

  • software: read if the control switch is on, and allow other parts of the program to control the relay
  • hardware: better, put that switch in the 12V line going to the relay, so software cannot accidentally turn the relay on. No programming overhead

Then, you have to decide if the switch to turn on the relay manually can over-ride the sensors (temp, float-switch).

Once you draw out the flow chart, it will really help clarify how you should write the statements.

And don't forget to read up on hysteresis as per @camsysca. Analog readings coming in from sensors will always fluctuate, and you will end up turning things on and off in rapid succession if the hysteresis band is too small.
The float switch may present a different problem: bounce. Mechanical contacts often go on/off rapidly over a few mSec. There are compact routines that will help with this.

Your relay will thank you for doing both.

1 Like

@cncnotes thnx... also really helpfull!
I will look into it the next days and study your options.

1 Like

Hi,
Can you please post image(s) of your project.
We need to know how you have your hardware connected?

A hand drawn circuit would be appreciated?

What relay are you using?
Do you have a MOSFET or BJT to drive its coil?

Thanks... Tom... :smiley: :+1: :coffee: :australia:

But others cannot correct them.

Why on earth are you showing a crap fritz with pins 0 and 1 wired up ,
these are the UART aka Serial pins . :roll_eyes: :astonished:

Hello Cherk,

I didn't make this fritz design - I was just trying to find my way in how I should connect a toggle switch. And yes I do know these are the wrong pins - as you could have seen in my code I adjusted that one.

This is a start of how I connected everything

you must think a toggle on pin 7
sensors at pin 2

some overview

@cherk But I think my question is clear - the suggested solution too. Will you help me write the right code?

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