Distance Measure with Sonar Sensor

Distance Measure with Sonar Sensor

This post is all about the Ultrasonic Sensor HC – SR04 mostly known as Sonar Sensor. I’ll explain how it works. Finally show how can you easily measure distance using this sensor.

DESCRIPTION

The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package. From 2cm to 400 cm or 1” to 13 feet. It’s operation is not affected by sunlight or black material like Sharp rangefinders are (although acoustically soft materials like cloth can be difficult to detect). It comes complete with ultrasonic transmitter and receiver module.

Sensor Features
Power Supply :+5V DC
Quiescent Current : <2mA
Working Current: 15mA
Effectual Angle: 15°
Ranging Distance : 2cm – 400cm or 1″ – 13ft
Resolution : 0.3 cm
Measuring Angle: 30 degree
Dimension: 45mm x 20mm x 15mm

Sensor Photo
HC-SR04
Pins
VCC: +5VDC
Trig : Trigger (INPUT)
Echo: Echo (OUTPUT)
GND: GND

Connection Schematics:
schematics
Sensor | Arduino
VCC > 5V
GND > GND
Trig > D11
Echo > D12

I am assuming everyone is pretty much fimilar with Arduino Programming IDE. So, I am going to coding part directly.

int trigPin = 11;
int echoPin = 12;
int sound = 250;
void setup() 
{
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
}
void loop() 
{
 long duration, distance;
 digitalWrite(trigPin, LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 distance = (duration/2) / 29.1;
 Serial.print(distance);
 Serial.println(" cm");
 delay(500);
}

Upload the code to your arduino board. Now check the serial monitor. To check serial monitor in Arduino IDE Click on Tools> Serial Monitor
serial-monitor

You should get the measure of distance from your sonar sensor. Use your hand to check weather it’s work or not.
There are some following situation where sonar sensor might not work properly.
wrong-reading-sonar-sensor
If you face any problem regarding this experiment. Please comment below !


What are your thoughts?