MicroPython Driver for US-100 Ultrasonic Distance Sensor
Contents
Introduction
This a MicroPython driver written specifically for the BBC micro:bit that will work with the US-100 ultrasonic distance sensor. This driver uses the sensor's UART interface.
The US-100 distance sensor is discussed in some detail here.
FIG 1 - US-100 board: [Left] front view, [Right] rear view
The US-100 boards are readily available and not particularly expensive.
Connecting the US-100
This sensor has two interfaces:
- HC-SR04 style interface
- UART interface
This driver uses the UART interface. It's easy to hookup to the micro:bit.
| micro:bit | US-100 |
|---|---|
| 3.3V | VCC |
| GND | GND |
| Pin 1 | Trig/Tx |
| Pin 2 | Echo/Rx |
While the default micro:bit pins used are Pin 1 and Pin 2, any two available digital pins may be used. The pins used are simply passed to the constructor when creating an instance of the sensor class - fully explained below.
Driver Overview
This driver is very simple. There are two properties which:
- Returns distance.
- Returns temperature.
The 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_us_100.py - OR -
- Download as a zip file using the link. Unzip the file and save it as fc_us_100.py into the default directory where the MicroPython editor e.g. Mu Editor saves python code files.
After saving the fc_us_100.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 US_100 class to obtain a sensor object.
Syntax:
US_100(Trig=pin1, Echo=pin2)
The two default micro:bit pins are: pin1 and pin2
Trig and Echo can be set to any available digital
pins on the micro:bit.
Example 1
from fc_us_100 import *
# Declare a US-100 sensor object.
# This uses the default micro:bit pins
sensor = US_100()
Example 2
from fc_us_100 import *
# Declare a US-100 sensor object.
# Use pin14 and pin15 on the micro:bit.
sensor = US_100(Trig=pin14, Echo=pin15)
This assumes that the file fc_us_100.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:
from fc_us_100 import *
sensor = US_100()
d = sensor.Reading
print('Distance:', d, 'cm')
Typical Output:
Distance: 34.5 cm
Measuring Temperature
The temperature sensor on the US-100 board is used to compensate the distance measurements since the speed of sound varies with temperature. Temperature is accessible to the user as a low resolution 8-bit value.
It's a very simple process to obtain temperature values from the sensor. They are in degrees Celsius.
Syntax:
Temperature
Property returns temperature in degrees Celsius.
Example:
from fc_us_100 import *
sensor = US_100()
t = sensor.Temperature
print('Temperature:', t, 'C')
Typical Output:
Temperature: 16 C
Enjoy!
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.