from hcsr04 import HCSR04
from machine import Pin, I2C
import ssd1306
import time
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=100000)
lcd = ssd1306.SSD1306_I2C(128, 64, i2c)
sensor = HCSR04(trigger_pin=5, echo_pin=16, echo_timeout_us=1000000)
buzzer_pin = 12
buzzer = Pin(buzzer_pin, Pin.OUT)
try:
while True:
distance = sensor.distance_cm()
print("Distance in cm:", distance)
lcd.fill(0)
lcd.text("Distance:", 30, 20)
lcd.text(str(distance), 30, 40)
lcd.show()
if distance < 50:
buzzer.on()
time.sleep(0.5)
buzzer.off()
except KeyboardInterrupt:
pass