from machine import Pin
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import utime
import utime
trigger = Pin(4,Pin.OUT)
echo = Pin(5, Pin.IN)
i2c = I2C(1, sda=Pin(2), scl=Pin(3), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
def ultra():
trigger.low()
utime.sleep_us(2)
trigger.high()
utime.sleep_us(5)
trigger.low()
while echo.value() == 0:
signaloff = utime.ticks_us()
while echo.value() == 1:
signalon = utime.ticks_us()
timepassed = signalon - signaloff
distance = (timepassed*0.0330)/2
print ("the distance from object is", distance, "cm")
oled.fill(0)
oled.show()
utime.sleep(1)
# Display text on the OLED display
oled.text("Distance,", 0, 0) # Write text at position 0, 0 (x, y)
oled.text(str(distance), 0, 20) # Write text at position 0, 20 (x, y)
oled.show()
while True:
ultra()
utime.sleep(1)