from machine import Pin, time_pulse_us,I2C
from time import sleep_us, sleep
from ssd1306 import SSD1306_I2C
i2c=I2C(0,sda=Pin(0), scl=Pin(1), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
# Define the GPIO pin numbers for the trigger and echo pins
ECHO_PIN = 26
TRIGGER_PIN = 27
buzz = Pin(1,Pin.OUT)
# Initialize trigger and echo pins
trigger = Pin(TRIGGER_PIN, Pin.OUT)
echo = Pin(ECHO_PIN, Pin.IN)
def measure_distance():
# Ensure trigger is low initially
trigger.low()
sleep_us(2)
# Send a 10 microsecond pulse to the trigger pin
trigger.high()
sleep_us(10)
trigger.low()
# Measure the duration of the echo pulse (in microseconds)
pulse_duration = time_pulse_us(echo, Pin.high)
# Calculate the distance (in centimeters) using the speed of sound (343 m/s)
distance = pulse_duration * 0.0343 / 2
return distance
def main():
while True:
# Measure the distance and print the value in centimeters
distance = measure_distance()
if distance < 150.55:
buzz.value(1)
else:
buzz.value(0)
print("Distance: {:.2f} cm".format(distance))
oled.text("Distance: {:.2f} cm".format(distance), 0, 32)
oled.show()
sleep(1)
oled.text(" ", 0, 0)
oled.show()
sleep(1)
# Wait for 1 second before taking the next measurement
sleep(1)
if __name__ == "__main__":
main()