from hcsr04 import HCSR04
from machine import Pin, I2C
import ssd1306
import time
# ESP32 Pin assignment
i2c = I2C(scl=Pin(5), sda=Pin(4), freq=100000)
lcd = ssd1306.SSD1306_I2C(128, 64, i2c)
sensor = HCSR04(trigger_pin=12, echo_pin=13, echo_timeout_us=100000)
led_pin = 14 # Replace with the actual pin number you connect the LED to
led = Pin(led_pin, Pin.OUT)
while True:
distance = sensor.distance_cm()
print(distance)
lcd.fill(0)
lcd.text(" RIDWAN ", 30, 20)
lcd.text(str(distance), 30, 40)
lcd.show()
if distance <= 20:
led.on()
else:
led.off()
time.sleep(1) # Optional delay to avoid rapid LED toggling