Using the Ultrasonic sensor HC-SR04 with Arduino


The HC-SR04 ultrasonic sensor works like Sonar to determine the distance of an object just like the bats do. It offers excellent non-contact range detection from 2 cm to 400 cm (or 1” to 13 feet) with high accuracy and stable readings in an easy-to-use package.

The operation is not affected by sunlight or black material, although acoustically, soft materials like cloth can be difficult to detect. It comes complete with ultrasonic transmitter and receiver module.

The HC-SR04 Ultrasonic Module has 4 pins, Ground, VCC, Trig and Echo. The Ground and the VCC pins of the module needs to be connected to the Ground and the 5 volts pins on the Arduino Board respectively and the trig and echo pins to any Digital I/O pin on the Arduino Board.

In order to generate the ultrasound you need to set the Trig on a High State for 10 µs. That will send out an 8 cycle sonic burst which will travel at the speed of sound and Echo from any Object will be detected by the sensor. The Echo pin will output the time in microseconds the sound wave traveled.

For example, if the object is 10 cm away from the sensor, and the speed of the sound is 340 m/s or 0.034 cm/µs the sound wave will need to travel about 294 u seconds. But what you will get from the Echo pin will be double that number because the sound wave needs to travel forward and bounce backward.  So in order to get the distance in cm we need to multiply the received travel time value from the echo pin by 0.034 and divide it by 2.



Technical Specifications


Power Supply − +5V DC
Quiescent Current − <2mA
Working Current − 15mA
Effectual Angle − <15°
Ranging Distance − 2cm – 400 cm/1″ – 13ft
Resolution − 0.3 cm
Measuring Angle − 30 degree
Trigger Input Pulse width: 10uS
Dimension: 45mm x 20mm x 15mm

Components Required
You will need the following components −
1 × Breadboard
1 × Arduino Uno R3
1 × ULTRASONIC Sensor (HC-SR04)


Procedure
Follow the circuit diagram and make the connections as shown in the image given below.




////  Arduino Code  /////////////////////////////////////////////////////

const int pingPin = 7; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 6; // Echo Pin of Ultrasonic Sensor

void setup() {
   Serial.begin(9600); // Starting Serial Terminal
}

void loop() {
   long duration, inches, cm;
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(pingPin, LOW);
   pinMode(echoPin, INPUT);
   duration = pulseIn(echoPin, HIGH);
   inches = microsecondsToInches(duration);
   cm = microsecondsToCentimeters(duration);
   Serial.print(inches);
   Serial.print("in, ");
   Serial.print(cm);
   Serial.print("cm");
   Serial.println();
   delay(100);
}

long microsecondsToInches(long microseconds) {
   return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
   return microseconds / 29 / 2;
}
//////////////  CODE END  ///////////////////////////////////////////

 

The Ultrasonic sensor has four terminals - +5V, Trigger, Echo, and GND connected as follows −
Connect the +5V pin to +5v on your Arduino board.
Connect Trigger to digital pin 7 on your Arduino board.
Connect Echo to digital pin 6 on your Arduino board.
Connect GND with GND on Arduino.


In our program, we have displayed the distance measured by the sensor in inches and cm via the serial port.

Result:  You will see the distance measured by sensor in inches and cm on Arduino serial monitor.