# Level of a water tank with a microcontroller

How do i measure the level of a water tank with a microcontroller?

• This isnt worthy of as an answer but a friend of mine did this for an art project a couple years ago. Anyone interested I'd be happy to introduce you and I'm sure he would be happy to explain how he did it. His project is over here: unixarts.com/sealevel.html This combined both pumps and the sensors and required enough accuracy to loop up and down without too much skew Nov 30, 2009 at 23:25
• When the microcontroller stops working - it's underwater. Jan 18, 2013 at 13:06

Well, assuming you are operating here on earth (as opposed to in orbit), you can use a float sensor that you measure using the micro. The question is: Do you need to know the exact level, or do you just need some sort of trip when it gets to high/low?

On the 'trip at specific level' theme, you can get switches like this - generally the float contains some kind of attitude sensing switch. When the water level is lower, the float ends up on it's side, and the switch is 'off', while raising the level forces the float upright, and therefore 'on'. These are often used for sump pumps. They are pretty reliable and very easy to deal with, though you have to be careful to anchor them correctly, and you have to be sure that nothing gets in the way of the float.

If you need to know the actual level, then you've got lots of choices, but it's less simple. You can go with something like an ultrasonic sensor (ultrasound beam firing down from the top of the tank to reflect off the water level).

These guys seem to make level indicators for BIG tanks which consist of a float and an external weight cabled together. As the float rides up and down on the fluid, the external indicator moves appropriately. You could build something similar, with nothing more complicated than photo-cells to read the level (if it's a large tank, or you want lots of granularity, you'd need a lot of photo-cells).

Alternately, taking the cabled float idea, you could put an idler wheel that moves whenever the cable moves, and then attach an encoder to that. This would allow you to track the level with pretty high accuracy.

I know that the soon to be released book Practical Arduino has a water tank depth sensor project included, which uses a differential pressure transducer to measure the water pressure at the bottom of the tank, and from that to calculate how full the tank is.

Schematics are on the above link, along with a link to the source code on github.

(Full disclosure: I have no association with the book, but have met one of the authors a couple of times at our local hackerspaces.)

A method I like, but have not tried is to put two insulated plates in the liquid. No condution, electrolosis, contanimation....... They form the plates of a cap. and are used in some sort of oscillator ( choice up to you ) Because water is a good dialetric the frquency depends strongly on the amount of water between the plates. Meaure the frequency to get the depth.

You might be able to get a similar result by applying an ac signal and measureing the current thru the cap.

• Mind, it depends a lot on the dialectric constant of the fluid. Unless I'm mistaken, capacitive sensing is used more for detection of presence/absence than level. It seems just about every method I've run into is dependant on specific gravity, though. Tanks holding fluids of variable composition are tough to regulate. Nov 30, 2009 at 15:31
• The capacitance based liquid level sensors work best at detecting presence or absence of fluid. In order to read the water level charge/discharge the plates using a fixed resistor and measure the time. As mentioned above adding contaminates to the water (like salt) can cause a drastic change in capacitance and therefore your reading for a fixed liquid level. If the liquid you are measuring is homogeneous this is much less of a problem. Mar 22, 2010 at 19:49
• I made a dynatron oscillator with a simple off the shelf SMD coil that resonated with insulated plates like russ postulated.the sinewave was squared up and someboby else counted the pulses in firmware ,in other words what russ said DOES work. Sep 15, 2015 at 11:51

More details would help, but I think the ultrasonic method is probably the simplest, conceptually, anyway (hence the need for more details :-]). I threw together a level sensor for the fuel oil tank in my basement using a MaxSonar ultrasonic transducer. I'm switching to a Parallax Ping sensor, as soon as I can get my hands on one. The MaxSonar unit has proved to be somewhat of a pain; turns out it's only got a resolution of one inch (2.54cm), which equates to nearly 7 gallons in my tank. I put the MaxSonar unit in a PVC cap at the top of the tank pointing down (out of reach of the liquid).

One way of doing this is to place an led at the bottom of the tank pointing to the top, and a photodiode at the top of the tank, facing the LED. The LED will be attenuated by the water in the tank, you'll need to experiment to determine how much. Also, it's best to take a series of rapid measurements and average them when using this method.

• seems very prone to error, ambient light, sloshing, etc. Nov 30, 2009 at 12:54

I use a cheap stud sensor from the local hardware store with a photo transistor to sense the indicator led. With this glued to the side of the tank it senses when the level rises above the sensor and alerts us to a high water condition.

There are a surprising number of ways to check for level. There are sensors that use RF, sending a pulse down a waveguide and detecting the reflection off the surface of the liquid in the tank. There's ultrasound, floats, bubbler tubes, pressure taps... The method used depends on the size of the tank, the contents, ambient environment and other factors.

I'm surprised at all the complicated electronic solutions here, I would use a simple potentiometer. Most micro controllers have a basic analog i/p.

 +V
-----
|
|
/
\
/<----------> to analog i/p
\
|
|
-----
GND


Use a standard water tank valve float (there may already be one). The remaining problem is coupling the float to the pot to get maximum swing (you could also use a slider pot).

                  |-|
| |
|o| <--------Slider pot.
|||
|||
|
|  <--------Coupling.
|
____            |
(float)----------o-----o  <--Anchor point of float.
----

• How do you make sure the pot goes back to its original position?
– user41391
Aug 17, 2016 at 19:21
• The weight of the float will pull it down when water level drops...
– ttt
Aug 29, 2016 at 12:49
• Not so sure about that. Finding the correct weight of the float such that it floats in the presence of water and drops the pot in the absence of water is not so easy. Most pots have a fairly high static friction. I guess it is possible, however, given the right mechanical setup.
– user41391
Aug 30, 2016 at 21:32
• A lot of the longer slider pots have very low friction (used in finger-tip controlled mixers and DJ decks). I used a 4" slider from my bits box and tested it with a normal water tank float. It even worked on a smaller toilet cistern float! By the way, I would use a fixed resister in series with the pot so that even if it shorts in a wet environment it wont exceed the output of a controller port (or draw too much current from a fixed Vref). I used a 10k resister with a 10k pot in my test setup.
– ttt
Sep 1, 2016 at 13:06
• In testing I just used a piece of bent wire to latch on to the float arm. In the toilet cistern the arm is square-section plastic so it would be really easy to drill a small hole to make the attachment.
– ttt
Sep 1, 2016 at 13:15

Simplest approach

Place a small tube in a corner of the tank that is slightly larger in diameter than a ping pong ball.

Place an infrared LED on one side of the tube and a photoresistor opposite of the LED (by drilling a hole in the tube of it's opaque or placing on the outside if its transparent). Hot glue the electrical parts to waterproof them and drop the ping pong ball into the tube.

When the water level raises or lowers to the point that the ping pong ball breaks the infrared beam you'll know it hit the desired level. This works if you just need a discrete (on/off) level indicator.

This system is also used in paintball guns designed to prevent you from chopping balls by assuring that the whole ball is in the firing chamber before allowing the electronic trigger to arm the firing solenoid.

It's simple, effective, and requires little or no calibration.

One more clever approach: Make use of the bigger thermal conductivity of the water (fluid). The idea is to have temperature sensor and measure difference of its self heating when submerged and when in free air.

• This has actualy been done in CHCH NewZealand where I am ,but I didnt do it ,I think its about 20 years old.There was a row of many thermisters on the outside of the tank.So it works OK so you get my vote. Sep 15, 2015 at 11:59

Another solution (no pun intended);

Use a potentiometer as suggested above. The normal rotation range is 270 degrees. Attach a float to the potentiometer using a boom arm. (length=1 unit)

Between full and empty the boom arm will move 90 degrees.

The ADC on a PIC is either 256 or 1024 steps (yes, zero is a step).

I'll use 256 steps for clarity.

270 degrees = 256 ADC steps. 270/90 = 3 (one third of potentiometer range)

Program some code to mark the 0 degree point (tank empty) when a button is pushed.

This stores an offset point in the PIC eeprom. Now the potentiometer does not have to be exactly at zero because this calibration point can be set.

Using trigometry calculate a lookup table (hint: php script) to correspond to each step of the ADC.

Hint: each ADC step corresponds to 90/85 = 1.0588 degrees.

Yes, you should have paid more attention in maths class. dumb waste of time back then, indespensible now. engage brain. learn trigonometry. teach others.

Acronym : The Old Arab Carried A Heavy Sack Of Hay.

The hypontenuse is the length of the boom arm. Make it 1 unit long. Lookup table then provides percentage of tank depth. (multiply by 100 of course)