Need help with a basic script with a float switch and a relay.

Hello all,

I would like to build a simpje project (I think) but I can't figure out how to setup the script so it would be awesome if someone could help me with this.

What have I connected to my Arduino Uno:
Relay (ground, 5V, pin 7)
On/off float switch (connected between pin 8 and 5V with a 10k resistor between ground and pin 8 )

What I want it to do:
I want something like a timer (with seconds) to turn the relay on and off
for example:
Relay 36 seconds ON
Relay 1 hour 31 min 40 sec OFF
repeat
repeat

BUT the relay may only turn ON when the float switch is in the HIGH position.

Thanks in advance,

Michel

If you want to hire someone to write code, post in the Gigs and Collaborations sub forum. This forum is to get help with a project that you are undertaking yourself.

micheldb14:
What have I connected to my Arduino Uno:
Relay (ground, 5V, pin 7)

That right there doesn't sound like a good idea. Relays are usually driven by an intermediate transistor with some power capability. Got a _**clickable link **_to a part number/datasheet?

dougp:
That right there doesn't sound like a good idea. Relays are usually driven by an intermediate transistor with some power capability. Got a _**clickable link **_to a part number/datasheet?

It's just a basic arduino 4 Relay Module with GND,IN1,IN2,IN3,IN4,VCC
Part number on the board: 2ph63083A

aarg:
If you want to hire someone to write code, post in the Gigs and Collaborations sub forum. This forum is to get help with a project that you are undertaking yourself.

Oke, I didn't know that.
In that case I will start a new topic there.

This tutorial may be useful for you: Button control relay using Arduino

I have made a script that is working almost perfect.
When the float switch is up (LOW), the relay will just stay off (HIGH)
And when the float switch is in the lower position (HIGH) the relay will keep switching at the given time (now 10 min off and 30 sec on)

I only have one problem with this script. When I put the float switch in the lower position and instant back to the upper position the complete script will run (wait 10 min...turn relay on for 30 sec and turn off)

I want the float switch to work as an emergency swich. Which means when the float switch is going to its upper position (LOW) the relay should be off at all time.

Is this possible?

The script what I have right now:

int Relay1 = 7;
int FloatSwitch = 8;

void setup() {
Serial.begin(9600);
pinMode(FloatSwitch,INPUT_PULLUP);
pinMode(Relay1,OUTPUT);
}

void loop() {

if(digitalRead(FloatSwitch) == HIGH){
Serial.println("Level low");
digitalWrite(Relay1,HIGH);
delay(600000);
digitalWrite(Relay1,LOW);
delay(30000);
}
else{
Serial.println("Level high");
digitalWrite(Relay1,HIGH);
}

}

micheldb14:
Oke, I didn’t know that.
In that case I will start a new topic there.

No, please do not do that! Cross-posting (duplicates) is a no-no. If you really wanted someone to do it for you as a paid job, you can use the “report to moderator” to request it be moved, but by all means work on it here.

In the meantime, you need to go and read the forum instructions so that you can go back and modify your original post (not re-post it) - using the “More → Modify” option below the right hand corner of your post - to mark up your code as such using the “</>” icon in the posting window. Just highlight each section of code (or output if you need to post that) from the IDE and click the icon.

In fact, the IDE has a “copy for forum” link to put these markings on a highlighted block for you so you then just paste it here in a posting window. But even before doing that, don’t forget to use the “Auto-Format” (Ctrl-T) option first to make it easy to read. If you do not post it as “code” it can easily be quite garbled and is always more difficult to read due to the font.

It is inappropriate to attach it as a “.ino” file unless it is clearly too long to include in the post proper. People can usually see the mistakes directly and do not want to have to actually load it in their own IDE. And even that would also assume they are using a PC and have the IDE running on that PC.

Also tidy up your blank space. Do use blank lines, but only single blanks between complete functional blocks.

Whats the whole point of this forum? IMO a forum is a place to help people out with a problem.
I’m just doing a simple project where I need some help with…
But it seems this forum is just a place full of people who only want money.

I got a full mailbox of people telling me I can “hire” them for like 50 dollar for helping with my project… (which probably needs like 2 or 3 more lines)

For people with your knowledge it will probably take less time telling me whats wrong with my script than sending me a PM asking me to hire them…

I think this would be better with two floats wouldn't it? One at the top to say it's full, and the bottom to say it needs water.

If the bottom float is dry, turn on the pump. If top float is wet, turn the pump off.

So on day1 when the tank is new and empty, the bottom float is dry, pump goes on. Tank fills and fills, eventually top float is wet, pump off. Water gets used, top float gets dry: nothing happens since the bottom float is wet. Water gets used, gets used... eventually bottom float is dry, pump on.

blomcrestlight:
I think this would be better with two floats wouldn’t it? One at the top to say it’s full, and the bottom to say it needs water.

If the bottom float is dry, turn on the pump. If top float is wet, turn the pump off.

So on day1 when the tank is new and empty, the bottom float is dry, pump goes on. Tank fills and fills, eventually top float is wet, pump off. Water gets used, top float gets dry: nothing happens since the bottom float is wet. Water gets used, gets used… eventually bottom float is dry, pump on.

That is correct, but this means u will never have a failsafe when a floatswitch fails (or gets stuck) unless you are working with 3 float switches.

I have measured how many water evaporates and I will just pump this amount of water back in the aquarium. In my aquarium ± 15ml water evaporates every hour. So I just want the relay to turn on a peristaltic pump to pump this amount of water in the aquarium every hour.

And the float switch will be there as an savety feature when the water level gets to high the pump will stop turning on.

This method works way better than you can imagine but it will probably need some adjusting (winter/summer)

Ok, that makes sense.

So to clarify… we start with the tank full, and the float is wet. (The words high/low to mean both water level and a pin level is very confusing to me :wink: ).

Nothing happens, except we monitor the float. As long as it stays wet, do nothing. If we see the float is dry, we wait an hour (or 10 mins for testing purposes) to let enough water evaporate and run the pump for 30 seconds, UNLESS during the time the pump is running we see the float is wet (that would mean we didn’t get the expected evaporation?) in which case we turn the pump off.

I think the key here is to monitor the float and if it has become dry start the process, which means comparing its current state each time through loop to its state the previous time through loop.

blomcrestlight:
Ok, that makes sense.

So to clarify... we start with the tank full, and the float is wet. (The words high/low to mean both water level and a pin level is very confusing to me :wink: ).

Nothing happens, except we monitor the float. As long as it stays wet, do nothing. If we see the float is dry, we wait an hour (or 10 mins for testing purposes) to let enough water evaporate and run the pump for 30 seconds, UNLESS during the time the pump is running we see the float is wet (that would mean we didn't get the expected evaporation?) in which case we turn the pump off.

I think the key here is to monitor the float and if it has become dry start the process, which means comparing its current state each time through loop to its state the previous time through loop.

Thats correct.
And you are right, HIGH and LOW its all a bit confusing.

Float switch in upper position (high water level) = pin 8 LOW
Float switch in lower position (low water level) = pin 8 HIGH
On this way pin 8 reads LOW (high water level) when there is something wrong like a disconected float switch.

Relay1 HIGH = relay (pin 7) not powered
Relay1 LOW = relay (pin 7) powered
I have no idea why this is? I would normaly think HIGH means that it's powered but it's not in my case.

in normal state (if my evaperation calculations are right :cold_sweat: ) the float switch will always be in the lower position just floating above the water level.

In case I messed up my calculations and the water level will rise I want the floatswitch to stop the system.

I think I got the script sort of working right now with some extra safety checks build in.

int Relay1 = 7;
int FloatSwitch = 8;

void setup() {
  Serial.begin(9600);
  pinMode(FloatSwitch, INPUT_PULLUP);
  pinMode(Relay1, OUTPUT);
}

void loop() {
  if (digitalRead(FloatSwitch) == HIGH) {
    Serial.println("Level low");
    digitalWrite(Relay1, HIGH);
    Serial.println("Pump off");
    delay(900000);
    if (digitalRead(FloatSwitch) == HIGH) {
      digitalWrite(Relay1, LOW);
      Serial.println("Pump on");
      delay(60000);
    }
    else {
      digitalWrite(Relay1, HIGH);
      Serial.println("Level raised");
    }
  }
  else {
    digitalWrite(Relay1, HIGH);
    Serial.println("Level high");
  }
}

Actually I think what I said about it needing to have become dry is wrong. If there was abnormal evaporation, if the pump ran for normal time the float would still read dry, but would not have just become dry, it would still be dry, and the pump would never come back on to fill it.

So what's supposed to happen if the float is still dry after the pump has run? Should it be that if the float is dry, it waits the hour and pumps again?

Relay1 HIGH = relay (pin 7) not powered
Relay1 LOW = relay (pin 7) powered
I have no idea why this is? I would normaly think HIGH means that it's powered but it's not in my case.

Most relay modules work that way.

If you put this at the top of the sketch:

#define relayON LOW
#define relayOFF HIGH

... then you can use relayON or relayOFF in your digitalWrite()s. Means if you ever change to relay modules working the other way, or use leds for testing, you only have to change those lines at the top. Makes it more readable.

Float switch in upper position (high water level) = pin 8 LOW
Float switch in lower position (low water level) = pin 8 HIGH

Put these at the top:

#define WET LOW
#define DRY HIGH

... then use WET or DRY in your digitalRead()s.

I've re-written your code as a state machine.

Confirm this though before I post it (if you want it, that is):

So what's supposed to happen if the float is still dry after the pump has run? Should it be that if the float is dry, it waits the hour and pumps again?

I've written it so that if it's still dry after a pump, it waits again then pumps.

But at any time in the process, if the float says full, it stops.

blomcrestlight:
I've re-written your code as a state machine.

Confirm this though before I post it (if you want it, that is):

I've written it so that if it's still dry after a pump, it waits again then pumps.

But at any time in the process, if the float says full, it stops.

I have used the #define parts what you said and it made it a lot easier to read so thank you for that.
And yes the float switch should never touch the water. it's only for emergencies

This is the script I have now, I guess it will be somewhere the same as you have.

int Relay1 = 7;
int FloatSwitch = 8;
#define relayON LOW
#define relayOFF HIGH
#define WET LOW
#define DRY HIGH

void setup() {
  Serial.begin(9600);
  pinMode(FloatSwitch, INPUT_PULLUP);
  pinMode(Relay1, OUTPUT);
}

void loop() {
  if (digitalRead(FloatSwitch) == DRY) {
    Serial.println("Level low");
    digitalWrite(Relay1, relayOFF);
    Serial.println("Pump off");
    delay(900000);
    if (digitalRead(FloatSwitch) == DRY) {
      digitalWrite(Relay1, relayON);
      Serial.println("Pump on");
      delay(60000);
    }
    else {
      digitalWrite(Relay1, relayOFF);
      Serial.println("Level raised");
    }
  }
  else {
    digitalWrite(Relay1, relayOFF);
    Serial.println("Level high");
  }
}

Just struck me though, that there's no need for the timer if it's all delay()-less. Once the pump starts, just let it run until the float says wet.

And since you have the waiting time to let water evaporate, it won't be "hovering" at almost full switching the pump on and off rapidly.

So if the float reads dry, start the waiting clock, and after that, run the pump until the float says wet.

As I said before, I dont want that because when you use the floatswitch as a level indicator there is no failsafe. that is the only purpose of the floatswitch.

I think it will work out nice like I have it right now so I will do some testing now with the pump flow to figure out how many seconds the pump needs to pump.