Jump to content
Arduino.lk Community Sri Lanka
  • Announcements

    • Team Arduino.lk

      Arduino Stater Kit 2 එකක් දිනාගන්න.   11/13/2017

      arduino.lk community වෙත ඔබව සාදරයෙන් පිළිගනිමු. ඔබගේ ගැටළු අදහස් මෙහි පළකර Rs 3650.00 වටිනා Arduino Stater Kit 2 එකක් දිනාගන්න. ඔබ කල යුත්තේ Arduino, හා සම්බන්ධ අදහස්, ලිපි, කමෙන්ට් මෙහි පල කර මෙහි ඇති LeaderBoard එකේ පළමු තැනට පැමිණීමයි. සැම මාසකම අවසාන දින 11.55 PM හොදම Contributor තෝරනු ලැබේ
Hasarinda Manjula

sensor HC-SR04 Ultrasonic Sensor

Recommended Posts

The HC-SR04 is an ultrasonic distance sensor. It detects the distance of the closest object in front of the sensor (from 2 cm up to 3m). It works by sending out a burst of ultrasound and listening for the echo when it bounces off of an object. The Arduino board sends a short pulse to trigger the detection, then listens for a pulse on the echo pin. The duration of this second pulse is equal to the time taken by the ultrasound to travel to the object and back to the sensor. Using the speed of sound, this time can be converted to distance.

Hardware Required

Arduino Board
(1) HC-SR04 Ultrasonic Distance Sensor
Jumper wire

Circuit

The 5V pin of the HC-SR04 is connected to the 5V pin on the Arduino, the GND pin is connected to the GND pin, and the Trigger pin is connected to pin 8 and the Echo pin is connected to pin 9 on the Arduino.

003.jpg

/*
HC-SR04 for Arduino
Original project from http://www.arduino.lk
This project demonstrates the HC-SR
The distance presented in the code is in mm, but you can uncomment the line
for distance in inches.
The schematics for this project can be found on http://www.www.arduino.lk
This example code is in the public domain.
*/
const int TriggerPin = 8;      //Trig pin
const int EchoPin = 9;         //Echo pin
long Duration = 0;
void setup(){
  pinMode(TriggerPin,OUTPUT);  // Trigger is an output pin
  pinMode(EchoPin,INPUT);      // Echo is an input pin 
  Serial.begin(9600);          // Serial Output
}
void loop(){ 
  digitalWrite(TriggerPin, LOW);                   
  delayMicroseconds(2);
  digitalWrite(TriggerPin, HIGH);          // Trigger pin to HIGH
  delayMicroseconds(10);                   // 10us high 
  digitalWrite(TriggerPin, LOW);           // Trigger pin to HIGH
 
  Duration = pulseIn(EchoPin,HIGH);        // Waits for the echo pin to get
high
                                           // returns the Duration in
microseconds
  long Distance_mm = Distance(Duration);   // Use function to calculate the
distance
 
  Serial.print("Distance = ");             // Output to serial
  Serial.print(Distance_mm);
  Serial.println(" mm");
 
  delay(1000);                             // Wait to do next measurement
}
 
long Distance(long time)
{
    // Calculates the Distance in mm
    // ((time)*(Speed of sound))/ toward and backward of object) * 10
   
    long DistanceCalc;                      // Calculation variable
    DistanceCalc = ((time /2.9) / 2);     // Actual calculation in mm
    //DistanceCalc = time / 74 / 2;         // Actual calculation in inches
    return DistanceCalc;                    // return calculated value
}

 

  • Thanks 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×