Auto refilling fish tank

Hi all,
I am having trouble with my project,
I am trying to make an auto fish tank refiller for water using an Arduino Wifi Rev2.

The idea for the code is that there are two float switches that activate when the water reaches their level, they would be the upper and lower limit for the water level. When both switches are off the output connected to a relay, connected to a valve will actuate and fill up the tank. When both switches are on, the relay connected to the valve should turn off. An important detail is that when one of the switches are off and the other is on, no change should happen.
Attached below is the code and the wiring diagram, thanks in advance.

void setup() {

  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(7, OUTPUT);

}

void loop() {

  // put your main code here, to run repeatedly:
int LowSwitch = A0;
int HighSwitch = A1;
int Valve = 7;
  if ((LowSwitch>0.5) && (HighSwitch>0.5)) {

    digitalWrite(Valve, LOW);
  } else if ((LowSwitch<=0.5) && (HighSwitch<=0.5)) {

            digitalWrite(Valve, HIGH);
  }
   

}

Hello pattertwig26

Post the datasheet of the 'float switch' sensors used.

1 Like


int LowSwitch = A0;
int HighSwitch = A1;
int Valve = 7;
  if ((LowSwitch>0.5) && (HighSwitch>0.5)) {

    digitalWrite(Valve, LOW);
  } else if ((LowSwitch<=0.5) && (HighSwitch<=0.5)) {

            digitalWrite(Valve, HIGH);
  }

Two main problems.

  • LowSwitch>0.5 LowSwitch is equal to A0 not the analog value on the pin

  • You never actually read the voltage on A0


Why are you not using the pins as digital inputs :thinking: ?

1 Like

Hello pattertwig26

Wrt to data sheet these 'float switches' are simple on/off switches.
This sensor type will not provide any analog data.

Consider this small example to continue:

enum {One, Two}; // make names
constexpr uint8_t FloatSensor[] {A0, A1}; //declare pins used
constexpr uint8_t Valve{7}; //declare pins used
void setup()
{
  pinMode(FloatSensor[One], INPUT_PULLUP);  // connected to GND
  pinMode(FloatSensor[Two], INPUT_PULLUP);  // connected to GND
  pinMode(Valve, OUTPUT);                   // connected to +5V
}
void loop()
{
  // read current state of float sensor switches
  uint8_t floatSensorOne = digitalRead(FloatSensor[One]) ? LOW : HIGH;
  uint8_t floatSensorTwo = digitalRead(FloatSensor[Two]) ? LOW : HIGH;
  
  // make switch state
  uint8_t valveSwitchTo =(floatSensorOne xor floatSensorTwo)?LOW:HIGH;

  // control relay
  digitalWrite(Valve, valveSwitchTo);

} // end of application

  /*
    int LowSwitch = A0;
    int HighSwitch = A1;
    int Valve = 7;
    if ((LowSwitch > 0.5) && (HighSwitch > 0.5)) {
      digitalWrite(Valve, LOW);
    } else if ((LowSwitch <= 0.5) && (HighSwitch <= 0.5)) {
      digitalWrite(Valve, HIGH);
    }
    }
  */

Have a nice day and enjoy coding in C++.

Hi Paul thanks for the help,
I think I've done it wrong again though.
Below is the new wiring diagram I have and this time the relay is constantly on when both switches are open or closed but the relay switches off when one is closed and the other is opened.
I have directly uploaded your code without any changes.

Hello pattertwig26

Many thanks for your positve feedback.

Have a nice day and enjoy coding in C++.

Please show us a proper schematic.

Show us a good image of your ‘actual’ wiring.

Please give links to components.

Thanks for the help but I've figured it out now,
I think what was going wrong was how I was defining variables, how I was detecting voltage and switching over to using the digital pins.
Cheers again and the code that's working for me is attached below.

#define LowSwitch 3
#define HighSwitch 4
#define Valve 7

void setup() {

  pinMode(LowSwitch, INPUT_PULLUP);
  pinMode(HighSwitch, INPUT_PULLUP);
  pinMode(7, OUTPUT);
}

 
void loop() {

  if ((digitalRead(LowSwitch)==HIGH) && (digitalRead(HighSwitch)==HIGH)) {
    digitalWrite(Valve, LOW);

  }

  else if ((digitalRead(LowSwitch)==LOW) && (digitalRead(HighSwitch)==LOW)) {
    digitalWrite(Valve, HIGH);
  }
}
1 Like

You could do this more simply with a relay ( possibly 2?depending on what contacts are available) operating as a latch off the bottom float switch. Relay operates pump via contact on the top switch and the latch. When water reaches top , stops pump and breaks latch .

It can be instructive to look at logic circuits as alternatives

The analog pins do not have pull up or pull down resistors so the inputs will either float or be grounded. Consider adding pull up resistors.

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