Measuring Volume of Water in a Tank Using Arduino and Ultrasonic Sensor

Introduction: Measuring Volume of Water in a Tank Using Arduino and Ultrasonic Sensor

This instructable outlines the process of measuring water volume in a tank using an Arduino and ultrasonic sensor. This is a quick and easy solution to approximate the water capacity remaining in a tank.

The following apparatus will be used for the purposes of this demonstration:

- Computer with Arduino IDE

- Arduino Nano

- HC-SR04 Ultrasonic Sensor

- Breadboard

- USB CABLE, 2.0 PLUG A-MINI B

- Water Tank

- Tank lid or clamp (for positioning Arduino)

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Physical Set Up

1. Insert the Arduino onto the breadboard

2. Connect the ultrasonic sensor as follows on the breadboard

- Ground wire -> GND input

- V+ wire -> Vin input

- Trigger wire -> D9

- Echo wire -> D10

3. Fill the tank with the desired amount of water.

4. Position the Ultrasonic sensor on the top of the water tank using a clamp or mounting apparatus of choice that ensures stability when in use (for accurate readings)

5. Connect the Arduino to the PC of choice running the Arduino IDE software using the USB cable

Step 2: Coding in the Arduino IDE

1. Copy and Paste the template code below into the IDE

// CODE START
//all volume quantities are in [cm^3]

const int trigPin = 9;

const int echoPin = 10;

long duration;

long distance;

float waterHeight;

float tankHeight = 52; //enter your water tank height here in [cm]

float totalVolume = 2613; // volume here is in [cm^3], change depending on tank volume

float waterQuantity;

float volume;

float pi = 3.14159265359;

float offset = 245;

void setup() { // put your setup code here, to run once:

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

Serial.begin(9600); }

void loop() {

// put your main code here, to run repeatedly:

digitalWrite(trigPin, LOW);

delayMicroseconds (2);

digitalWrite(trigPin, HIGH);

delayMicroseconds (10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = duration*0.034/2;

volume = pi*16*distance; //change based on the volume for the tank

waterHeight = tankHeight-distance;

waterQuantity = totalVolume-volume-offset;

Serial.print("Water level [cm]: ");

Serial.println(waterHeight);

Serial.print("Water Quantity [ml]: ");

Serial.println(waterQuantity);

delay(5000);

}

// CODE END

2. Click verify and upload the code to the Arduino (this may take a few moments)

3. Observe the code run

Verify the functionality of the code and the accuracy of the calculation by changing the level of water in the tank and reading it from the IDE results.

Step 3: Possible Sources of Error & Troubleshooting

Please note that although the volume measurement system using the Arduino and ultrasonic sensor is a low-cost solution to measure volume, possible sources of error may arise.

Measuring water capacity - possible errors

1. Vibrations in the container may cause the level of water to shift, despite the volume remaining the same, this may cause the ultrasonic sensor to give an inaccurate reading in the IDE.

2. Ensure that the volume of your tank is correctly entered into the IDE.

Troubleshooting - Arduino connections

1. Ensure when using the Arduino nano, that the correct configuration settings have been entered.

a. Select 'Tools' and select 'Board'. Choose the appropriate Arduino (in this case chose 'Arduino Nano')

b. Select 'Processor' under the 'Tools' menu and choose 'ATmega328P'

c. Again, under the 'Tools' menu, choose the appropriate port as connected via USB

d. Ensure that your 'Programmer' is set to 'AVRISP mkII'

If problems persist, try the following;

- Change your Arduino

- Update & restart your Arduino IDE

- Change your breadboard

- Reconnect your Arduino to the PC

Be the First to Share

    Recommendations

    • Magnets Challenge

      Magnets Challenge
    • Raspberry Pi Contest 2020

      Raspberry Pi Contest 2020
    • Wearables Contest

      Wearables Contest

    Discussions