PyRasp

← All practice problems

Raspberry Pi Pico practice problem · Raspberry Pi Pico

Print the distance

MediumBudget $7MicroPython

An HC-SR04 is wired with TRIG on GP20 and ECHO on GP21 through a 1 kΩ + 2 kΩ divider, so ECHO is safe for the 3.3 V GPIO. VCC is 5 V (VBUS); GND is shared.

Every 0.5 s, ping and print the distance as a whole number of centimetres, like 42 cm. The test object is 42 cm away.

trig.value(1)
utime.sleep_us(10)
trig.value(0)
distance_cm = time_pulse_us(echo, 1, 30000) / 58

More in Raspberry Pi Pico