from machine import Pin, I2C
import ssd1306
from hcsr04 import HCSR04
from time import sleep
i2c = I2C(scl=Pin(22), sda=Pin(21))
den1= Pin(2, Pin.OUT)
sensor = HCSR04(trigger_pin=5, echo_pin=18)
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
while True:
oled.fill(0)
distance = sensor.distance_mm()
print('Distance:', distance, 'cm')
oled.text("Distance (cm)", 0, 15)
oled.text(str(distance), 0, 35)
oled.show()
if distance <1000:
den1.on()
else:
den1.off()
Loading
ssd1306
ssd1306