Ultrasonic Sensor

Description:
The ultrasonic sensor is a device that can measure the distance to an object by using sound waves. It measure distance by sending out a sound wave at specific frequency and listening for that sound wave to bounce back. By recording the time between the sound being generated and the sound bouncing back it is possible to calculate the distance between sensor and object using this formula below
Distance=Time*speed of sound divided/2 (as it travelled in two ways)
Pin Configuration:

1)VCC: it power the sensor typically with +5V
2)Trigger: Trigger pin is an Input pin . This pin has to be kept high for 10us to initialize measurement by sending ultrasonic waves. It will connected analog pin of arduino.
3)Echo: Echo pin is an output pin. This pin goes high for a period of time which will equal to the time taken for the Ultrasonic waves to return back to the sensor. It will connected to analog pin of arduino.
4)GND: This pin is connected to ground of the system.
Interfacing With Arduino




CODE
const int trigPin = 9;
const int echoPin = 10;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print(“Distance: “);
Serial.println(distance);
}
Applications:
- It is used to avoid and detect obstacles with robot like obstacle avoider robot, line follower robot etc.
- Used to measure the distance within a wide range of 2cm o 400cm
- Can be used to map the object surroundings the sensor by rotating it.
- Depth of certain places like wells, pits etc can be measures since the waves can penetrate through water.
Advantages:
- It has sensing capability to sense all the material type.
- This sensor is not affected due to atmospheric dust, rain, snow etc.
- it has higher sensing distance compare to other proximity sensor type.
- It provide good readings in sensing large sized objects with hard surfaces.
Limitations:
- It is very sensitive to variation in the temperature.
2. It has more difficulties in reading reflection from soft, curved, and small objects
Reference
https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/
https://www.tutorialspoint.com/arduino/arduino_ultrasonic_sensor.htm
https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/