Using the HC-SR04 Ultrasonic Sensor with Arduino

HC-SR04    Send article as PDF   
The working principle of an ultrasonic sensor is simple and use high-frequency sound waves that are evaluated when the sensor received back the waves. To determine the distance between the robot and object, the sensor measure the elapsed time between sending and receiving the waves.

These sensors are ideal for measurement in different environments where measurements cannot be affected by the surface, material, light, dust, or other noises.

In robotics, ultrasonic sensors are used in a wide range of applications including here the measurement of distance, presence detection, or detect the position of an object.


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. It has a range from 20mm to 4000mm or 1” to 13 feet’. Unlike other distance sensors, its operation is not affected by sunlight or black material like Sharp rangefinders are (although acoustically soft materials like cloth can be difficult to detect). The PCB is complete with ultrasonic transmitter and receiver modules.


  • 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




  • VCC: +5VDC
  • Trig : Trigger (INPUT)
  • Echo: Echo (OUTPUT)
  • GND: GND

Arduino with HC – SR04 Sensor:

This sensor is  popular among the Arduino and robotics Tinkerers, but it was inspired by my LEGO Mindstorms Robotics class I teach to 10-13 year olds.I’ve decided to post a couple of project examples using this sensor. In these projects, the ultrasonic sensor is read and write the distance is output to the serial monitor. It’s really simple, but easy to start off with.

The goal is to help you understand how this sensor works, and then you can use this example in your own more sophisticated projects.

Note: There’s an Arduino library called NewPing that can make your life easier when using this sensor.


HC-SR04 4-Wire Schematics

Source Code:

Source Code using NewPing Library:

Below is an example using the NewPing library. Download the library here.

NOTE: “If the HC-SR04 does not receive an echo then the output never goes low. Devantec and Parallax sensors time out after 36ms and I think 28ms respectively. If you use Pulsin as above, then with no return echo the program will hang for 1 second which is the default timeout for Pulsin. You need to use the timeout parameter.

If the HC-SR04 is barely working at a range of 3m, and you need a total path length of 4m, the reason for this is because the path time of about 20ms is too short over longer distances. Set the timeout to something above 20ms. Start by trying 25ms or 30ms in your code, and you should be able get to the full specification distance.

Three Pin Mode:

If you put a link between the Echo and Trigger pins, then you only need connect to Trigger, allowing you to use the HC-SR04 from just one Arduino pin.

The circuit:

HC-SR04 3-Wire Schematics

Connect together the circuit above.

3 Pin Ping Sensor Code


HC-SR04 3-Wire Output


Previous Post
Membrane Keypad

Using a Membrane Keypad with an Arduino

With this tutorial, you will be learn to access any keypad, and have it interact with a microcontroller. You can salvage these keypads from old telephones or you can purchase ... Read more

Next Post
bmp180 barometricsensor

Introduction to using the BMP180 barometric sensor with Arduino

The BMP180 barometric sensor (model GY-68) in the following two images is a very small module with 10mm x 10.1mm (0.039in x 0.043in) footprint. This guide is also applicable for ... Read more

Short URL:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Do NOT follow this link or you will be banned from the site!