import time
import board
import busio
# I2C setup for Raspberry Pi Pico
i2c = busio. I2C(scl=board.GP5, sda=board.GP4)
while True:
timeout = 1 # seconds
start_time = time.monotonic()
# Try to lock I2C with timeout
while not i2c.try_lock():
if time.monotonic() - start_time > timeout:
print("No I2C devices found!")
break
else:
try:
devices = i2c.scan()
if devices:
print("I2C devices found:", [hex(device) for device in devices])
else:
print("No I2C devices found!")
finally:
i2c.unlock()
time.sleep(2)