HC-SR04P Ultrasonic Distance Sensor

Contents

Description

The HC-SR04P is a drop-in replacement for the older HC‑SR04 ultrasonic distance sensor. It's much more versatile but retains full compatible with the older driver libraries.

HC-SR04P board front view
HC-SR04P board rear view

FIG 1 - HC-SR04P board (2022 model): [Left] front view, [Right] rear view

The following table provides a comparison of the original HC‑SR04 and the improved HC‑SR04P version.

Parameter HC-SR04 HC-SR04P
Voltage 5V 2.8V to 5.5V
MCU
5V
Direct connect Trig and Echo pins. Direct connect Trig and Echo pins.
MCU
3.3V
Direct connect Trig pin.

Echo pin must be connected through a voltage divider or logic level shifter.
Direct connect Trig and Echo pins.
Max range
(5V)
450 cm 450 cm
Max range
(3.3V)
N/A 400 cm
Min range 2 cm 2 cm
Accuracy
(Datasheet)
±0.3 cm ±0.3 cm
Interfaces Digital pin x 2 • Digital pin x 2
• Digital pin x 1
• I2C
• UART

Ultrasonic Distance Sensing

The HC-SR04P has two ultrasonic transducers. One is a transmitter, the other is the receiver.

The transmitter emits ultrasonic waves with a 40kHz frequency. The receiver listens for the echo of those waves after they bounce off an object.

Knowing the speed of sound in air, it's a simple task to calculate the distance between the target object and the sensor board.

Ultrasonic Sensor Working Principle

Ultrasonic Sensor Working Principle[1]

Pinout

The HC-SR04P has four pins:

  • VCC: Power supply input, usually 3.3V or 5V.
  • Trig/RX/SCL:
    • Trig: Used to trigger a measurement if using HC-SR04 Digital pin x 2 mode.

      Connect to any available digital pin on the microcontroller.
    • RX: Connected to microcontroller's TX pin if using UART mode.
    • SCL: Serial clock pin if using I2C mode. Connect to the microcontroller's I2C SCL pin.
  • Echo/TX/SDA:
    • Echo: Goes High if using HC-SR04 Digital pin x 2 mode; time that signal is held High is proportional to distance measured.

      Connect to any available digital pin on the microcontroller.
    • TX: Connected to microcontroller's RX pin if using UART mode.
    • SDA: Serial data pin if using I2C mode. Connect to the microcontroller's I2C SDA pin.
  • GND: Connected to microcontroller's ground pin.

Interfaces

As noted in the table above the HC-SR04P has four different modes that can be used to obtain a distance measurement.

The rear of the HC-SR04P board has two jumpers labelled M1 and M2. The connection state of M1 and M2 determines which interface is in use.

Interface M1 M2
Digital pin x 2 ✕ ✕
Digital pin x 1 ✓ ✓
I2C ✕ ✓
UART ✓ ✕

Where "✓" is connected and "✕" is not connected.

Digital Pin x 2

This is the classic HC-SR04 distance measurement mode. These are the steps:

  • The microcontroller pulls the Trig pin High for at least 10µs.
  • The sensor sends eight x 40kHz ultrasonic pulses then waits to receive the returning (reflected) pulse back.
  • The sensor pulls the Echo pin High for the same amount of time that the ultrasound pulse has taken to leave and return back.
  • The microcontroller must detect the rising edge of the Echo pin and time the period that it remains high.
  • Distance (cm) = (0.0340 X Time)/2

    Where:
       Time = Echo pin High time in µs
       Speed of sound in air = 340 m/s

HC-SR04 ultrasonic timing diagram

HC-SR04 ultrasonic timing diagram[2]

Digital Pin x 1

This is very similar to the digital pin x 2 mode described above. The only difference is the Echo pin is not used.

The microcontroller sends the 10 µs initiation pulse to the Trig pin. The microcontroller must then turn its pin that's connected to the Trig pin from output to input.

The sensor outputs a High signal on the Trig pin which the microcontroller must time. The distance calculation is the same as given above.

I2C

This is a very simple mode to use. The I2C address is 0x57. Use wiring hook up given in Pinout. From the RCWL-9610 datasheet the steps are:

  • Write 0x01 to the sensor register 0xAE to initiate a distance measurement.
  • Wait 150 ms for the sensor to take the measurement and process the result.
  • Read three bytes from sensor register 0xAF.
  • Assemble the bytes into a single 24-bit unsigned integer. Divide the result by 10,000 to obtain the distance in cm.

UART

The sensor's UART baud rate is 9600. Use wiring hook up given in Pinout. The steps are:

  • Transmit the byte 0XA0 to the sensor to initiate a distance measurement.
  • Wait 150 ms for the sensor to take the measurement and process the result.
  • Receive three bytes from the sensor. Assemble the bytes into a single 24-bit unsigned integer. Divide the result by 10,000 to obtain the distance in cm.

HC-SR04P MicroPython Drivers for micro:bit

Two MicroPython drivers that support the HC-SR04P have been written that specifically target the BBC micro:bit:

References

  • [1]Author: electrocomps.in; 27 September, 2024
    Article: HC-SR04 Ultrasonic Sensor Working with Arduino & Raspberry Pi,
    Section: How Does the HC-SR04 Work?,
    Available at: https://electrocomps.in/understanding-how-ultrasonic-sensors-work-a-comprehensive-guideunderstanding-how-the-hc-sr04-ultrasonic-sensor-module-works-with-arduino-and-raspberry-piunderstanding-how-ultrasonic-sensors-work-a-c/
    Accessed: November, 2025.
  • [2]Author: ElectricWings;
    Article: HC-SR04 Ultrasonic Sensor Guide with Arduino Interfacing,
    Section: Ultrasonic Sensor HC-SR04 Working Principle,
    Available at: https://www.electronicwings.com/sensors-modules/ultrasonic-module-hc-sr04
    Accessed: November, 2025.