A digital theremin built on an STM32F401RE (Nucleo board): an HC-SR04 ultrasonic sensor measures how far your hand is, and the microcontroller turns that distance into a tone in real time, generated as a PWM signal.
Exam project for Digital Programmable Systems, B.Sc. in Computer and Automation Engineering (Ingegneria Informatica e dell'Automazione), Politecnico di Bari.
📄 Full report: Ultrasonic Theremin – A Digital Sound Synthesis Project (PDF)
Hand ──► HC-SR04 ──echo──► TIM1 (1 µs tick) ──► distance [cm]
│
linear mapping 1–100 cm → 300–800 Hz
│
Buzzer/Speaker ◄── PA1 PWM ◄── TIM2 CH2 (ARR updated at runtime)
│
UART2 @115200 ──► "Distance: xx cm, Freq: yy Hz"
- Distance measurement: a 10 µs pulse on
TRIGstarts a measurement. TIM1 runs at 1 MHz and times the width of theECHOpulse, which gives the distance (d = t · 0.034 / 2cm). The waits have timeouts, so a missing echo can't freeze the loop. - Distance → pitch: the distance is limited to 1–100 cm and mapped linearly onto 300–800 Hz.
- Sound generation: TIM2 Channel 2 outputs a PWM signal. Each loop iteration recalculates its auto-reload register (ARR), so the output frequency follows the hand.
- Debug output: distance and frequency are printed on USART2 (115200 baud) and can be read from any serial monitor.
| Component | Notes |
|---|---|
| STM32 Nucleo-F401RE | ARM Cortex-M4 MCU |
| HC-SR04 | Ultrasonic distance sensor |
| Passive buzzer / small speaker | Driven by the PWM output |
| Signal | MCU pin |
|---|---|
HC-SR04 TRIG |
PA10 (GPIO output) |
HC-SR04 ECHO |
PA4 (GPIO input) |
| Audio out (TIM2_CH2 PWM) | PA1 |
| UART TX / RX | PA2 / PA3 (ST-Link virtual COM port) |
- C with the STM32 HAL
- STM32CubeIDE / CubeMX for peripheral configuration and code generation
- Peripherals: GPIO, hardware timers (input timing + PWM), UART
Core/
├── Inc/ # Headers (main.h, HAL config, interrupt handlers)
├── Src/
│ ├── main.c # Application logic: distance reading, mapping, PWM update
│ └── ... # CubeMX-generated init, MSP and system files
└── Startup/ # Startup assembly for STM32F401RETx
- Open the project in STM32CubeIDE (File → Import → Existing Projects into Workspace).
- Build it and flash it to a Nucleo-F401RE over ST-Link.
- Wire the sensor and the buzzer as shown above, then open a serial terminal at 115200 baud to watch the live readings.