Random Nerd Tutorials

Complete Guide for Ultrasonic Sensor HC-SR04


This post is all about the Ultrasonic Sensor HC – SR04. I’ll explain how it works, show some features and share an Arduino Project example to help you with your projects.


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 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.


  • 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 really cool and popular among the Arduino Tinkerers. So I’ve decided to post a project example using this sensor. In this project the ultrasonic sensor read and write the distance in the serial monitor. It’s really simple.

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

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

Parts Required

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!



Source code

Source code with NewPing

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

Where to buy?


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.

The HC-SR04 barely works to 10 feet giving a total path length of 20 feet and a path time of about 20ms so set the timeout to something above that, say 25 or 30ms.

If you put a resistor, say 2k2 between E and T then only connect to T you can use the HC-SR04 from just one Arduino pin. Look up single pin operation of ultrasonic sensors.

Also if you are using a HC-SR04 with a PicAxe you need to up the clockspeed to at least 8MHz otherwise they don’t see the start of the echo pulse so pulsin never starts. The HC-SR04 works fine with a BS2.” by David Buckley

Thanks to my friend David to leave such an helpful comment!

I hope you found this useful.

Share this post with a friend that also likes electronics!

Thanks for reading, you can contact me by leaving a comment. If you like this post probably you might like my next ones, so please support me by subscribing my blog and my Facebook Page.

P.S. I’ve used this sensor in one of my previous project. Make sure you watch that project now: Ultrasonic Sensor with LED’s and buzzer

P.P.SClick here to get one ultrasonic sensor

Recommended Resources

Home Automation using ESP8266 »
Build IoT projects and home automation gadgets with the ESP8266 Wi-Fi module.

Build a Home Automation System »
Learn how to build a automation system using open-source hardware and software from scratch.

Arduino Step-by-Step Projects »
Build 25 cool Arduino projects with our couse even with no prior experience!

Leave a Comment:

Add Your Reply