© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
DevHeads IoT Integration ServerDIIS
DevHeads IoT Integration Server•17mo ago
Enthernet Code

UnboundLocalError in Obstacle Detection with Ultrasonic Sensor Using MicroPython

Am working on a smart car automation system using MicroPython, the system include various functionalities such as
obstacle detection
obstacle detection
,
line following
line following
,
speed control
speed control
, and
environmental monitoring
environmental monitoring
. am trying to detect obstacles in front of the car using an ultrasonic sensor
HC-SR04
HC-SR04
and control the motor accordingly. my aim is to measure the distance to obstacles and stop the car if an obstacle is too close.
But I keep getting the error
UnboundLocalError: local variable 'start' referenced before assignment
UnboundLocalError: local variable 'start' referenced before assignment


here's my code 👇
from machine import Pin
import time

# Ultrasonic sensor setup
trigger = Pin(5, Pin.OUT)
echo = Pin(18, Pin.IN)

def measure_distance():
    trigger.off()
    time.sleep_us(2)
    trigger.on()
    time.sleep_us(10)
    trigger.off()

    while echo.value() == 0:
        start = time.ticks_us()
    while echo.value() == 1:
        end = time.ticks_us()

    duration = time.ticks_diff(end, start)
    distance = (duration * 0.0343) / 2
    return distance

while True:
    distance = measure_distance()
    print('Distance:', distance, 'cm')
    if distance < 10:
        print("Obstacle detected! Stopping car.")
        stop()  # Add stop function from Stage 1
    time.sleep(1)
from machine import Pin
import time

# Ultrasonic sensor setup
trigger = Pin(5, Pin.OUT)
echo = Pin(18, Pin.IN)

def measure_distance():
    trigger.off()
    time.sleep_us(2)
    trigger.on()
    time.sleep_us(10)
    trigger.off()

    while echo.value() == 0:
        start = time.ticks_us()
    while echo.value() == 1:
        end = time.ticks_us()

    duration = time.ticks_diff(end, start)
    distance = (duration * 0.0343) / 2
    return distance

while True:
    distance = measure_distance()
    print('Distance:', distance, 'cm')
    if distance < 10:
        print("Obstacle detected! Stopping car.")
        stop()  # Add stop function from Stage 1
    time.sleep(1)
DevHeads IoT Integration Server banner
DevHeads IoT Integration ServerJoin
The DevHeads IoT Integration Server accelerates technology engineering by helping pro devs learn, share and collaborate.
2,984Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Ultrasonic sensor
DevHeads IoT Integration ServerDIISDevHeads IoT Integration Server / 🟩-pcb-and-analog
2y ago
Can you provide detailed information about ultrasonic sensor HC-SR05 maximum detection range?
DevHeads IoT Integration ServerDIISDevHeads IoT Integration Server / 🟩-pcb-and-analog
2y ago
A motion sensor or an ultrasonic sensor to correctly detect a hand at about 1.3 meters?
DevHeads IoT Integration ServerDIISDevHeads IoT Integration Server / 🟩-pcb-and-analog
2y ago
help me choosing the right sensor of ligne following and color detection for my robot
DevHeads IoT Integration ServerDIISDevHeads IoT Integration Server / 🟩-pcb-and-analog
2y ago