# WRITE A MICROPYTHON CODE- BLIND STICK USING ULTRASONIC SENSOR(USE COMPONENTS - RASPERRY PI, PICO AND BUZZER).


from machine import Pin, PWM
import utime

trig_Pin = Pin(4, Pin.OUT)
echo_Pin = Pin(5, Pin.IN)
buzzer_Pin = Pin(15, Pin.OUT)
pwm = PWM(buzzer_Pin)

def measure_distance():
    trig_Pin.high()
    utime.sleep_us(10)
    trig_pin.low()

    while echo_Pin.value() ==  0:
        pulse_start utime.ticks_us()
    while echo_Pin.value() == 1:
        pulse_end = utime.ticks_us()
    pulse_duration = pulse_end - pulse_start

    distance = (pulse_duration * 34300) / 2 / 1000000
    return distance

def activate_buzzer():
    pwm.freq(1000)
    pwm.duty_u16(32768)

def deactivate_buzzer():
    pwm.duty_u16(0)

while True:
    diatance = measure_diatance()
    print("Distance:", distance, "cm")
    if distance < 30:
        activate_buzzer()
    else:
        deactivate_buzzer()
    utime.sleep(0.1)


BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT