HC-SR04 Ultrasonic Sensor

Ultrasonic distance sensor for robotic projects

More details


Vendor's Description: 

The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats or dolphins do. It offers excellent range accuracy and stable readings in an easy-to-use package. 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). Similar in performance to the SRF005 but with the low-price of a Sharp infrared sensor.


  • Power Supply :5V DC
  • Quiescent Current : <2mA
  • Effectual Angle: <15°
  • Ranging Distance : 2cm – 500 cm/1" - 16ft
  • Resolution : 0.3 cm


Using the HC-SR04 with an Arduino 

There is an Arduino library for the HC-SR04 that offers two ways to use the sensor. To install, download the “Ultrasonic Library” from this page, unzip the release package into your “arduino-0022/libraries/” folder. Open the Arduino IDE and include the library by Sketch-Import library-Ultrasonic . There is also an example sketch in File-Examples-Ultrasonic-UltrasonicDemo. 

Arduino Library & Sample

You can directly connect it to your arduino board and measure the distance without wiring with this sketch.

#include <Ultrasonic.h>

int trigpin = 10;//appoint trigger pin
int echopin = 11;//appoint echo pin

Ultrasonic ultrasonic(trigpin,echopin);

void setup()
Serial.begin(9600);//set Serial Baud rate
Serial.println("Ultrasonic sensor starting!!!!!");

pinMode(9, OUTPUT);
pinMode(12, OUTPUT);
digitalWrite(9, HIGH);
digitalWrite(12, LOW);


void loop()
float cmdistance,indistance;
long microsec = ultrasonic.timing();
Serial.print("microsec: ");
cmdistance = ultrasonic.CalcDistance(microsec,Ultrasonic::CM);//this result unit is centimeter
indistance = ultrasonic.CalcDistance(microsec,Ultrasonic::IN);//this result unit is inches
Serial.print(" cmdistance: ");
Serial.print(" indistance: ");