MicroPython Driver for US-100 Ultrasonic Distance Sensor

US-100 Driver Code for micro:bit

Download as zip file

'''
US-100 Ultrasonic distance sensor
MicroPython driver for BBC micro:bit

To operate this sensor in UART mode the
jumper on the back of the board must be in place.
This sensor is a 3.3V to 5V powered device.

AUTHOR: fredscave.com
DATE  : 2025/10
VERSION : 1.00
'''

from microbit import *
from micropython import const

_CMD_DISTANCE    = const(0x55)
_CMD_TEMPERATURE = const(0x50)

class US_100():
    def __init__(self, Trig=pin1, Echo=pin2):
        self.tx = Trig
        self.rx = Echo

    # Returns the distance in cm.
    @property
    def Reading(self):
        cnt = 1
        uart.init(tx = self.tx, rx = self.rx)
        # Initiate distance measurement.
        uart.write(bytes([_CMD_DISTANCE]))
        # Wait for the result.
        while not (uart.any()) and (cnt < 150 ):
            sleep(1)
            cnt += 1
        buf = uart.read()
        uart.init(115200) # Restore the REPL.
        if cnt == 150:
            return 0
        elif len(buf) == 2:
            return (buf[0] * 256 + buf[1]) / 10
        else:
            return 0

    # Returns temperature in degrees Celsius.
    @property
    def Temperature(self):
        cnt = 1
        uart.init(tx = self.tx, rx = self.rx)
        # Initiate temperature measurement.
        uart.write(bytes([_CMD_TEMPERATURE]))
        # Wait for the result.
        while not (uart.any()) and (cnt < 10 ):
            sleep(1)
            cnt += 1
        buf = uart.read(1)
        uart.init(115200) # Restore the REPL.
        if cnt == 10:
            return None
        else:
            return buf[0] - 45
          

Another Code Example

This program will read the distance once every three second then apply a set of simple rules to determine if a safe distance from the object in front is being maintained or a collision is imminent.

The rules are:

  • Distance > 30cm : Safe distance being maintained.
  • 30cm < Distance > 10cm : Warning, increase distance.
  • Distance < 10cm : Collision is imminent.

Code:
# This program reads the distance to the
# object in front of the sensor.
# A set of simple rules are applied to
# the distance and warnings of close
# approach are issued if necessary.

from fc_us_100 import *

# Accepts a distance and applies a set
# of safety rules.
def ApplyRules(D):
    if D == 0:
        print('Distance measuring error!')
    elif D > 30:
        print(D, 'cm: Safe distance')
    elif D < 10:
        print(D, 'cm: Collision is imminent')
    else:
        print(D, 'cm: Warning! Increase distance')

sensor = US_100()
# Set up an endless loop
while True:
    # Take a distance reading once every 3 seconds
    # and check if distance is safe.
    sleep(3000)
    Distance = sensor.Reading
    ApplyRules(Distance)


Typical Output:
34.5 cm: Safe distance
30.8 cm: Safe distance
29.0 cm: Warning! Increase distance
26.9 cm: Warning! Increase distance
24.3 cm: Warning! Increase distance
21.3 cm: Warning! Increase distance
19.4 cm: Warning! Increase distance
17.2 cm: Warning! Increase distance
15.1 cm: Warning! Increase distance
11.7 cm: Warning! Increase distance
10.4 cm: Warning! Increase distance
8.6 cm: Collision is imminent
8.6 cm: Collision is imminent
11.4 cm: Warning! Increase distance
17.8 cm: Warning! Increase distance
23.4 cm: Warning! Increase distance
28.4 cm: Warning! Increase distance
35.0 cm: Safe distance
          

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.

US-100 distance sensor, micro-bit and the target board used in the development in this driver.
Fig 2 US-100 ultrasonic distance sensor with the micro:bit and the target board used during the development of this driver.

US-100 hooked up to the micro:bit.
Fig 3 US-100 ultrasonic distance sensor hooked up to the micro:bit.