Overview

What is a theremin?

A theremin is an electronic musical instrument that you can play without even touching it - spooky! The theremin senses the distance from its antennae to the player's hands, and changes its pitch and volume based on these distances. The theremin was used to make soundtrack music for many iconic spooky movies, like The Day the Earth Stood Still (1958), and is perfect inspiration for a Halloween project!

Here, I've turned a pumpkin into a Jack-o-Theremin using an ultrasonic distance sensor and a Circuit Playground Express (CPX). The CPX uses the distance measurement to generate a sound and flash the onboard NeoPixels in various colors. This version of a theremin has a much more digital-bleepy-bloopy sound than a traditional theremin, but you still get to make music by waving your hand around in the air!

Materials

You'll need:

Circuit Playground Express

PRODUCT ID: 3333
Circuit Playground Express is the next step towards a perfect introduction to electronics and programming. We've taken the original Circuit Playground Classic and...
$24.95
IN STOCK

HC-SR04 Ultrasonic Sonar Distance Sensor + 2 x 10K resistors

PRODUCT ID: 3942
If you're like me, you've dreamed of being a dolphin - smoothly gliding through the water. Using your echo-location abilities to detect tasty fish treats. Until genetic...
$3.95
IN STOCK

3 x AAA Battery Holder with On/Off Switch and 2-Pin JST

PRODUCT ID: 727
This battery holder connects 3 AAA batteries together in series for powering all kinds of projects. We spec'd these out because the box is slim, and 3 AAA's add up to about...
$1.95
IN STOCK

Mini Metal Speaker w/ Wires - 8 ohm 0.5W

PRODUCT ID: 1890
Listen up! This tiny 1" diameter speaker cone is the perfect addition to any small audio project where you need an 8 Ω impedance and will be using 0.5W or less of power. We...
$1.95
IN STOCK

2-pin JST SM Plug + Receptacle Cable Set

PRODUCT ID: 2880
These 2-wire cables are 16cm long and come as a set.  One side has a 2-pin JST SM type connector plug on the end.  The other side has a matching 2-pin JST SM type...
$0.75
IN STOCK

Note: For my project, I used a scavenged speaker that I had in my spare parts box, but the speaker listed above will work just fine!

You'll also need a pumpkin to carve, and I highly recommend choosing a plastic or foam pumpkin for this project. That way, you can enjoy your Jack-o-Theremin for many Halloweens to come! If you do build this into a real pumpkin, you'll need to cover all the electronics with plastic wrap or seal them in a plastic bag inside the pumpkin.

Carve the Pumpkin

Start by carving a fun face (or other design) into your pumpkin as you normally would. Have fun with this, the spookier the better! Don't forget to cut a lid, too (and consider a notch so you remember the orientation if the lid).

When you're done decorating the front of your pumpkin, we need to add a few extra holes for our electronics.

Be careful with sharp objects during the cutting process. Children should be supervised depending on the implements used and adult discression.

Holes for the Rangefinder

Hold the rangefinder face down on where you want to install it on the Jack-o-Theremin. To the right of the face is a nice spot so you can watch the flash go while you play it. Use a pencil to trace around the sensor's emitters. Then, use your carving tool to carefully cut the holes. 

For a foam pumpkin, I found that a hot knife attachment for my craft iron worked fairly well, but it takes patience (that I don't have!) to get a clean cut. Take your time and cut slowly and carefully.

Hole for the Speaker

On the other side of the pumpkin, cut a hole for the speaker to fit in. Again, holding the speaker in place, trace around it so you know where to cut. Depending on the shape of your speaker, you may want to just cut a small hole for the speaker's wires to run through, and glue the speaker securely to the outside of the pumpkin.

Hole for the Battery Holder

This hole is optional. If you prefer to not have to reach inside the pumpkin to turn your Jack-o-Theremin on and off, you may want to keep the battery holder, with the on/off switch, outside the pumpkin. If so, cut a small hole in the back of the pumpkin for the wires to run through. 

CircuitPython Code

Getting Set Up for CircuitPython

This project is written in CircuitPython. If you're new to CircuitPython, visit the Welcome to CircuitPython guide for everything you need to know to get started!

For in-depth instructions on how to program your Circuit Playground Express with the code shown below, visit this page in the Circuit Playground Express guide.

While you can edit CircuitPython in any text editor of your choice, Adafruit highly suggests using Mu Editor, which contains helpers and an interactive REPL. To install Mu and learn how to use it, visit the Mu guide.

This code uses the Adafruit HC-SR04 library, which you will need to download and add to the lib folder on your Circuit Playground Express, if it's not already there. You can learn about installing the adafruit_HC-SR04 library in the Library guide. It is easiest to install the whole library package.

The Code

In a nutshell: This code sets up the HC-SR04 as a sensor for the Circuit Playground Express to read. The distances sent from the sensor can range from zero to over 400 cm. With a little math (the pitchMultiplier variable), we turn the distance into a frequency for the Circuit Playground Express to play through the connected speaker. We also use the wheel helper function, commonly used in Adafruit code with rainbow animations, to turn this distance measurement into an RGB value for the NeoPixels on-board the Circuit Playground Express.

When the sensor detects an object, your hand for instance, within 3 - 25 cm, it plays a sound and flashes its lights!

To learn more about the super handy wheel function and how it works, see this page in Kattni Rembor's Ikea Lamp Hack guide.

import time
import board
import adafruit_hcsr04
from adafruit_circuitplayground.express import cpx

# This line creates the distance sensor as an object.
sonar = adafruit_hcsr04.HCSR04(trigger_pin=board.D9, echo_pin=board.D6, timeout=0.1)
pixels = cpx.pixels
pitchMultiplier = 300   # Change this value to modify the pitch of the theremin.

def wheel(pos):
    # Input a value 0 to 255 to get a color value.
    # The colours are a transition r - g - b - back to r.
    if pos < 0 or pos > 255:
        return (0, 0, 0)
    if pos < 85:
        return (255 - pos * 3, pos * 3, 0)
    if pos < 170:
        pos -= 85
        return (0, 255 - pos * 3, pos * 3)
    pos -= 170
    return (pos * 3, 0, 255 - pos * 3)

while True:
    try:
        handDistance = int(sonar.distance)
        print("Distance:", handDistance)
    except RuntimeError:
        print("retrying!")
    time.sleep(.00001)

    pitch = handDistance*pitchMultiplier

    # Limits on the distances that trigger sound/light to between 3 and 25 cm.
    if (handDistance >= 3) & (handDistance < 25):
        cpx.play_tone(pitch, 0.1)
        pixels.fill(wheel(handDistance*10))
        pixels.show()
        time.sleep(.00001)
        print(pitch)
    else:
        cpx.stop_tone()
        pixels.fill((0, 0, 0))
        pixels.show()

Build the Circuit

The connections are as follows:

Sensor:

  • VCC on the HC-SR04 to VOUT on the CPX
  • TRIG on the HC-SR04 to A2 on the CPX
  • ECHO on the HC-SR04 to voltage divider (one end of the two 10k resistors)
  • Divided voltage (other end of the two resistors) to A1 on CPX
  • GND on the HC-SR04 to GND on the CPX
  • Also connect a wire from between the two resistors to GND on the CPX

Speaker + Amplifier:

  • PAM8302 A+ to CPX A0
  • PAM8302 A- to CPX GND
  • PAM8302 Vin to CPX Vout
  • PAM8302 GND to CPX GND
  • Speaker to PAM8302 output pins (wire color/order doesnt matter)
While you can try to use alligator clip wires, we recommend soldering the wires to have the best connections, especially if things are moved around. Soldering irons can get hot so younger ones will need supervision.

Connect the Sensor

Start by soldering four wires to the HC-SR04 Distance Sensor. Make your wires long enough to reach from the inside bottom of your pumpkin to the holes you cut for the sensor. For use in future pumpkins, that may be larger, give yourself a little extra length.

Cut and tin four wires for the HC-SR04 Distance Sensor.

Solder the wires to the pins of the HC-SR04.

Slide some heat shrink tubing over each pin and heat with a hot air gun for insulation.

Add the Resistors

The Echo pin on the sensor outputs a 5v signal, but the Circuit Playground Express (like many microcontrollers) is a 3.3V device. This means you'll need to divide the voltage of the Echo pin with two 10k resistors. For a more in-depth explanation of this setup, check out Kevin Walters' guide on using this sensor with a Circuit Playground Express.

Twist the two resistors together and solder the twist, then trim the wires to about 1 cm long. Be careful not to bend the wires back and forth too much, or they will break.

Solder wires to the resistors as shown. The white wire in this image connects to the Echo pin on the HC-SR04. The green and black wires will be soldered to the Circuit Playground Express in the next step.

Cover each connection with heat shrink tubing, then cover the whole resistor assembly with a larger piece of heat shrink tubing.

Make sure that the two wires that will go to the Circuit Playground Express are pointing in the same direction.

Connect the Circuit Playground Express

Follow the circuit diagram to connect the sensor to the Circuit Playground express. Solder the wires as shown below.

Add the Speaker

In most cases, you'll need to extend the wires of your speaker to reach from the hole you cut in your pumpkin to the bottom of the pumpkin, where the Circuit Playground Express will be.

We didn't realize at first but its not a great idea to connect a speaker directly to the A0 pin - the pin isn't designed to control a speaker. The wiring diagram at the top shows how to add a PAM8302 amplifier but the images below don't show it!

For permanent installation in a fake pumpkin: If your speaker will be inserted into its hole from the outside of the pumpkin, you'll need to feed it through this hole first before soldering it to the Circuit Playground Express.

For temporary installation in a real (or fake) pumpkin: To make it easy to move the whole project to a new pumpkin, add a 2-pin connector between the speaker and the CPX as shown.

Connect the speaker wires to the Circuit Playground Express. Since there is just one speaker in this project, it doesn't really matter which wire is positive and which is ground. Solder one wire to A0 and the other to GND on the CPX.

Installation

Plug the battery pack into your Circuit Playground Express (if locating inside) and carefully place the whole circuit inside your pumpkin. Slide the sensor into its holes and the speaker into its hole. If you kept the holes snug, the components should stay in place without additional help.

The Circuit Playground Express and battery pack will sit at the bottom of your project, and it can be helpful to stick the CPX to the battery pack with tape to keep it facing the front of the pumpkin. For a layer of diffusion, place a crumpled piece of white tissue paper or a paper towel in front of the Circuit Playground Express.

When everything is installed, just add batteries and you're ready to play with your Jack-o-Theremin!

Play!

Playing your Jack-o-Theremin is easy! Just turn it on and move your hand back and forth in front of the distance sensor. The Circuit Playground Express will translate your hand movements into interpretive pumpkin sound art, and provide a light show to go along with it. What could be spookier? 

This guide was first published on Oct 28, 2018. It was last updated on Oct 28, 2018.