Introduction: Ultrasonic Theremin With Scratch for Raspberry Pi

Control a ultrasonic distance sensor with Scratch for raspberry Pi and use in the ultrasonic theremin project of MagPi magazine (issue 58).


The project uses a Python program to send the sensor data to the Scratch program.

Step 1: Build the Circuit.

Picture of Build the Circuit.

Build the circuit following this scheme.

Step 2: Install Python Libraries and Make the Program

Picture of Install Python Libraries and Make the Program

To communicate Python wtih Scratch we need Scratchpy library.

Go to the tenrminal and type:

sudo pip install scratchpy

You will need the gpiozero library (preinstalled in raspbian).

Download the Python program here.

Step 3: Scratch Program I

Picture of Scratch Program I

Now we write a Scratch program that converts the distance from the sensor to a MIDI musical note and plays it.

The distance of the sensor is given in meters, so we calculate the musical note by converting the distance into cm and adding a value of 50 (you can play with this value depending on your taste). The program shows the note that is playing with the cat sprite.

Adding the sensor value to the Scratch program is a bit tricky, so we do it in five steps.

The first step is to write a version of the program (see image) not using the sensor value (it uses a zero constant value as a distance).

Step 4: Scratch Program Il

Picture of Scratch Program Il

Click mouse right button on the "Sensor value" block, and select "Enable remote sensor connections".

Step 5: Scratch Program Ill

Picture of Scratch Program Ill

Now, we execute the python program from the terminal. It will show the
distance in meters at the terminal screen and it will send the data to the Scratch program (Like in the first image). Return to Scratch (be careful not to close the terminal, the Python program needs to be running) and go to the "Sensing" tray (light blue). We select "UltrasonicSensor", this is our sensor, as shown in the second image.

Step 6: Scratch Program IV

Picture of Scratch Program IV

Next, we add a tick to the "Ultrasonic sensor sensor value" block, making a left click on the mouse, as shown in the image.

Step 7: Scratch Program V

Picture of Scratch Program V

Now, add the "Ultrasonic sensor sensor value" block to the Scratch program, as in the images.

Step 8: Run the Program...

Picture of Run the Program...

Now you can run the Scratch program, pressing the green flag.

You have to see the cat saying "Playing note <the note now is playing>" and a light blue and orange variables on the top (In orange the note, and in light blue the sensor value).


About This Instructable




Bio: Here I post our family creations mainly with Raspberry Pi and Kano.
More by jfaraudo:Ultrasonic Theremin With Scratch for Raspberry PiConnect PiStorms to Kano ComputerHacking a ball circuit toy with Raspberry Pi
Add instructable to: