MicroPython Driver for HC-SR04P Ultrasonic Distance Sensor
Contents
Introduction
This a MicroPython driver written specifically for the BBC micro:bit that will work with any of the HC-SR04P ultrasonic distance sensors. This driver uses the sensor's I2C interface.
The HC-SR04P distance sensors are discussed in some detail here.
FIG 1 - HC-SR04P board (2022 model): [Left] front view, [Right] rear view
The HC-SR04P boards are readily available in various forms. However they all operate in the same manner and use the same driver software.
Connecting the HC-SR04P
This sensor has four interface modes:
- Traditional HC-SR04 style interface (GPIO) using 2 microcontroller digital pins.
- Traditional HC-SR04 style interface (GPIO) using a single microcontroller digital pin.
- UART interface.
- I2C interface.
This driver uses the I2C interface. The I2C mode is activated with the M1 and M2 jumpers that are located on the rear of the board.
Jumper M1 must be left unconnected while jumper M2 must be connected with a blob of solder. See Fig 3A and Fig 3B.
It's easy to hookup to the micro:bit.
| micro:bit | HC-SR04P |
|---|---|
| 3.3V | VCC |
| GND | GND |
| Pin 19 | Trig/RX/SCL |
| Pin 20 | Echo/TX/SDA |
This utilises the standard I2C pins of the micro:bit.
Driver Overview
This driver is very simple. There is a single property which returns distance in cm.
Driver codeThe driver code can be:
- Copied from this webpage onto the clipboard then pasted into the MicroPython editor e.g. the Mu Editor. It should be saved as fc_hc_sr04p_i2c.py - OR -
- Download as a zip file using the link. Unzip the file and save it as fc_hc_sr04p_i2c.py into the default directory where the MicroPython editor e.g. Mu Editor saves python code files.
After saving the fc_hc_sr04p_i2c.py file to the computer it should be copied to the small filesystem on the micro:bit. The examples on this page will not work if this step is omitted. Any MicroPython editor that recognises the micro:bit will have an option to do this.
The Mu Editor is recommended for its simplicity. It provides a Files button on the toolbar for just this purpose.
Class constructorThe driver is implemented as a class.The first thing to do is call the constructor of the HC_SR04P_I2C class to obtain a sensor object.
Syntax:
HC_SR04P_I2C()
The I2C address is fixed (0x57)
and can't be changed.
Example
from fc_hc_sr04p_i2c import *
# Declare a HC-SR04P sensor object.
sensor = HC_SR04P_I2C()
This assumes that the file fc_hc_sr04p_i2c.py has been successfully copied to the micro:bit's filesystem as described above.
Measuring Distance
It's a very simple process to read distance values from the sensor. The distance values have units of centimeters.
Syntax:
Reading
Property that returns the distance in cm.
A value of 0 will be returned in the
event of an error. This might occur
e.g. if the target or the sensor are
in rapid movement.
Example 1:
from fc_hc_sr04p_i2c import *
sensor = HC_SR04P_I2C()
d = sensor.Reading
print('Distance:', d, 'cm')
Typical Output:
Distance: 38.0 cm
Example 2:
# Takes a distance measurement every
# 3 seconds in an endless loop.
from fc_hc_sr04p_i2c import *
sensor = HC_SR04P_I2C()
while True:
d = sensor.Reading
print('Distance:', d, 'cm')
sleep(3000)
Typical Output:
Distance: 38.0 cm
Distance: 46.0 cm
Distance: 54.3 cm
Distance: 45.2 cm
Distance: 35.5 cm
Distance: 23.9 cm
Distance: 10.9 cm
Distance: 3.3 cm
Traceback (most recent call last):
File "main.py", line 10, in <module>
KeyboardInterrupt:
While Example 2 was running the sensor was continuously moved. The program was stopped by typing CTRL/C (Windows) in the REPL.
Enjoy!
HC-SR04P Driver Code for micro:bit
Download as zip file
'''
HC-SR04P ultrasonic distance sensor
MicroPython driver for BBC micro:bit
******* IMPORTANT ********
This driver uses I2C mode.
As per RCWL-9610 datasheet.
**************************
AUTHOR: fredscave.com
DATE : 2025/11
VERSION : 1.00
'''
from microbit import *
from micropython import const
_REG_COMMAND = const(0xAE)
_REG_DISTANCE = const(0xAF)
_CMD_DISTANCE = const(0x01)
_ADDRESS = const(0x57)
class HC_SR04P_I2C():
# Returns the distance in cm.
@property
def Reading(self):
# Send command to start measurement
i2c.write(_ADDRESS, bytes([_REG_COMMAND]), True)
i2c.write(_ADDRESS, bytes([_CMD_DISTANCE]))
sleep(150)
# Retrieve distance measurement
i2c.write(_ADDRESS, bytes([_REG_DISTANCE]))
buf = i2c.read(_ADDRESS, 3)
d = buf[0] * 65536 + buf[1] * 256 + buf[2]
return round(d/10000, 1)
Images
Our sincere thanks goes to Frank, the local highly talented 'design and build' specialist, for the target board used in the development of this driver.
This target board will find use in the future development of drivers for other distance sensors; both ultrasonic and infrared based.
In order to configure I2C mode the M1 jumper must not be connected and the M2 jumper must be connected with a blob of solder.