Ultrasonic Sensor HC-SR04 is a sensor that can measure distance. It emits an ultrasound at 40 000 Hz (40kHz) which travels through the air and if there is an object or obstacle on its path It will bounce back to the module. It is commonly used in obstacle avoiding robots and automation projects. It comes complete with ultrasonic transmitter and receiver modules.

Features

Here’s a list of some of the HC-SR04 ultrasonic sensor features and specs:

  • 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

How Does It Work

Ultrasonic sensors work by emitting sound waves with a frequency that is too high for a human to hear. These sound waves travel through the air with the speed of sound, roughly 343 m/s. If there is an object in front of the sensor, the sound waves get reflected back and the receiver of the ultrasonic sensor detects them. By measuring how much time passed between sending and receiving the sound waves, the distance between the sensor and the object can be calculated.

At 20°C, the speed of sound is roughly 343 m/s or 0.034 cm/µs. Let’s say that the time between sending and receiving the sound waves is 2000 microseconds. If you multiply the speed of sound by the time the sound waves travelled, you get the distance that the sound waves travelled.
Distance = Speed x Time
But that is not the result we are looking for. The distance between the sensor and the object is actually only half this distance because the sound waves traveled from the sensor to the object and back from the object to the sensor. So you need to divide the result by two.
Distance (cm) = Speed of sound (cm/µs) × Time (µs) / 2
And so for the example this becomes:
Distance (cm) = 0.0343 (cm/µs) × 2000 (µs) / 2 = 34.3 cm

 

HC-SR04 Ultrasonic Sensor Pinout

VCC  is the power supply for HC-SR04 Ultrasonic distance sensor which we connect the 5V pin on the Arduino.
Trig (Trigger) pin is used to trigger the ultrasonic sound pulses.
Echo pin produces a pulse when the reflected signal is received. The length of the pulse is proportional to the time it took for the transmitted signal to be detected.
GND should be connected to the ground of Arduino.

 

Connection

  • Sensor has four pins -GND, VCC, trig, echo
  • It works under 5v. Connect Vcc  pin of sensor with Arduino 5v
  • GND pin of ultrasonic sensor is connected to GND of Arduino
  • Interface trig pin and echo pin of sensor with any digital pins of Arduino. Here we are connected with digital pin 2 and 3 of Arduino.

Sample Code

 

Testing

 

Step 1 - Connect the A to B  cable to the USB port of a computer and make sure that the port has been detected by checking the Device Manager option.

Step 2 - Open two Arduino IDE window and Select Arduino UNO as Board and select the appropriate COM port.
Board: Tools > Board > Arduino/Geniuno UNO.

 

 

Step 3 - Port: Tools > Port in Arduino.

 

Step 4 - Compile and upload this program to your Arduino Uno board via Arduino IDE
Readings from the sensor will be obtained in the serial monitor.

 

 

The video demonstration is shown below