Skip to content
Arduino library for HC-SR04 ultrasonic distance sensor.
Branch: master
Clone or download
Latest commit 39d4619 Mar 27, 2019

Arduino library for HC-SR04 ultrasonic distance sensor.

HC-SR04 is an ultrasonic sensor that measures distances from 2 to 400cm.


This is a simple library for it!


Sensor is initialized by creating instance of class UltraSonicDistanceSensor and providing trigger and echo pins.

UltraSonicDistanceSensor sensor(triggerPin, echoPin);

Then, to measure the distance, you just call measureDistanceCm(), which will return distance in centimeters (double). If distance is larger than 400cm, it will return negative value.


In this simple example, we need to connect sensors pins like this:

  • vcc to 5V
  • trig to digital pin 13
  • echo to digital pin 12
  • gnd to gnd
#include <HCSR04.h>

// Initialize sensor that uses digital pins 13 and 12.
int triggerPin = 13;
int echoPin = 12;
UltraSonicDistanceSensor distanceSensor(triggerPin, echoPin);

void setup () {
    Serial.begin(9600);  // We initialize serial connection so that we could print values from sensor.

void loop () {
    // Every 500 miliseconds, do a measurement using the sensor and print the distance in centimeters.
    double distance = distanceSensor.measureDistanceCm()
You can’t perform that action at this time.