Theremin:Edition 2

At the starting of this year and the even semester of our third year,  Shankhajyoti and Sparsh and I(Tejas) decided to carry forward the Neo-theremin designed by last year’s team..

Soon we had cleared all the high level details of our project like the design aesthetic of the theremin, what kind of sensors to use and what mini-computer to use. This was the easy part. We decided we would use the Raspberry Pi computer and two sensors to achieve the required design: Ultrasonic Sensor(HC-SR-04) and 3-axis accelerometer and gyroscope(MPU6050).

To get the Raspberry Pi up and running is a feat in itself. Because of this, the first part of this blog will be about how to quickly set up the Raspberry Pi and modify its settings to our needs. So here goes

Part 1: Setting up the Raspberry Pi

  1. Working on a linux PC will make the job a lot easier and this blog will stick to instructions for linux PCs.
  2. Download the zip file of the raspbian stretch operating system with desktop and recommended hardware from here:Raspbian Download
  3. Extract the “.img” file from the zip file.
  4. The operating system in the “.img” file has to be burnt to an SD Card of at least 16GB storage space. This can be done through a software “Etcher”. This software is very intuitive and once the image is flashed/burnt to the SD Card, there will be two partitions of the SD Card: rootfs and boot.
  5. Browse into the “boot” partition and create two files: A blank file with the name “ssh” and another file with the name “wpa_supplicant.conf”.
  6. The following lines have to be written into the “wpa_supplicant.conf” file. NetworkSSID is the name of the WiFi the raspberry pi has to connect to and Password is the security key of the WiFi.


7. Now eject the SD Card and insert it into the Raspberry Pi(powered down)

8. Connect the PC you are working on to the same WiFi your Raspberry Pi will connect to and power up the Raspberry Pi. Disconnect any ethernet cable connected to the PC or Pi.

9. On your PC, open the terminal and type “hostname -I”. This will return your PC’s IP address on the WiFi network. Suppose it is “”.Since the Pi is also on the same network, its IP address will be a variation of your PC’s address. To find the IP address of the Pi, we need the “nmap” command. Type “sudo apt-get install nmap” on your PC.

10. Once it is installed run the command “nmap -sn”. This will scan all IP addresses from to and return all IPs that are up ad running along with their hostnames. Look up the entry with hostname “raspberrypi” and note its IP address.

11. Now to remotely access the Pi over the WiFi network, we will use SSH(Secure Shell). We enabled SSH in the Pi when we added the blank file named ssh to the boot partition of the SD Card. Now type the following command on the terminal.

ssh pi@IP

12. You will get a security warning. Type yes and press enter. The Pi will now ask for the password. All fresh installations of raspbian have “username: pi” and “password:raspberry”.

13. Enter the password and we’re in!!

Part 2: Projecting Pi’s Desktop to PC

To make working with the Pi easier, we use a software called vnc to project the Pi’s desktop to our laptop’s screen. This software uses the ssh connection to create a virtual desktop for the Pi.

These are the steps we took

  1. Install vncserver on the pi with the command on the Pi’s terminal
    sudo apt-get install realvnc-vnc-server realvnc-vnc-viewer
  2. Now type “sudo raspi-config” on the Pi’s terminal and press enter. A menu will pop up. Navigate to interfacing options and select VNC. Then toggle to yes and press enter. This will enable vnc.
  3. Now install vncviewer on the PC. The software can be downloaded from VNC Viewer
  4. Now run the command vncserver on the Pi’s command line.
  5. On your PC open the VNC Viewer application and enter the Pi’s IP address in the search bar.VNC Viewer dialog showing IP address
  6. The Pi’s desktop will now open. Click OK when the security warning for SSH pops up. Skip the update. Now you can work conviniently with the Raspberry Pi.

Part 3: Streaming audio on the 3.5mm jack

To start building the theremin, we need to verify if the audio jack on the Pi is working properly. We have a few prerequisites for this. We have to install the pyaudio library in python.

  1. First we need to install portaudio modules: sudo apt-get install libasound-dev
  2. Download the portaudio archive from:
  3. Unzip the archive: tar -zxvf [portaudio.tgz]
  4. portaudio.tgz should be replaced by the actual name of the file downloaded.
  5. Enter the directory, then run: ./configure && make
  6. Install: sudo make install
  7. And finally: sudo pip install pyaudio
  8. Check the version of pyaudio, it should be 0.2.11

Once pyaudio is installed open the text editor on the Pi and write the following code.

import numpy as np
import pyaudio

p = pyaudio.PyAudio()

volume = input("Enter the volume (0.1 to 1): ")
fs = 44100 #Sampling Rate
duration = 5.0 #Duration of Sound in Seconds
f = input("Enter the frequency (201 - 17999): ") #Frequency
samples = (np.sin(2*np.pi*np.arange(fs*duration)*f/fs)).astype(np.float32) #Generating Samples

stream =, channels=1, rate=fs, output=True) 



Save this file and run it. It will ask you for two inputs: Volume and frequency. Enter various values and listen to the sound produced.

If you are hearing white noise there are some changes to be done. Go to the boot partition of the pi and add the line “audio_pwm_mode=2” to the file “config.txt”. Then run the command “sudo rpi-update”. After the update is done, reboot the Pi. the issue should be resolved now.

In the next two parts we will be interfacing the HC-SR04 and the MPU6050 respectively with the Pi.

Part 4:Interfacing Ultrasonic Sensor(HC-SR04)

The HC-SR04 is a very simple sensor to use. It has four pins: VCC, Ground, Trigger and Echo. The sensor measures time taken for 8 pulses of high frequency sound (Ultrasound:40Khz) to hit the closest solid object and return. We can calculate the distance by multiplying this time with the speed of sound(34300 cm/s) and dividing by two.

For working with the GPIO pins on the Pi we need to install a python library RPi.GPIO. Once it is installed we connect the pins of Pi and HC-SR04 in the following way.


The VCC and Ground pins of the Ultrasonic sensor are connected to the respective pins on the Pi. Two GPIO pins are chosen on the Pi to connect to the trigger and echo pins on the sensor. The GPIO pin connected to the trigger pin is an output pin and the one connected to echo is an input pin. Since echo is a 5V pin, we build a voltage divider to convert it to 3.3V which the GPIO pin is rated for.

The operation of the sensor is as follows

  1. The Trigger pin is set to high for 0.01ms (which starts sound transmission) and then set to low. At this point the echo pin becomes high. We note this time.
  2. Once the transmitted sound hits a solid object and returns, the echo pin goes low and we again note this time.
  3. The difference between the two times noted is multiplied by 34300cm/s and divided by two to account for the round  trip. This gives us the distance.

The following code tests the Ultrasonic Sensor

import RPi.GPIO as GPIO
import time


#set GPIO Pins

#set GPIO direction (IN / OUT)

def distance():
# set Trigger to HIGH

# set Trigger after 0.01ms to LOW
GPIO.output(GPIO_TRIGGER, False)

StartTime = time.time()
StopTime = time.time()

# save StartTime
while GPIO.input(GPIO_ECHO) == 0:
StartTime = time.time()

# save time of arrival
while GPIO.input(GPIO_ECHO) == 1:
StopTime = time.time()

# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2

return distance

if __name__ == '__main__':
while True:
dist = distance()
print ("Measured Distance = %.1f cm" % dist)

# Reset by pressing CTRL + C
except KeyboardInterrupt:
print("Measurement stopped by User")

The output on the command line is as follows


Note that the block starting with “if __name__ == ‘__main__’:” will run only when the above file is the main(topmost) file. If this file is imported by another module, the block won’t run as the module using won’t need us to print the measured distance.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s