2 years ago

Raspberry Pi ultrasonic theremin: build a space-age instrument

Build your very own theremin musical instrument using an ultrasonic distance sensor and a little bit of Python and Sonic Pi code

A theremin is a unique musical instrument that produces sound without being touched.

In this tutorial, you will use an ultrasonic distance sensor to control the notes played by Sonic Pi.

You’ll need

The full article can be found in The MagPi 58 and was written by Marc Scott.

An ultrasonic distance sensor has four pins: Gnd (ground), Trig (trigger), Echo (echo), and Vcc (power).

To use the sensor, you need to connect its Gnd pin to a GND (ground) pin on the Raspberry Pi, the Trig pin to a GPIO pin on the Pi, and the Vcc pin to the 5V pin on the Pi.

The Echo pin is a little more complicated. It needs to be connected through a 330 ohm resistor to a GPIO pin on the Raspberry Pi, and that pin needs to be grounded through a 470 ohm resistor. The diagram shows one suggested arrangement. If you’ve wired up the sensor as shown in the diagram below, your echo pin is 17 and your trigger pin is 4.

Wire up the circuit like so

Click on Menu > Programming > Python 3 (IDLE), to open a new Python shell. Click on New > New File. The code to detect distance is listed in theremin1.py, below. Type it into your new file, then save and run it.

The sensor.distance is the distance in metres between the object and the sensor. Run your code and move your hand backwards and forwards. You should see the distance changing, as it is printed in the shell.

Getting Sonic Pi ready

Sonic Pi will receive messages from your Python script. Open Sonic Pi by clicking on Menu >  Programming > Sonic Pi. In the buffer that is open, you can begin by writing a live_loop. This is a loop that runs forever, but can easily be updated, allowing you to experiment. You can add a line to reduce the time it takes for Sonic Pi and Python to talk.

Next, you can sync the live loop with the messages that will be coming from Python.

The message that comes in will be a dictionary, containing the key :args. The value of this key will be a list, where the first item is the MIDI value of the note to be played.

Lastly, you need to play the note.

You can set this live loop to play straight away, by clicking on the Run button. You won’t hear anything yet, as the loop is not receiving any messages.

Sending notes from Python

To finish your program, you need to send note MIDI values to Sonic Pi from your Python file. You’ll need to use the OSC library for this part.

Now you need to create a sender object that can send the message.

You need to convert the distance into a MIDI value. These should be integers (whole numbers), and hover around the value 60, which is middle C. Round the distance to an integer, multiply it by 100, and then add a little bit, so that the note is not too low in pitch.

To finish off, you need to send the pitch over to Sonic Pi and reduce the sleep time. The final code is listed in theremin3.py below. Save and run your code and see what happens. If all goes well, you’ve made your very own theremin!

Final code listing