I've created a sort of sensor to detect whether the water level in my aquarium is too high or too low.

Hardware part:

See this schematic for how it works:enter image description here

So the red part floats on top of the water, if the water is getting low it goes down, if the water is high it goes up. The black part is on the ground and never moves.

When the water is getting low, the red part (and thus purple) goes down connecting pin 21 to ground.

Software part:

If pin 21 is connected to ground I switch on a pump using a relay and when the water level has risen enough pin 17 gets connected to ground, and I switch off the pump.

The code I use to detect whether pin 21 or pin 17 is connected is of this form:

GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)

and in some loop I check:

isConnected = GPIO.input(17)

My problem:

This used to work well, but lately the pump doesn't switch off anymore when pin 17 gets connected.

What is wierd though is: when I manually push down the red part for a moment (breaking the connection of pin 17) and then let it connect again, the pump shuts off like it should.

Could this be a power issue? Or maybe I need a pull up/down resistor?

  • Who's to say? You are in the best position to troubleshoot the problem. Put some diagnostics in the code and run some tests. Are you sure it's not just corrosion causing poor contact?
    – joan
    Commented Jun 11, 2016 at 9:44
  • @joan basically I'm wondering if the way I set it up has any apparent flaws, and if perhaps the behavior of 'disconnecting for a moment to fix it' sounds familiar to anyone. I just checked for corrosion but I can't find anything like that Commented Jun 11, 2016 at 9:52
  • Your setup sounds perfectly reasonable to me (for what that is worth). You need to print out the GPIO states. If GPIO17 doesn't pull to ground when you expect it strongly suggests a faulty contact. If it does pull to ground but the pump doesn't stop that also helps pin-point the problem.
    – joan
    Commented Jun 11, 2016 at 9:55
  • Is the floater wet? If it is wet, electricity will flow thru the water, short circuiting your contacs.
    – Candid Moe
    Commented Jun 17, 2020 at 5:58

2 Answers 2


Probably your electrodes got dirty. For a quick solution, you can clean them, preferably with alcohol.

For a long term one, you can improve your design with a different sensor. E.g. use your floater to activate two fixed photo gates, or induction sensors/hall sensors with a magnet on the floater. Or even better: use an ultrasonic sensor to measure your water level exactly (not just the two end points).

  • Or a couple of humidity sensors hanging at the two desired water levels... Commented Jun 30, 2020 at 12:54

I don't have the hardware, so I'll just go ahead and give generic advise (in specific order):

  1. I recommend a pull-up on your pins (just Google if needed). Leaving them floating (pun intended) generally causes issues with random high/low signals, and sometimes gets stuck at that signal.

    • You can implement this in-software or in-hardware using pull-up resistors (nothing fancy, just a resistor between the GPIO pins and the 3V3 pin(s)).
  2. Check for corrosion.

  3. Check your Pi. If you have another one, try testing on that. If you don't, try using other pins.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.