About The Project
In this project, we will study how to measure the water level in a tank with a height of 20 cm using an ultrasonic sensor.
Ultrasonic Sensor (HC-SR04)
The working principle of an ultrasonic distance sensor is based on the echo of high-frequency sound (Ultrasonic Sound)waves.
- Sound Wave Emission: The sensor emits short pulses of ultrasonic sound waves (typically above the range of human hearing, around 40 kHz).
- Travel to Object: These sound waves travel through the air and bounce off any obstacle or object in their path.
- Echo Reception: The sensor then listens for the echoes of the sound waves reflected back from the object.
Time Calculation : By measuring the time interval between sending the sound pulse and receiving its echo, the Arduino Uno calculates the distance to the object using the formula: Distance = Speed of Sound ×Time interval between sending the sound pulse and receiving its echo/2.
Speed of Sound: The speed of sound wave in air is approximately 343 meters per second .
Water Level Calculation: The water level (in cm) is equal to the tank height (in cm) minus the distance (in cm). This is because the sensor is placed at the top of the tank.
This Arduino sketch demonstrates a basic implementation of interfacing with an HC-SR04 ultrasonic sensor to measure distances. It uses the time-of-flight method where the Arduino sends a trigger pulse and measures the time it takes for the pulse to return after bouncing off an object.
Programme Code
// www.matthewtechub.com
// leval transducer using ultrasonic sensor
// assume tank height is 20 cm
// use ploting shhet to reflect
int trigPin = 10; // TRIG pin
int echoPin = 7; // ECHO pin
int waterLevel;
int tankHeight =20;
float duration_us;
int distance_cm;
void setup() {
// begin serial port
Serial.begin (9600);
// configure the trigger pin to output mode
pinMode(trigPin, OUTPUT);
// configure the echo pin to input mode
pinMode(echoPin, INPUT);
}
void loop() {
// generate 10-microsecond pulse to TRIG pin
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// measure duration of pulse from ECHO pin
duration_us = pulseIn(echoPin, HIGH);
// calculate the distance from sensor to top level of water.
distance_cm = 0.017 * duration_us;
// calculate water level in the container.
waterLevel= tankHeight- distance_cm;
Serial.println(" Water Level");
Serial.println(waterLevel);
Serial.println(" cm");
delay(1000);
}
This code enables the Arduino to continuously measure and display the water level in a tank using an ultrasonic sensor. The sensor sends out an ultrasonic pulse, measures the time it takes for the echo to return, and then calculates the distance to the water surface. By subtracting this distance from the total tank height, the code determines the current water level.
Code Explanation
int trigPin = 10;
int echoPin = 7;
int waterLevel;
int tankHeight =20;
float duration_us;
int distance_cm;
- Variable Declarations:
- int trigPin = 10;: Defines the TRIG pin for the ultrasonic sensor, connected to pin 10 on the Arduino.
- int echoPin = 7;: Defines the ECHO pin for the ultrasonic sensor, connected to pin 7 on the Arduino.
- int waterLevel;: Variable to store the calculated water level in the tank.
- int tankHeight = 20;: Sets the height of the tank to 20 cm.
- float duration_us;: Variable to store the duration of the pulse in microseconds.
- int distance_cm;: Variable to store the calculated distance from the sensor to the water surface in centimeters.
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
- Setup Function:
- Serial.begin(9600);: Initializes serial communication at a baud rate of 9600 bps to enable data output to the serial monitor.
- pinMode(trigPin, OUTPUT);: Sets the TRIG pin as an output pin to send the ultrasonic pulse.
- pinMode(echoPin, INPUT);: Sets the ECHO pin as an input pin to receive the echo of the pulse.
void loop() {
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration_us = pulseIn(echoPin, HIGH);
distance_cm = 0.017 * duration_us;
waterLevel= tankHeight- distance_cm;
Serial.println(” Water Level”);
Serial.println(waterLevel);
Serial.println(” cm”);
delay(1000);
}
Loop Function:
- Generating a Pulse:
- digitalWrite(trigPin, HIGH);: Sets the TRIG pin high to generate a pulse.
- delayMicroseconds(10);: Waits for 10 microseconds.
- digitalWrite(trigPin, LOW);: Sets the TRIG pin low to end the pulse.
- Measuring the Echo:
- duration_us = pulseIn(echoPin, HIGH);: Measures the duration of the high pulse on the ECHO pin in microseconds. This duration corresponds to the time it takes for the ultrasonic pulse to travel to the water surface and back.
- Calculating the Distance:
- distance_cm = 0.017 * duration_us;: Converts the duration in microseconds to distance in centimeters. The factor 0.017 is derived from the speed of sound in air (approximately 343 meters per second).
- Calculating the Water Level:
- waterLevel = tankHeight – distance_cm;: Calculates the water level by subtracting the measured distance from the total tank height (20 cm).
- Displaying the Water Level:
- Serial.println(“Water Level”);: Prints the label “Water Level” to the serial monitor.
- Serial.println(waterLevel);: Prints the calculated water level to the serial monitor.
- Serial.println(” cm”);: Prints the unit “cm” to the serial monitor.
- Delay:
- delay(1000);: Waits for 1 second before repeating the loop. This creates a 1-second interval between each measurement and display of the water level.
Modify Yourself
Water surfaces can cause reflection and refraction of ultrasonic waves, potentially affecting the accuracy of measurements. Therefore, placing a floating sheet on the water and measuring the water level can help mitigate these issues.