Move your hand toward a distance sensor and hear the pitch rise. This small instrument turns a measured distance into sound, showing how a sensor, a mathematical rule and an output work together. It is inspired by a theremin, although it uses ultrasound rather than the original instrument’s radio-frequency sensing.
Target: original Raspberry Pi Pico or Pico H with the RP2040 microcontroller, running MicroPython 1.26. MicroPython is a version of Python for microcontrollers. Pico is different from a Raspberry Pi computer running Linux.

Parts and the important voltage boundary
- Pico/Pico H, USB data cable and breadboard.
- HC-SR04 ultrasonic module with a 5 V supply and a documented 3.3 V-compatible trigger input.
- 2.2 kฮฉ and 3.3 kฮฉ resistors, preferably 1% tolerance, for the echo voltage divider.
- Passive piezoelectric sounder suitable for 3.3 V drive, plus a 1 kฮฉ series resistor. Do not substitute a low-impedance speaker or magnetic buzzer.
The HC-SR04 sends an ultrasonic burst and returns a timed pulse. Its ECHO output can reach 5 V, while the Pico uses 3.3 V signals. A voltage divider, two resistors in series, reduces that signal before it reaches the Pico.
| Connection | Purpose |
|---|---|
| Pico VBUS, physical pin 40 โ sensor VCC | USB’s 5 V supply. Use USB power for this experiment. |
| Pico GND โ sensor GND | Common voltage reference. |
| GP14, physical pin 19 โ TRIG | Starts a measurement. |
| ECHO โ 2.2 kฮฉ โ GP15, physical pin 20 | Upper divider resistor. |
| GP15 node โ 3.3 kฮฉ โ GND | Lower divider resistor; both are required. |
| GP16, physical pin 21 โ 1 kฮฉ โ passive piezo โ GND | Current-limited tone output. |
With a 5.0 V echo, the nominal divided voltage is 5 ร 3.3 / (2.2 + 3.3) = 3.0 V. At 5.25 V it is 3.15 V before resistor tolerance. The divider draws about 0.91 mA at 5 V. Never wire ECHO directly to the Pico. VBUS is not the 3V3 output or the VSYS input.
Load the program
- Install the official MicroPython firmware for the original Pico, following Raspberry Pi’s MicroPython guide. Select the matching Pico interpreter in your editor.
- Disconnect USB while wiring. Check both divider resistors and the shared ground.
- Reconnect USB, save the following program to the Pico as
main.py, and run it. The editor’s serial console displays distance and frequency. - Hold a broad, flat object 10โ60 cm in front of the two round transducers. Move it slowly closer, then farther away.
- Remove the target or aim into open space. An invalid measurement should silence the sounder instead of producing an extreme note.
Full MicroPython program
"""Original Pico / Pico H + HC-SR04 with a 2.2k / 3.3k echo divider."""
def distance_from_pulse(pulse_us):
if pulse_us < 0:
return None
cm = pulse_us * 0.0343 / 2
return cm if 2 <= cm <= 80 else None
def frequency_for_distance(cm):
if cm is None or not 2 <= cm <= 80:
return None
return round(1800 - (cm - 2) * (1800 - 220) / (80 - 2))
def main():
from machine import Pin, PWM, time_pulse_us
import time
trigger = Pin(14, Pin.OUT, value=0)
echo = Pin(15, Pin.IN)
tone = PWM(Pin(16), freq=440, duty_u16=0)
try:
while True:
trigger.off()
time.sleep_us(2)
trigger.on()
time.sleep_us(10)
trigger.off()
pulse = time_pulse_us(echo, 1, 30000)
distance = distance_from_pulse(pulse)
frequency = frequency_for_distance(distance)
if frequency is None:
tone.duty_u16(0)
else:
tone.freq(frequency)
tone.duty_u16(32768)
print("cm:", distance, "Hz:", frequency)
time.sleep_ms(70) # More than 60 ms between trigger pulses.
finally:
tone.duty_u16(0)
tone.deinit()
trigger.off()
if __name__ == "__main__":
main()
How the measurement becomes a note
The trigger pulse lasts 10 microseconds. A microsecond is one millionth of a second. We use 343 metres per second as an approximate room-temperature speed of sound, or 0.0343 centimetres per microsecond. The pulse measures the outward and return trip, so distance in centimetres is pulse duration ร 0.0343 / 2.
A 1,000 ยตs pulse represents about 17.15 cm. Our rule maps 2 cm to 1,800 Hz and 80 cm to 220 Hz; Hz, or hertz, means cycles per second. A 41 cm distance gives 1,010 Hz. This is a linear mapping of distance to frequency, not a musical scale.
PWM means pulse-width modulation: repeatedly switching a digital output on and off. The program uses a 50% duty cycle, meaning equal high and low times, and changes its frequency to change pitch. The piezo element turns those changes into sound.
The measurement has a timeout, a maximum waiting period. A negative timeout result or a distance outside the chosen 2โ80 cm playing range becomes silence. The loop waits another 70 ms after a measurement, giving the sensor time before the next burst.
What to change and what to check
Soft fabric, tilted objects and a small hand can produce poor echoes; first test with a flat book. Clone modules differ: if a verified divider and supply still give no reading, check the module’s required trigger-high voltage. Do not โfixโ that by sending 5 V into the Pico. A fixed-pitch active buzzer will not follow this frequency mapping.
Challenge: why divide the travel distance by two?
The sound travels from the sensor to the object and back. Omitting the division doubles the reported distance.
Continue with the loaded voltage-divider experiment. References: HC-SR04 timing sheet, MicroPython pulse timing, RP2 pin and PWM reference, and Raspberry Pi MicroPython setup.



