jvmccall
Published © GPL3+

Ultrasonic Theremin

This is an arduino based theremin which uses two ultrasonic sensors to control the volume and frequency of the sound output.

BeginnerShowcase (no instructions)7,031
Ultrasonic Theremin

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Breadboard (generic)
Breadboard (generic)
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×2
Non Volatile Digital Potentiometer, 10 kohm
Non Volatile Digital Potentiometer, 10 kohm
https://www.newark.com/microchip/mcp4161-103e-p/digital-potentiometer-10kohm-257step/dp/77M2738?COM=ref_hackster
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Speaker: 3W, 4 ohms
Speaker: 3W, 4 ohms
×1

Hand tools and fabrication machines

Hot glue gun (generic)
Hot glue gun (generic)
Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Theremino Circuit

Code

Theremin Code

Arduino
//In this project we create a theremin with two ultrasonic sensors, one for volume and another for tone. We use the digital potentiometer X9C103P.
// Two ultrasonic sensors are used to create a theremin. One sensor controls volume, the other controls frequency.
// This project is an iteration on a previous project I found for a theremin. The previous project had library conflicts (two libraries used the same timer) and used a different type of poteniometer. I have also added some other small features.
// Project which provided original inspiration: https://create.arduino.cc/projecthub/opus9/theremino-f72d32
// Resource on controlling similar potentiometer using SPI protocol, provides example code and electrical setup: http://www.learningaboutelectronics.com/Articles/MCP4131-digital-potentiometer-circuit.php

 *
 */
//NewTone is used to avoid the timer incompatibiliy of Tone with NewPing. 
#include <NewTone.h>
#include <NewPing.h> 
#include <SPI.h>
#define MIN_DISTANCE 35 

NewPing sonar(5, 6, 35); //sonar(pin, pin, distance), this controls volume 

// name pins for sensors and the potentiometer


// For SPI
byte address = 0x00;
int CS= 10;

int echo = 3;                            
int trigger = 4;

int distance;
int PotValue;

unsigned long TimeResponse;
float distance2;
float tone1;

void setup() {
// set all the pins 
Serial.begin(9600); 
pinMode(trigger, OUTPUT); //Tone sensor                    
pinMode(echo, INPUT); //Tone sensor    
pinMode (CS, OUTPUT);
SPI.begin();

//This has the speaker emit a start up sound from Low to High to Low
digitalPotWrite(255);
for (int i = 50; i <= 400; i++)
{
pinMode(9, OUTPUT);
  NewTone(9,i);
  delay(10);
}
delay(500);
for (int i = 400; i >= 50; i--)
{
pinMode(9, OUTPUT);
  NewTone(9,i);
  delay(10);
}

  
}

void loop() {
  
  digitalWrite(trigger, HIGH);           
  delayMicroseconds(10);   // creates a 10 microsecond pulse                      
  digitalWrite(trigger, LOW);                   
  TimeResponse = pulseIn(echo, HIGH);  // records how long it took to receive echo of the pulse
  distance2 = TimeResponse/58;  //calculates distance in centimeters


  if (distance2 < MIN_DISTANCE) { 
  //Conversion of distance into a value for a sound 
  tone1 = 50.0*pow(2,(distance2/12.0));  //calculate frequency, range of about 50-360 Hz
  pinMode(9, OUTPUT);
  NewTone(9,tone1);
    }
  else {
    pinMode(9, OUTPUT); // This sets the sound to a high pitched noise when no distance is detected
  NewTone(9,400);
  }
 
distance = sonar.ping_cm(); //Uses the library NewPing to calculate distance

//distance is converted to Potentiometer relevant values

int PotValue = map(distance, 0, 35, 240, 255); // I only use a range of resistance which is very low otherwise the speaker is too quiet becuae it is only a 4 ohm speaker

if (distance == 0) // sets volume to max when no hand is detected or if hand is too far away
{
  PotValue = 255;
}

digitalPotWrite(PotValue);
delay(10);

  
}

int digitalPotWrite(int value)
{
digitalWrite(CS, LOW); //This uses SPI protocol to communicate with the potentiometer and sets the resistance
SPI.transfer(address);
SPI.transfer(value);
digitalWrite(CS, HIGH);


}

Credits

jvmccall
0 projects • 1 follower
Contact

Comments

Please log in or sign up to comment.