Raspberry Pi, ultrasonics, and music

Building a theremin with a Raspberry Pi using an ultrasonic distance sensor

rock concert audience party music celebrate
iggyshoot (CC BY 2.0)

My son is a musician and he’s mentioned several times that he’d like to get an instrument called a  theremin. If you haven’t encountered this instrument before, it consists of an antenna that the theraminist (yes, that is a real word) waves their hand around. The device responds with a musical tone that’s dependent on how close the theraminist’s hand is to the antenna. How does it actually work? According to Wikipedia:

The theremin uses the heterodyne principle to generate an audio signal. The instrument's pitch circuitry includes two radio frequency oscillators set below 500 kHz to minimize radio interference. One oscillator operates at a fixed frequency. The frequency of the other oscillator is almost identical, and is controlled by the performer's distance from the pitch control antenna. /  The performer's hand acts as the grounded plate (the performer's body being the connection to ground) of a variable capacitor in an L-C (inductance-capacitance) circuit, which is part of the oscillator and determines its frequency.

What does a theremin sound like? It traditionally produces the quintessential sci-fi wavering tone, for example, see (or rather, hear) The Day The Earth Stood Still 1951 - Theremin studio session.

I recently stumbled upon a design for a theremin-type instrument (in other words, a musical device controlled by hand waving) that’s much more up my street because instead of being based on the modification of radio frequency signals, it uses a Raspberry Pi and an ultrasonic range finder. This device was detailed on the Raspberry Pi Learning Resources page Ultrasonic theremin.

I just happened to have all the required components to hand and in about 15 minutes got the whole project up and running. The HC-SR04 ultrasonic distance sensors are cheap on Amazon (I bought five for about $10) and the only other things you need are a Raspberry Pi running Raspbian, two resistors (a 330Ω and a 470Ω), and a few pieces of wire. A breadboard is handy but you can just as easily tape it all together.

hc sr04 Mark Gibbs

The HC-SR04 ultrasonic distance sensor

The sensor, which has an ultrasonic pulse generator and a microphone next to each other, has four pins; 

  • VCC for power which will come from pin 2, the first of the two 5V pins on the Raspberry Pi’s 40-pin General Purpose Input Output (GPIO) header
  • Trig, which triggers the sensor to emit a burst of ultrasonic sound when GPIO 4 (pin 7) is enabled
  • Echo, which delivers a voltage proportional to the time it takes for the ultrasonic pulse to travel from the sensor to an object then return. This value is measured on GPIO 17 (pin 11).
  • GND which is, of course, ground and we’ll use pin 39 though any of the other GND GPIO pins can be used.

Here’s the circuit:

screen shot 2017 05 19 at 10.29.33 am Mark Gibbs

How to connect the HC-SR04 sensor to the Raspberry Pi GPIO header

Pretty simple, eh? Now, we’ve set up the hardware we need some software to access it so we need to have the GPIO Zero library installed. This is a Python library that makes working with the Raspberry Pi input/output hardware much easier (raspberry.org has a good introduction to the library). If you’re running Raspbian Jessie then this library is pre-installed; if you’re not, we leave it as an exercise for the user to upgrade. 

To see how the ultrasonic distance sensor works, run the following Python code: 

from gpiozero import DistanceSensor
from time import sleep

sensor = DistanceSensor(echo=17, trigger=4)

while True:
    print (sensor.distance)

gpiozero is an incredibly useful library; as you can see, it has built-in support for ultrasonic distance sensors so we only have to specify  which GPIO header pins are to be used and then, whenever we invoke sensor.distance, we'll get the current sensor reading.

You’ll see that the values produced by the sensor range from 1 for no return (i.e. infinite distance) down to near to 0 for very short distances. I haven’t got around to calibrating a sensor yet but the specifications state a ranging distance of 2 cm to 500 cm (under 1 inch to just over 16 feet) with a resolution of 0.3 cm (which is, of course a theoretical limit given that most things that the signal will reflect from won’t be perfectly smooth and normal to the sensor’s axis). For our purposes, i.e. making a musical instrument, we’re good with the inexactitude.

In the next installment: Turning the sensor's distance measurements into a musical notes.

Comments? Thoughts? Drop me a line then follow me on Twitter and Facebook. And sign up for my new newsletter!

Copyright © 2017 IDG Communications, Inc.

The 10 most powerful companies in enterprise networking 2022