Skip to content

Ultrasonic Sensor

Level Intermediate
Board V1 & V2
Parts micro:bit · HC-SR04P (3 V) or HC-SR04 (5 V) ultrasonic sensor · 5 V module only: 1 kΩ and 2 kΩ resistors, and a 5 V supply · Edge connector breakout · Jumper wires
Copy first ultrasonic.py onto your micro:bit before flashing the example — how?

What you'll build

Measure the distance to an object using sound pulses and scroll it on the display.

Wiring

Pins 13 and 15 are only reachable through an edge connector breakout board. Pick the tab for your module. The HC-SR04P runs on 3 V; the classic HC-SR04 needs 5 V (check the label or the seller's listing).

Part Part pin micro:bit pin
HC-SR04P (3 V) Trig P13
HC-SR04P (3 V) Echo P15
HC-SR04P (3 V) VCC 3V
HC-SR04P (3 V) GND GND

HC-SR04P (3 V)TrigEchoVCCGNDmicro:bitP13P153VGND

Never connect a 5 V Echo straight to the micro:bit

A 5 V HC-SR04 sends 5 V out of Echo, but micro:bit pins only tolerate about 3.6 V. Wired directly, it can damage the micro:bit. Two resistors, a voltage divider, bring the signal down to 5 V × 2 kΩ / (1 kΩ + 2 kΩ) ≈ 3.3 V. No 2 kΩ resistor? Use two 1 kΩ in series, or 2.2 kΩ (≈ 3.4 V).

The micro:bit has no 5 V pin, so power the sensor from a separate 5 V supply, such as a breakout board with a 5 V rail, or a USB power bank through a USB breakout. Connect the supply's GND to the micro:bit's GND: without a shared ground, the signals have nothing to be measured against. Trig can be wired directly, because 3 V is high enough for the sensor to read.

Part Part pin micro:bit pin
HC-SR04 (5 V) VCC +5V of an external 5 V supply
HC-SR04 (5 V) Trig P13
HC-SR04 (5 V) Echo P15 through 1 kΩ; a 2 kΩ resistor also links P15 to GND
HC-SR04 (5 V) GND GND

5 V supply+5VGNDHC-SR04 (5 V)VCCTrigEchoGNDmicro:bitP13P15GND1 kΩ2 kΩGND rail: all grounds join here

Code

ultrasonic-example-1.py
'''
Example: Measure distance using Ultrasonic sensor and display results on LED matrix in micro:bit
By: Meqdad Darwish
'''
from microbit import *
from ultrasonic import Ultrasonic

# default pins in Ultrasonic class are:
# trigger: pin13
# echo: pin15

ultrasonic_sensor = Ultrasonic()
# or
# ultrasonic_sensor = Ultrasonic(trig=pin13, echo=pin15)

MAX_RANGE_CM = 400  # HC-SR04 range is about 2-400 cm

while True:
    distance_value = ultrasonic_sensor.measure_distance_cm()
    if 0 < distance_value <= MAX_RANGE_CM:
        display.scroll(str(int(distance_value)))
    else:
        # No echo came back: nothing in range, or check the wiring
        display.show("?")
    sleep(2000)
ultrasonic.py
'''
Ultrasonic sensor (HC-SR04) library  in microPython for the microcontrollers like micro:bit
Author: Meqdad Darwish
GitHub: @MeqdadDev
https://github.com/MeqdadDev/
'''

from microbit import *
from machine import time_pulse_us

class Ultrasonic:
    '''
    A library to interface Ultrasonic HC-SR04 sensor in microPython with micro:bit and other controllers.

    Example:
    ```
    from microbit import *
    from ultrasonic import Ultrasonic

    ultrasonic_sensor = Ultrasonic()

    while True:
        distance_value = ultrasonic_sensor.measure_distance_cm()
        display.scroll(str(int(distance_value)))
    ```
    '''
    def __init__(self, trig=pin13, echo=pin15):
        self.trigger = trig
        self.echo = echo
        self.trigger.write_digital(0)
        self.echo.read_digital()

    def measure_distance_cm(self):
        self.trigger.write_digital(1)
        self.trigger.write_digital(0)
        msec = time_pulse_us(self.echo, 1)
        echo_time = msec / 1000000
        dist_cm = (echo_time / 2) * 34300
        return dist_cm

How it works

The sensor sends an ultrasonic ping from Trig and holds Echo high until the echo returns. The library times that pulse with time_pulse_us and converts it to centimetres using the speed of sound. Pass Ultrasonic(trig=..., echo=...) to use other pins.

If no echo returns (nothing within about 4 m, or a wiring problem), the reading is zero or negative, so the example shows ? instead of a misleading number. Waiting for a missing echo takes up to one second.

Try this next

  • Make a parking sensor that beeps faster as objects get closer.
  • Show distance as a bar graph on the LED matrix.
  • Combine with the servo to build a sweeping radar.