KaveeshaB
Published © GPL3+

Simple DIY Project Using HC-SR04 Ultrasonic Sensor

A simple DIY project using HC-SR04 ultrasonic sensor to measure the water level of a water tank.

BeginnerFull instructions provided998
Simple DIY Project Using HC-SR04 Ultrasonic Sensor

Things used in this project

Story

Read more

Schematics

Bread Board Diagram

Illustration of LED illumination

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.

Code

Arduino Code

Arduino
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);
  }

}

Credits

KaveeshaB

KaveeshaB

1 project • 0 followers

Comments