Esp32Theremin - an Easy 2-Antenna Theremin
by ericCycles in Circuits > Gadgets
1759 Views, 5 Favorites, 0 Comments
Esp32Theremin - an Easy 2-Antenna Theremin
This is a very simple and cheap to build Theremin based on an ESP32 dev board. A theremin is a musical instrument that you play by moving your hands closer or further away from two antenna. One antenna controls the pitch (output frequency), while the other controls the amplitude (volume). This one is more toy quality than studio quality.
This design is somewhat unique in that the ESP32 does virtually everything except driving speakers directly, there are no other ICs, transistors, varactors, or inductors. The position of your hands is read using the ESP32's touch input circuitry (it is sensitive enough to pick up objects at short distances), this is mapped to wave form samples via the processors, and output to the ESP32's 2 built-in DACs (digital to analog converter). A weighted average of the two ADC outputs can then be routed to your computer speakers. You supply 5V power via the boards USB jack, and sound amplification is provided by common-as-dirt wired computer speakers.
Update: the software has been updated to improve the antenna sampling and output sampling rate (making high pitched sounds better).
Supplies
1x - ESP32 dev board 30 pin - you need an original ESP32 (aka "the classic). The S3, C3, and C6 variants do not have built-in DACs. The S2 has the DACs but they are on different pins.
1x - a 9.5cmx7.5cm (4" x 3") prototyping board (because making a PCB would be overkill)
2x - 15 pin female row headers (so you don't have to solder the dev board onto the prototyping board)
3x - 2 pin female row headers, 2 pin male row headers (two for antenna, one for audio output).
3x - momentary contact switches
very1x - a red LED
1x - a yellow LED
1x - a green LED
1x - blue LED
1x - 3.5mm audio jack (for the output)
4x - 270 ohm resistors
1x - 91k resistor
1x - 510R resistor
1x - 22nf capacitor
2x - 50k potentiometers (anything between 10k and 1M would be fine)
2x - antenna. I used 20cm (8") of electrical conduit pipe, but the system calibrates to what you supply.
single strand wire for connecting components on the board
multistrand wire for connecting off-board components
solder &soldering iron
hot glue & hot glue gun.
a box to put it in.
wired computer speakers
computer running arduino IDE
Optional
2x - 3.5mm audio jacks (for connecting antenna's to the board)
2x - 3.5mm audio plugs (for connecting antenna's to the board)
1x - 330uF capacitorattached
Background
one of thoseYou can skip reading this step without losing anything except human context.
This project began when my brother said "Lets make a theremin!" and like with most projects, I didn't know what I was getting myself into. I started with a bit of googling. The first thing I found was a two chip kit that was sold mainly to provide soldering practice. It used two 555 timers as capacitively controlled oscillators, and what was probably an 8 bit microcontroller to read the period of the signals and a PWM channel to output the resulting sound. My guess is that by modifying the duty cycle from 0 to 50%, they could control amplitude. The sound quality would have been really raspy but the 555 timers sounded like a good idea to borrow. However, it turns out they were relatively unstable, the frequency changed over time as parts warmed up.
I searched further, found the OpenTheremin project, and bought a v3 kit from aliexpress. Once assembled only the amplitude control worked. The OpenTheremin kit required soldering tiny surface mount components (7 ICs and discrete components the size of a grain of rice) and was virtually impossible to debug because it had to be clamped to a Arduino Uno components inwards, which means you can't do much probing while it's actually running.
Failing that, I thought I'd make a variation of the OpenTheremin using their circuit diagram but using through-hole components, an ESP32 dev board instead of the Arduino, hardware PWM channels instead of crystal oscillators, and combining two built-in 8 bit DACs for acceptable 16 bit output instead of an external 12 bit DAC. I was able to get input working, or output working, but not at the same time.
Then I tried repeating an experiment done by the "Matthias random stuff" youtube channel, which was getting values from hand capacitances just by using the ESP32's built-in touch input support. That seemed to provide as much sensitivity as I saw with the OpenTheremin kit or my own variation. The ESP theremin component list was now down to just the ESPDev board and some discrete components.
Building the Hardware
Given the simplicity of the circuit, I built this on a 9.5cmx7.5cm prototyping board. The bare board was a bit goofy in that the bus-lines were connected, so I cut a trace in the upper right corner as shown.
You need to keep the wires to the antennas away from any other wires (or metal) because those other wires will affect the capacitance readings.
Programming the ESP32
In your Arduino IDE, you need add support for ESP32. To do this, go to File/Preferences... and enter the following into Additional Boards manager URLS
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
You may need to restart the Arduino IDE at this point to get it to see the new library options.
Go into Tools/Board "whatever the last board you were using" > Board Manager ...
and install the "esp32 by Expressif Systems"
Then go into Tools/Manage Libraries... and install the DacESP32 library.
In Tools/Board > you should now find "ESP 32 Dev Module". Select it. If you don't use this board type, you may not be able to compile the source code. Not all ESP32 are the same.
Download the source files from
https://github.com/ericthecyclist/esp32theremin_sourceCode.git to your machine.
[Instructables wasn't allowing the uploading of some of the .h files so I had to move the files into their own github repo].
Create a project called Esp32Theremin and move the downloaded files into it. You should be able to build the project and upload it into an ESP32 Dev board via it's usb socket.
Most of the files are pretty simple little modules with explanatory comments. The dac.cpp ties a timer interrupt to a particular core - this is a holdover from an earlier version that operated more like the OpenTheremin project (measuring the interval between pulses of two oscillators). Adding more features like the OpenTheremin project supports should be pretty basic if one was interested.
Note: pin assignments are done in gpioAssignment.h. I find it useful to put them in one spot to keep track of which pins I'm already using. If you need to shuffle pins around because you are using an ESP32-S2 instead of an ESP 32 original, you would also need to change this file.
Overall Use
ESP32Theremin is mode oriented. You switch modes the pressing the buttons, and the LEDs tell you what mode you are in.
Initially you are in start mode and the red led is lit. After a few seconds, the sysem shifts to over to play mode and the blue LED is lit.
The other two modes are "Calibration" and "Tuning". Calibration is when the theremin determines the range of capacitance values for each antenna; you'll need to do it the first time you power up the theremin, and whenever you try using different antennas. Tuning allows you to select the range of pitch values by adjusting each potentiomenter. The Calibration and Tuning values are saved to persisistent storage; you don't need to redo them everytime you power up the theremin.
Calibration
When you press the calibrate button, the yellow led will start a cycle where it starts dark and then ramps up to full brightness and repeats. This indicates that it's trying to record the greatest capacitance value possible with your hands are AWAY from the antenna - so immediately move your hands away from the antennas as much as possible. This state continues for about 20 seconds.
When the the yellow LED stays on continously, it's trying to observe the greatest capacitance value possible when your hands are close to the antennas - so move each hand close to an antenna. It's sufficient to do this once.
Next, you must press the tune button or the play button so that the values are stored - if you power down the theremin before pressing one of those buttons, the values will be lost and you'll have to repeat this procedure.
Tuning
When you press the tuning button, the green led will light up dimly. This indicates that the system using the first pot to set on the lower pitch and the pitch output will reflect that. When you press the tuning button a second time, the green light will light up brightly indicating that you are setting the upper pitch with the second pot. You can flip back and forth between the two by pressing the tuning button again.
Next, you must press the calibrate button or the play button so that the values are stored - if you power down the theremin before pressing one of these button, the values will be lost and you'll have to repeat this procedure.
Playing
If you aren't already in play mode, press the play button. The blue LED will light up.
If you move your hands near the antenna, the pitch and/or volume will change in response. The red LED's brightness provides feedback about how close your hand is to the pitch antenna, the yellow LEDS's brightness provides feedback about how close your hand is to the volume antenna.