The idea of this project is inspired by (and for) the Connect things with code contest. I thought it'd be a fun challenge to create something interesting with only code, since that's the focus of the challenge after all. There is still a peripheral device, which is in this case a PC. The idea here is that anyone with a PSoC and a USB cable could run the code and play a Theremin-type instrument on their PC via Audacity. Let's get into it.
The code transforms the PSoC 6 MCU into something akin to a digital version of a Theremin. That's the inspiration, anyway. Theremin instruments are played without any touch so it's clearly a bit different, but calling this something like a capacitive touch-controlled tone generator just doesn't have the same ring to it. Still the notion is similar, where we use a slider instead of proximity to determine pitch. Plus Theremin is fun to say. Theremin.
Since part of the goal here is to not have much in the way of hardware setup, this part is going to be fast. Still, I like to consolidate things in one place to make things easy for people.
The initial steps to get going with the PSoC are outlined by Infineon, but for convenience, here they are (below):
For this project I used ModusToolbox. Specifically, I utilized the Eclipse IDE for ModusToolbox. In getting started, when you create a New Project, it has you select a template to get started For these, we're utilizing the CAPSENSE Buttons and Slider template and the emUSB-Device Audio Recorder FreeRTOS template.
I also found the template projects Hello World and PDM PCM Audio helpful for figuring out the timer functionality and the tone generation logic, respectively, but that code has already been integrated in the code I've shared within the project.
At the time, I hadn't realized that you can reference other projects so I combined what I needed in just one project, but it appears you can simply reference other projects by going to Properties --> Project References (as seen below):
This is the meat of the project, so here's how it works. The MCU's CapSense feature, designed for touch-sensing applications, is employed to detect finger positions on a slider, converting this interaction into musical tones. We setup and utilize the built in Capsense features for touch interactions and then prepare a tone generation mechanism using a timer. The "TouchTask" RTOS task continually processes CapSense data to check for touch presence and location on the slider. The touch location is then used to calculate a frequency value within a set musical range (220Hz to 880Hz). This value sets the timer's interrupt frequency, which in turn toggles a GPIO pin connected to a speaker, producing the corresponding tone. The interrupt service routine (ISR) for the timer ensures the pin toggles at the correct rate, thus generating the continuous tone until the touch is removed, at which point the "StopTone" function halts the tone generation and resets the pin state, stopping the sound. The combination of these components allows for a capacitive touch-based musical experience mimicking a Theremin's operation.
All that's to say, when you touch the Capsense, sounds happen at different frequencies.
Thankfully, a lot of the groundwork is taken care of by an example project, found here. There's a more up to date version of this, but this one seemed to suit this project's setup a bit more easily. It is designed to interface with a computer over USB for audio applications and has functionality for audio playback. It uses Audacity as the program, which is free and makes things easy for us.
After making sure you've got the example project and Audacity ready, connect your PSoC to the PC with a USB cable. Launch Audacity, go to its preferences, and set the PSoC as your recording device. Now, it's time to fire up the code from ModusToolbox. Once it's running, the CapSense slider on the PSoC should be responsive to your touch, translating it into audio frequencies that Audacity will pick up. You'll actually hear the sounds change as you move your finger along the slider, creating a tonal range from low to high just like a Theremin. Once that's done, you should be ready to roll and able to play the audio over the PC.
Hopefully you enjoyed! For anyone who followed along, I look forward to hearing about an exciting new rock band that features a PSoC Theremin 🤔. Have a good one.
Comments