I am currently tracking water level in a container that will start a pump when it gets high.

To do this I use 3 wires one tapes to the bottom as a power wire and the other two taped to the wall at different depths to track low and high.

In my code I turn on the control, digital read both wires then turn off the control wire. This works okay but sometimes the readings are off displaying high when it is low.

My question is, is this the best way to track water in a container or are there other better ways?

  • Can you please describe the dimensions of the container, types of materials it is made out of and your budget? May 28, 2016 at 1:59
  • First is plastic and is 13liters 20x25x40cm and the second is rubber bitumen at about 70liters 130x60x40cm. My budget is fairly low but if I have to I can stretch it.
    – Dev-Mev
    May 28, 2016 at 2:01
  • can you please elaborate on what sensors you are currently using? May 28, 2016 at 2:17
  • I use 3 wires. 1: provide power to water taped to bottom of container. 2: will be low so it is 2 cm from bottom.3: is high which is taped to the top of the container. When I want to read I turn 1 on read 2 and 3 then turn 1 off.
    – Dev-Mev
    May 28, 2016 at 2:45
  • are you ok with some python code as an example? do you have any python experience? May 28, 2016 at 2:47

2 Answers 2


I would recommend that you use an ultrasonic sensor placed at the top of the container to measure the water level relative to the top of the container then you may subtract that from the overall height of the container to find the true water level.

Now to do this you would want to start off by selecting an ultrasonic sensor, I would personally recommend the HC-SR04 ultrasonic sensor because of its cost effectiveness and reliability. The sensor chosen should be mounted to the top of your container the container does not have a top then you should be ok, but if it does you will likely want to just drill 1 or 2 holes depending on your sensor type and have it mounted on top of the container to prevent humidity from rusting out the sensors circuits or worse creating a short circuit.

After you have the sensor mounted you will want to wire it up to the pi, depending on the sensor you have you may need a different circuit to connect it but for the one that I have recommended above, the instructions for connecting it are included below.

The HC-SR04 ultrasonic sensor has four pins ground, Echo, Trigger, and 5 volt in. The ground pin should be connected to the ground on your raspberry pi which is pin 6, now there are multiple ground's and 5volt lines on the pi and it really does not matter which one you choose to use. Now connect the 5 volt supply on the sensor to a 5 volt GPIO pin on your pi such as GPIO pin 2. Now connect the trigger pin on your sensor to a GPIO pin on the pi of your choice. Now since the sensor that we have chosen to use uses 5 volt logic but our pi's gpio pins will only accept 3.3 we will have to step-down the echo pins voltage but should be ok for the trigger as 3v3 should be enough to set the sensor off. Now for the echo pin on the sensor you will want to put a 1k resistor between the pin and a GPIO pin of your choosing on the pi, and the connect the same GPIO pin that you have just chosen to a 2k resistor and the other end of that resistor to a ground pin on your pi effectively creating a voltage divider.

Now onto software below is an example script on how water level would be measured.

import RPI.GPIO as GPIO
import time
triggerPin=1;#enter the gpio pin that you are using for the trigger here
echoPin=2;#enter the gpio pin that you are using for the echo here
## now setup the gpio pins as an input and an output
#now ensure that the trigger pin output is set to low and give the sensor a second to ensure that you do not get false positives
GPIO.output(triggerPin, False)
## now after sleeping for 2 seconds we want to run our trigger pin for the amount of time stated on the sensors manual which for the HC-SRo4 is 1 ten thousandths of a second
GPIO.output(triggerPin, True)
GPIO.output(triggerPin, False)
## now we want to start a timer to measure how long the response took
while GPIO.input(echoPin)==0:
while GPIO.input(echoPin)==1:
## now we want to multiply this value by the aproxamete speed of sound which is 34300 cm but divide that by two because our ultrasonic sensor is measuring the time taken both ways but we only want one.
## now we want to subtract the above number from the height of our container
bin_height=100## in cm
print distance_from_bottom
  • Great answer mate I will definitely do this for both my containers
    – Dev-Mev
    May 28, 2016 at 3:01
  • @Dev-Mev not a problem, hope you have a nice day my fine sir and good luck with your project. May 28, 2016 at 3:02

While @Mohammad Ali has a reasonable response I would like to advise this is not accurate enough to detect water levels in a container, the hc-sr04 sensor has a 1cm error rate and very spontaneous results.

I would highly recommend anyone looking to detect accurate water levels not to try this method.

Edit: I have tried this method for over a week and am not happy with the results, my original method provided much more accurate results.

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