Tank Level Monitoring

Whether in food production or fuel/water usage, its good to know the volume of a liquid is in a tank. You may be surprised to learn you can do this with a Sonar Phidget! By mounting the sensor looking into the top of the tank, you can measure the distance to the tank's contents. If you know the dimension of the tank, you can calculate the volume.

Setup

Before getting started, make sure you have the following parts.

Sonar Phidget

USB cable

 

Container *

Phidget cable

 

Rice **

NOTE:
* Depending on the size of your container you may meed some cardboard/tape to secure the Sonar Phidget.
** Rice is easier to use than a liquid in the classroom because you do not need to worry about the Phidget or computers getting wet

Step 1

Attach the Sonar Phidget to the VINT Hub and the VINT Hub to your computer

Step 2

Cut a hole in cardboard big enough so the full sensor is exposed, and attach to the sensor.

Step 3

Place the sensor on top of container (do not secure the cardboard as it needs to be mobile so you can add rice to the container)

Write code (Java)

Copy the code below into a new Java project. If you need a reminder of how to do this, revisit the Getting Started Course. Insert the code below.

Not your programming language? Set your preferences so we can display relevant code examples

  
//Add Phidgets Library
import com.phidget22.*;

public class TankLevel {
    public static void main(String[] args) throws Exception {

        //Create
        DistanceSensor distanceSensor = new DistanceSensor();

        //Open
        distanceSensor.open(1000);

        //Use your Phidgets
        while (true) {
            System.out.println("Distance: " + distanceSensor.getDistance() + " mm");
            Thread.sleep(150);
        }
    }
}
  
  
package tanklevel;

//Add Phidgets Library
import com.phidget22.*;

public class TankLevel {
    public static void main(String[] args) throws Exception {

        //Create
        DistanceSensor distanceSensor = new DistanceSensor();

        //Open
        distanceSensor.open(1000);

        //Use your Phidgets
        while (true) {
            System.out.println("Distance: " + distanceSensor.getDistance() + " mm");
            Thread.sleep(150);
        }
    }
}
  

Write code (Python)

Copy the code below into a new Python project. If you need a reminder of how to do this, revisit the Getting Started Course. Insert the code below.

Not your programming language? Set your preferences so we can display relevant code examples

  
#Add Phidgets Library
from Phidget22.Phidget import *
from Phidget22.Devices.DistanceSensor import *
#Required for sleep statement
import time

#Create
distanceSensor = DistanceSensor()

#Open
distanceSensor.openWaitForAttachment(1000)

#Use your Phidgets
while (True):
    print("Distance: " + str(distanceSensor.getDistance()) + " mm")
    time.sleep(0.15)
  

Write code (C#)

Copy the code below into a new C# project. If you need a reminder of how to do this, revisit the Getting Started Course. Insert the code below.

Not your programming language? Set your preferences so we can display relevant code examples

  
//Add Phidgets Library
using Phidget22;

namespace TankLevel{
    class Program{
        static void Main(string[] args){

            //Create
            DistanceSensor distanceSensor = new DistanceSensor();

            //Open
            distanceSensor.Open(1000);

            //Use your Phidgets
            while(true){
                System.Console.WriteLine("Distance: " + distanceSensor.Distance + " mm");
                System.Threading.Thread.Sleep(150);
            }
        }
    }
}
  

Write code (Swift)

Copy the code below into a new Swift project. If you need a reminder of how to do this, revisit the Getting Started Course. Insert the code below.

Not your programming language? Set your preferences so we can display relevant code examples

  
import Cocoa
import Phidget22Swift

class ViewController: NSViewController {

    @IBOutlet weak var distanceLabel: NSTextField!
    
    //Create
    let distanceSensor = DistanceSensor()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        do{
            //Subscribe to event
            let _ = distanceSensor.distanceChange.addHandler(distance_change)
            
            //Open
            try distanceSensor.open()
            
        } catch {
                 print(error)
        }
    }
    
    func distance_change(sender:DistanceSensor, distance: UInt32) {
      DispatchQueue.main.async {
        // Use information from your Phidget to change label
        self.distanceLabel.stringValue = String(distance) + " mm"
      }
    }
}
  

Try running your program. You will see the distance from the sensor to the top of the "liquid" printed.

Practice

  1. Modify the code to print the height of the "liquid" (rice) in the container.
  2. Create alerts if the levels approach the top of the container and when the levels are nearing empty.

What are Phidgets?

Phidgets are programmable USB sensors. Simply plug in your sensor, write code in your favorite language and go!

Phidgets have been used by STEM professionals for over 20 years and are now available to students.

Learn more

Set your preferences

Windows

Mac OS

Raspberry Pi

Java

Python

C#

Swift

NetBeans

Processing

Eclipse

Thonny

PyCharm

PyScripter

Visual Studio

Xcode

Setting your preferred operating system, programming language and environment lets us display relevant code samples for the Getting Started Tutorial, Device Tutorials and Projects

Done