Simple DIY project using HC-SR04 Ultrasonic Sensor

You can create a circuit to measure the water level of a water tank using this HC-SR04 Ultrasonic Sensor. You only have to add few lines in to the code that I used to display measured distance from this sensor. Revised code is given below.
int echoPin = 2;  //Define Echo pin
int trigPin = 3;     // Define Trig pin
long  inches, duration, cm;   //Define variables
int height=300; //define depth of tank in cm
int firstLED = 4; // define LEDs pin
int secondLED = 5;
int thirdLED = 6;
int fourthLED = 7;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);  //Define output pin
pinMode(firstLED, OUTPUT);
pinMode(secondLED, OUTPUT);
pinMode(thirdLED, OUTPUT);
pinMode(fourthLED, OUTPUT);
pinMode(echoPin, INPUT);  //Define input pin
}

void loop()
{
//Give a short low pulse  to trigPin for ensure a clean high pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
//give high pulse to trigPin 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor
duration = pulseIn(echoPin, HIGH);
// convert time into a distance
// The speed of sound is 29.1 microseconds per centimeter.
cm = (duration/2) / 29.1;
//Print distance
Serial.print(cm);
Serial.print(“cm”);
Serial.println();
if (cm >= (3*height/4)) {
    digitalWrite(firstLED, HIGH);
    digitalWrite(secondLED, LOW);
    digitalWrite(thirdLED, LOW);
    digitalWrite(fourthLED, LOW);
  }
else if(cm >= (height/2)){
    digitalWrite(firstLED, HIGH);
    digitalWrite(secondLED, HIGH);
    digitalWrite(thirdLED, LOW);
    digitalWrite(fourthLED, LOW);
  }
else if(cm >= (height/4)){
    digitalWrite(firstLED, HIGH);
    digitalWrite(secondLED, HIGH);
    digitalWrite(thirdLED, HIGH);
    digitalWrite(fourthLED, LOW);
  }
else if(cm <= 10){
    digitalWrite(firstLED, HIGH);
    digitalWrite(secondLED, HIGH);
    digitalWrite(thirdLED, HIGH);
    digitalWrite(fourthLED, HIGH);
  }
}

 

I have used four LEDs to illustrate the water level of the tank. Each LEDs short lead (negative) is connected to the same wire and that wire is connected to a ground pin of Arduino board. Each LEDs are going to illuminate according to the water level of the tank. First LED is connected to the 4th Digital pin. Second LED is connected to the 5thDigital pin. Third LED is connected to the 6th Digital pin. And fourth LED is connected to the 7th Digital pin. The sensor panel should be placed inside the water tank and above 5 cm from the maximum water level. Transducers’ face should be aimed to water surface. LED panel and Arduino board can be kept at any place.
If the water level is
1.       Below the quarter of height of the tank– illuminates first LED.
2.       Half of height of the tank – illuminates first and second LEDs.
3.       Below the three quarter of height of the tank – illuminates first, second and third LEDs.
4.       Maximum height of the tank – illuminates every LEDs.
Illustration of LED illumination is given below according to the water level as described above.
If you like this post, spread by sharing it on social media. Thank you !
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: