Monday, 1 July 2013

HC-SR04 Ultrasonic Sensor

Have you ever wonder what type of sensor that you can use to sense for obstacle ? Well, in this post, I am going to explain a brief detail about using a HC-SR04 Ultrasonic Sensor for sensing obstacle.

The HC-SR04 sensor is using the sonar to determine the distance to an object like what you know about bats or dolphin. It comes complete with ultrasonic transmitter and recevier module.

Some features of the HC-SR04 ultrasonic sensor :
  • Power supply :  +5V DC
  • Working current : 15mA
  •  Measuring angle : 30 degree
  • Ranging distance : 2cm - 400cm 

Datesheet HC-SR04

A very good video preview on ultrasonic sensor ! 

 Let's get started !
Hardware :
  • HC-SR04 x 1
  • LCD x 1
  • Arduino x 1
  • Buzzer x 1
  • Few jumper wires 
Software :
  • Arduino IDE
Hardware connection :

Connect your HC-SR04 as shown below :

 Connect a buzzer to Pin 7 of arduino UNO.

Software :
I found a pretty easy to use arduino library for this. You may download it here :

Ping Library

You may want to view the guide below on how to use this library in arduino

Ping Library Guide

 I have uploaded below code to my arduino

Download Code

A simple demo on my project using HC-SR04:

Feel free to comment and ask if you have any doubt. Would try my best to answer and help :)




  1. i used same Ultrasonic Sensor few days back.
    it's amazing and is very accurate too.

    1. Yup, I have tested it. Quite accurate with the readings given :)

  2. what's there is "delay(distance*10)" in your code use for?

    1. HaHahaha, this is because the sonar.ping_cm will give you the value like 20 if 20cm la.

      So I just add that to increase my delay time only. If delay(20) and delay(30) seems not big difference. So I multiply it with 10 to have a slight obvious difference between the delay for different distance.

      Do you get what I mean ?

      Thanks :)

  3. I also sell this type of sensor Only RM20/pcs..

    1. That is cheap ! Maybe you can attach the link here so that it will direct the reader to that particular item ?

      Thanks :)

    2. Thanks :D