What is the HC-SR04?

The HC-SR04 is an ultrasonic distance sensor that
can be paired alongside Arduino and Raspberry Pi. It utilizes non-contact technology, with physical contact between the sensor and objects not needed for it to work.

Product Features

Here are some of the significant product features:

  • Power Supply: DC 5V
  • Quiescent Current : <2mA
  • Working Current: 15mA
  • Working Frequency: 40Hz
  • Ranging Distance : 2cm – 400cm/4m
  • Resolution : 0.3 cm
  • Measuring Angle: 15 degree
  • Trigger Input Pulse width: 10uS
  • Dimension: 45mm x 20mm x 15mm

How does it work?

  • The transmitter (trig pin) sends a sound wave
  • The object picks the wave up, reflecting it back
  • The receiver (echo pin) picks it up

The time between transmission and reception allows distance between to be calculated since sound’s velocity in air is known.

When using the HC-SR04, take note of:

Not to directly connect to electric

  • If directly connected, it’ll affect the normal work of the module

Range of area when testing objects to not be less than 0.5 square meters

  • Results of measuring will be affected if not followed

Comparing the HC-SR04 to Seeed Grove Ultrasonic Distance Sensor

What is Seeed Grove-Ultrasonic Distance Sensor?

It is Seeed very own version of an ultrasonic distance sensor which is comparative to the HC-SR04 but contains significant differences as shown below:

Grove – Ultrasonic Distance Sensor HC-SR04
Working Voltage 3.3V / 5V compatible
Wide voltage level: 3.2V – 5.2V
Measurement Range 3cm – 350cm 2cm – 400cm
I/O Pins needed 3 4
Operating Current 8mA 15mA
Dimensions 50mm x 25mm x 16mm 45mm x 20mm x 15mm
Ease of pairing with Raspberry Pi Easy, directly connect to I/O of Raspberry Pi Requires voltage conversion circuit

The main differences we can observe from the table above include:

Working Voltage

  • Grove -Ultrasonic Distance Sensor supports a wider voltage level

I/O Pins Needed

  • Transmitting and receiving signals on the Grove-Ultrasonic Distance Sensor share one pin, saving the requirement of using all available pins

Ease of pairing with Raspberry Pi

  • The Grove Ultrasonic Distance Sensor supports 3.3V, allowing it to be directly connected to the I/O of the Raspberry Pi instead of using a voltage conversion circuit

Despite the popularity of HC-SR04, the grove ultrasonic distance sensor is a more versatile option that allows for lesser external components and easier pairing with the Raspberry Pi. Apart from having the regular functionality of an ultrasonic sensor, it can be applied for proximity alarms and smart cars as well.

Arduino and Raspberry Pi Pairing Guide:

Arduino Guide:

If you’re thinking of picking up a Grove-Ultrasonic Distance Sensor after the above comparison, you can simply pair it with the following:

Seeduino V4.2: Seeed very own Arduino

The Grove-Ultrasonic Distance Sensor is compatible with Seeeduino V4.2, Seeed’s very own Arduino.

You’ll need the following items:

Step 1: Connect Ultrasonic Ranger to port D7 of Grove-Base Shield

Step 2: Plug Grove – Base shield into Seeeduino

Step 3: Connect Seeeduino to PC via a USB cable

It should look like this after completing the above steps:

Software configurations:

#include "Ultrasonic.h"

Ultrasonic ultrasonic(7);
void setup()
void loop()
    long RangeInInches;
    long RangeInCentimeters;

    Serial.println("The distance to obstacles in front is: ");
    RangeInInches = ultrasonic.MeasureInInches();
    Serial.print(RangeInInches);//0~157 inches
    Serial.println(" inch");

    RangeInCentimeters = ultrasonic.MeasureInCentimeters(); // two measurements should keep an interval
    Serial.println(" cm");
  • Step 4: We will see the distance display on the terminal as below:
The distance to obstacles in front is:
2 inches
6 cm
The distance to obstacles in front is:
2 inches
6 cm
The distance to obstacles in front is:
2 inches
6 cm

If you’re looking to connect multiple ultrasonic to one Arduino, you can do so by connecting one to D2 and the other to D3. Below are the respective codes:

#include "Ultrasonic.h"

Ultrasonic ultrasonic1(2);
Ultrasonic ultrasonic2(3);
void setup()
void loop()
    long RangeInCentimeters1;
    long RangeInCentimeters2;

    RangeInCentimeters1 = ultrasonic1.MeasureInCentimeters(); // two measurements should keep an interval
    Serial.println(" cm");

    RangeInCentimeters2 = ultrasonic2.MeasureInCentimeters(); // two measurements should keep an interval
    Serial.println(" cm");


Playing it with Codecraft: Quick Guide

Before we touch on the Raspberry Pi Guide, here’s a quick guide on pairing it with Codecraft

Hardware Configurations:

Step 1. Connect a Grove – Ultrasonic Ranger to port D7 if the Base Shield

Step 2. Plug the Base Shield to you Seeeduino/Arduino

Step 3. Link Seeeduino/Arduino to your PC via a USB cable.

Software configurations:

Step 1. Open Codecraft, add Arduino support, and drag a main procedure to working area.

Step 2. Drag blocks as the picture below or open the cdc file which can be downloaded at the end of this page.

Upload the program to your Arduino/Seeeduino

When the code finishes uploading, you will see the distance displayed in Serial Monitor

Raspberry Pi Guide:

The Grove Ultrasonic Distance Sensor can be paired with the Raspberry Pi as well. We’ll be using the Raspberry Pi 3B for the guide below but other models that support the base hat can be used as well.

If you’ve yet to own raspberry pi, you can pick up a Raspberry Pi Zero, a low-cost option to use alongside the Grove-Ultrasonic Distance Sensor.

You’ll need the following items:

Step 1: Plug the Grove Base Hat into Raspberry

Step 2: Connect the Grove – Ultrasonic Ranger to port D5 of the Base Hat

Step 3: Connect the Raspberry Pi to PC through USB cable

  • You can connect the ultrasonic ranger to any GPIO port as well but make sure you change the command with the corresponding port number

It should look something like this after completing the above steps:

Software Configurations:

  • Step 1. Follow Setting Software to configure the development environment.
  • Step 2. Download the source file by cloning the grove.py library.
cd ~
git clone https://github.com/Seeed-Studio/grove.py
  • Step 3. Execute below commands to run the code.
cd grove.py/grove
python grove_ultrasonic_ranger.py 5 6

Following is the grove_ultrasonic_ranger.py code.

import sys
import time
from grove.gpio import GPIO

usleep = lambda x: time.sleep(x / 1000000.0)

_TIMEOUT1 = 1000
_TIMEOUT2 = 10000

class GroveUltrasonicRanger(object):
    def __init__(self, pin):
        self.dio =GPIO(pin)

    def _get_distance(self):


        t0 = time.time()
        count = 0
        while count < _TIMEOUT1:
            if self.dio.read():
            count += 1
        if count >= _TIMEOUT1:
            return None

        t1 = time.time()
        count = 0
        while count < _TIMEOUT2:
            if not self.dio.read():
            count += 1
        if count >= _TIMEOUT2:
            return None

        t2 = time.time()

        dt = int((t1 - t0) * 1000000)
        if dt > 530:
            return None

        distance = ((t2 - t1) * 1000000 / 29 / 2)    # cm

        return distance

    def get_distance(self):
        while True:
            dist = self._get_distance()
            if dist:
                return dist

Grove = GroveUltrasonicRanger

def main():
    if len(sys.argv) < 2:
        print('Usage: {} pin_number'.format(sys.argv[0]))

    sonar = GroveUltrasonicRanger(int(sys.argv[1]))

    print('Detecting distance...')
    while True:
        print('{} cm'.format(sonar.get_distance()))

if __name__ == '__main__':

If everything goes well, you should see the following result:

pi@raspberrypi:~/grove.py/grove $ python grove_ultrasonic_ranger.py 5 6
Detecting distance...
121.757901948 cm
246.894770655 cm
2.60205104433 cm
0.205533257846 cm
0.657706425108 cm
247.433267791 cm
122.485489681 cm
^CTraceback (most recent call last):
  File "grove_ultrasonic_ranger.py", line 110, in <module>
  File "grove_ultrasonic_ranger.py", line 107, in main

You can quit this program by simply pressing Ctrl + C

Want to know more? Check these resources out

You can check out the following resources if you’re interested in finding out more:


I hope the above tutorials have been helpful, if you need any further technical support, you can send an email to techsupport@seeed.cc

The HC-SR04 is at no means a bad ultrasonic sensor with its integrated technology and wide working range, but it loses out to the Grove-Ultrasonic Distance Sensor as seen through the comparison.

The Grove-Ultrasonic Distance Sensor are available in stock after being sold out. Pick one up yourself now!

Please follow and like us: