from machine import Pin, I2C
import utime
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
# Configuración LCD I2C (dirección 0x27, 2 filas x16 columnas)
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
# Configuración de pines para 4 HC-SR04
sensors = [
{'trig': Pin(2, Pin.OUT), 'echo': Pin(3, Pin.IN)},
{'trig': Pin(4, Pin.OUT), 'echo': Pin(5, Pin.IN)},
{'trig': Pin(6, Pin.OUT), 'echo': Pin(7, Pin.IN)},
{'trig': Pin(8, Pin.OUT), 'echo': Pin(9, Pin.IN)},
]
def measure(sensor):
sensor['trig'].low()
utime.sleep_us(2)
sensor['trig'].high()
utime.sleep_us(10)
sensor['trig'].low()
while sensor['echo'].value() == 0:
pass
start = utime.ticks_us()
while sensor['echo'].value() == 1:
pass
duration = utime.ticks_diff(utime.ticks_us(), start)
return (duration / 2) / 29.1 # cm
while True:
distances = [round(measure(s), 1) for s in sensors]
lcd.clear()
lcd.putstr(f"D1:{distances[0]:4}cm D2:{distances[1]:4}cm")
utime.sleep(1)
lcd.clear()
lcd.putstr(f"D3:{distances[2]:4}cm D4:{distances[3]:4}cm")
utime.sleep(1)