# Accurate water level

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.

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:

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:

http://kookye.com/2017/06/01/%E5%9F%BA%E4%BA%8E%E6%A0%91%E8%8E%93%E6%B4%BE%E7%9A%84%E6%B0%B4%E4%BD%8D%E6%BA%A2%E5%87%BA%E6%A3%80%E6%B5%8B%E5%99%A8/

I appreciate any help.

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. 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.

• Seems complex, why not just use a mercury switch? 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? 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.