# Objetivo del Proyecto: Medición de distancia con ultrasonido HCSR04 y OLED ssd1306
# Hardware y conecciones requeridas:
# Raspberry Pi Pico GND
# Ultrasonido HC-SR04
# OLED SSD1306
# Programador: Computadoras y sensores, adaptado por José Andrés Montoya Espinosa
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
import utime
trigger = Pin(15, Pin.OUT)
echo = Pin(14, Pin.IN)
Jarak = 0
# INICIALIZA LA PANTALLA
i2c = I2C(0,sda=Pin(4), scl=Pin(5), freq=40000)
oled = SSD1306_I2C(128,64,i2c)
while True:
trigger.high()
utime.sleep(0.00001)
trigger.low()
while echo.value() == 0:
comienzo = utime.ticks_us()
while echo.value() == 1:
final = utime.ticks_us()
duracion = final - comienzo
Jarak = (duracion * 0.0343)/2
print("Jarak:",Jarak,"cm")
oled.fill(0)
oled.text("Jarak:",0,2)
oled.text(str(Jarak) + " cm",0,17)
oled.show()
utime.sleep(3)