I am creating a self watering indoor garden with a Raspberry Pi 3 B+, but for the way I'm building it, I need an accurate level detection. The water will be fed into this container, and then this pump will pump the water.

enter image description here

For different reasons, which I won't explain not to make this post long, I need to accurately detect when the water reaches two different levels. I tried this sensor connected to the MCP3008:

enter image description here

But it always returns different readings, so it's never accurate. I tried different codes, using the SPI and the normal GPIO, but it still returns a crazy reading. The ultrasonic sensor wouldn't work for me because this container will be closed and the water would touch it.

I read about using 2 wires, like on this trhead Detect water level and I think it would be ideal for this project, but how can I build that? I couldn't understand from he way he explained. Or is that sensor I'm using supposed to be accurate and I'm doing something wrong? I used these two ways of wiring and code:



I appreciate any help.


3 Answers 3


There are a lot of potential ways to do this. Based on this answer I'd suggest this:


simulate this circuit – Schematic created using CircuitLab

What's shown in the schematic as a 20M ohm resistor is actually just two wires which are about 1cm apart. They should be arranged so that they just touch the water when the water gets to the desired level. If you want two levels, build two circuits.

The 3.3V comes from the Raspberry Pi's power supply (the whole circuit consumes less than 0.5mA) and Vout is connected to a GPIO input pin on the Pi. A high voltage (logic 1 = 3.3V) indicates no water and a low voltage (logic 0 = 0V) indicates water.

  • This is pretty much what I was looking for. I actually stopped the project at this point. I will give it a go. Thanks for your answer.
    – Filcp
    Commented Nov 22, 2018 at 1:08

Roller Ball Tilt Switch

Perhaps you can make use of the SW-520D basic tilt switch which detects orientation.

Inside the can is a ball that make contact with the pins when the case is upright. Tilt the case over and the balls don’t touch, thus not making a connection.

You need to make some sort of lever with one end connected to the tile sensor and another end to a ball which floats and tilts the lever when the water level reaches a particular level.

Magnetic Tilt Switch

I never heard of this switch mentioned in the comment. So I googled to know more.

Float Level Switches & Sensors with Magnetic Reed Switches are accurate to 1/8 inch. When the liquid level rises or falls, a stationary sensor detects the change of the magnetic field of the floating magnet and the accuracy can be as small as 1/8 inch, ...

Mercury Tilt Switch

I read the Arduino Mercury Tilt Switch Tutorial and found mercury switch is very similar to roller ball switch. The disadvantages I know about mercury switch is that mercury is toxic and the glass container is not that strong comparing to the roller ball metal container.

  • 1
    Seems complex, why not just use a mercury switch?
    – Glen Yates
    Commented May 16, 2018 at 13:08
  • @GlenYates: Yes, I agree mercury switch is simple, cheap, and has no bouncing problem. The 2 metal balls thing is complicated, expensive, and bouncing. But I still like 2 metal balls. Why?
    – tlfong01
    Commented May 17, 2018 at 0:19
  1. Uncork a nice bottle of wine.
  2. Push a small magnet into the cork (remembering to consume the wine before it goes sour).
  3. Use hot melt glue to attach a reed switch on the outer surface (at a corner of the container) at each desired sense level.
  4. Wire the reed switches to inputs of your microcontroller.
  5. Place the cork inside the container at the corner where you attached the reed switches (with a baffle to constrain it to travel vertically at that corner without floating away).
  6. If this is unsuccessful, go back to step 1 and repeat as necessary.

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.