Skip to content

Commit aa49ad9

Browse files
committed
2 parents f56f2d0 + 3d1126f commit aa49ad9

3 files changed

Lines changed: 26 additions & 23 deletions

File tree

UltrasonicSensor/UltrasonicSensor/Examples/UltrasonicSensor-readDistanceNative.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# FILE: UltrasonicSensor-readDistanceNative.py
1+
# FILE: UltrasonicSensor-readDistanceNative.py
22
# AUTHOR: Josip Šimun Kuči @ Soldered
33
# BRIEF: An example showing how to read the distance of the Native Ultrasonic sensor
44
# WORKS WITH: Ultrasonic module HC-SR04: www.solde.red/101202
5-
# LAST UPDATED: 2025-06-12
5+
# LAST UPDATED: 2025-06-12
66

77
from machine import I2C, Pin
88
from UltrasonicSensor import UltrasonicSensor
@@ -27,4 +27,4 @@
2727
print(f"Distance: {distance:.2f} cm")
2828
print(f"Time it took: {duration:.2f} us\n")
2929

30-
time.sleep(0.25) # Delay between readings
30+
time.sleep(0.25) # Delay between readings
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# FILE: UltrasonicSensor-readDistanceQwiic.py
1+
# FILE: UltrasonicSensor-readDistanceQwiic.py
22
# AUTHOR: Josip Šimun Kuči @ Soldered
3-
# BRIEF: An example showing how to read the distance of the Qwiic compatible
3+
# BRIEF: An example showing how to read the distance of the Qwiic compatible
44
# Ultrasonic sensor from an object
55
# WORKS WITH: Ultrasonic sensor with Qwiic: www.solde.red/333001
6-
# LAST UPDATED: 2025-06-12
6+
# LAST UPDATED: 2025-06-12
77

88
from machine import I2C, Pin
99
from UltrasonicSensor import UltrasonicSensor
@@ -15,18 +15,17 @@
1515
while True:
1616
sensor.takeMeasure()
1717
time.sleep(0.1) # Small delay for measurement
18-
19-
#The sensor sends a pulse through the trigger pin, which is deflected
20-
#By a surface in front of it and picked up by the echo pin, we then use the
21-
#time it took the impulse to reflect to calculate the distance
18+
19+
# The sensor sends a pulse through the trigger pin, which is deflected
20+
# By a surface in front of it and picked up by the echo pin, we then use the
21+
# time it took the impulse to reflect to calculate the distance
2222
distance = sensor.getDistance()
23-
24-
#How long did it take for the pulse to reflect
23+
24+
# How long did it take for the pulse to reflect
2525
duration = sensor.getDuration()
26-
27-
#Print out the distance and duration to the user
26+
27+
# Print out the distance and duration to the user
2828
print(f"Distance: {distance:.2f} cm")
2929
print(f"Time it took: {duration:.2f} us\n")
30-
31-
32-
time.sleep(0.25) # Delay between readings
30+
31+
time.sleep(0.25) # Delay between readings

UltrasonicSensor/UltrasonicSensor/UltrasonicSensor.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
1-
# FILE: UltrasonicSensor.py
1+
# FILE: UltrasonicSensor.py
22
# AUTHOR: Josip Šimun Kuči @ Soldered
33
# BRIEF: A MicroPython module for the HC-SR04 UltraSonic sensor. Supports both the Native and Qwiic version
4-
# LAST UPDATED: 2025-06-12
4+
# LAST UPDATED: 2025-06-12
55

66

77
# Import Qwiic base class for I2C devices
88
from Qwiic import Qwiic
9+
910
# Import Pin and I2C classes for GPIO and I2C operations
1011
from machine import I2C, Pin
12+
1113
# Used to detect the board type (ESP32, ESP8266, etc.)
1214
from os import uname
15+
1316
# Time module used for precise delays and measuring pulse duration
1417
import time
1518

1619
# Register addresses for I2C communication with a supported ultrasonic module
17-
TAKE_MEAS_REG = 0 # Command register to start measurement
18-
DISTANCE_REG = 1 # Register where distance in cm is stored (from external sensor)
19-
DURATION_REG = 2 # Register where echo pulse duration is stored (from external sensor)
20+
TAKE_MEAS_REG = 0 # Command register to start measurement
21+
DISTANCE_REG = 1 # Register where distance in cm is stored (from external sensor)
22+
DURATION_REG = 2 # Register where echo pulse duration is stored (from external sensor)
23+
2024

2125
class UltrasonicSensor(Qwiic):
2226
"""
2327
Ultrasonic sensor class that supports both native (GPIO) and I2C-based operation.
2428
In native mode, it uses GPIO pins to trigger and read echo.
2529
In I2C mode, it uses a pre-programmed I2C-based ultrasonic sensor.
2630
"""
31+
2732
def __init__(self, i2c=None, address=0x30, echo_pin=None, trig_pin=None):
2833
"""
2934
Initializes the sensor either in native GPIO mode or I2C mode.
@@ -124,4 +129,3 @@ def getDistance(self):
124129
# Read distance from I2C register
125130
data = self.read_register(DISTANCE_REG, 2)
126131
return data[1] << 8 | data[0]
127-

0 commit comments

Comments
 (0)